ImageIcon not showing in JButton
I am trying to display an icon in a JButton, but when I run the application, it just shows a blank JButton with no image inside. The path is correct and I am not getting any errors...just a blank button. I'm not sure where I am going wrong.
JButton quickGuide = new JButton();
Icon icon1 = new ImageIcon("/Project1/images/quickGuideIcon.png");
quickGuide.setIcon(icon1);
quickGuide.setBounds(490, 10, 50, 50);
quickGuide.setContentAreaFilled(false);
java user-interface imageicon
|
show 2 more comments
I am trying to display an icon in a JButton, but when I run the application, it just shows a blank JButton with no image inside. The path is correct and I am not getting any errors...just a blank button. I'm not sure where I am going wrong.
JButton quickGuide = new JButton();
Icon icon1 = new ImageIcon("/Project1/images/quickGuideIcon.png");
quickGuide.setIcon(icon1);
quickGuide.setBounds(490, 10, 50, 50);
quickGuide.setContentAreaFilled(false);
java user-interface imageicon
1
What happens when you stop using setBounds(), and instead use a layout manager that respects the preferred size of the button? What is the output you get if you open a terminal, and execute the commandls -l /Project1/images/quickGuideIcon.png
?
– JB Nizet
Nov 24 '18 at 17:52
The path: /Project1/images/quickGuideIcon.png looks suspicious. Are you on Linux?
– user10639668
Nov 24 '18 at 18:02
I'm on Mac using Eclipse. That's the path that is shown in the properties of the image in Eclipse.
– Kev
Nov 24 '18 at 18:07
1
Why don't you answer the question? What is the output you get if you open a terminal, and execute the commandls -l /Project1/images/quickGuideIcon.png
?
– JB Nizet
Nov 24 '18 at 18:18
eclipse most probably is showing the relative path to the file; Java is expecting the absolute one.... (will equal only ifProject1
is in the root, hardly possible unless you are using the root as workspace for eclipse)
– Carlos Heuberger
Nov 24 '18 at 18:35
|
show 2 more comments
I am trying to display an icon in a JButton, but when I run the application, it just shows a blank JButton with no image inside. The path is correct and I am not getting any errors...just a blank button. I'm not sure where I am going wrong.
JButton quickGuide = new JButton();
Icon icon1 = new ImageIcon("/Project1/images/quickGuideIcon.png");
quickGuide.setIcon(icon1);
quickGuide.setBounds(490, 10, 50, 50);
quickGuide.setContentAreaFilled(false);
java user-interface imageicon
I am trying to display an icon in a JButton, but when I run the application, it just shows a blank JButton with no image inside. The path is correct and I am not getting any errors...just a blank button. I'm not sure where I am going wrong.
JButton quickGuide = new JButton();
Icon icon1 = new ImageIcon("/Project1/images/quickGuideIcon.png");
quickGuide.setIcon(icon1);
quickGuide.setBounds(490, 10, 50, 50);
quickGuide.setContentAreaFilled(false);
java user-interface imageicon
java user-interface imageicon
asked Nov 24 '18 at 17:45
KevKev
133
133
1
What happens when you stop using setBounds(), and instead use a layout manager that respects the preferred size of the button? What is the output you get if you open a terminal, and execute the commandls -l /Project1/images/quickGuideIcon.png
?
– JB Nizet
Nov 24 '18 at 17:52
The path: /Project1/images/quickGuideIcon.png looks suspicious. Are you on Linux?
– user10639668
Nov 24 '18 at 18:02
I'm on Mac using Eclipse. That's the path that is shown in the properties of the image in Eclipse.
– Kev
Nov 24 '18 at 18:07
1
Why don't you answer the question? What is the output you get if you open a terminal, and execute the commandls -l /Project1/images/quickGuideIcon.png
?
– JB Nizet
Nov 24 '18 at 18:18
eclipse most probably is showing the relative path to the file; Java is expecting the absolute one.... (will equal only ifProject1
is in the root, hardly possible unless you are using the root as workspace for eclipse)
– Carlos Heuberger
Nov 24 '18 at 18:35
|
show 2 more comments
1
What happens when you stop using setBounds(), and instead use a layout manager that respects the preferred size of the button? What is the output you get if you open a terminal, and execute the commandls -l /Project1/images/quickGuideIcon.png
?
– JB Nizet
Nov 24 '18 at 17:52
The path: /Project1/images/quickGuideIcon.png looks suspicious. Are you on Linux?
– user10639668
Nov 24 '18 at 18:02
I'm on Mac using Eclipse. That's the path that is shown in the properties of the image in Eclipse.
– Kev
Nov 24 '18 at 18:07
1
Why don't you answer the question? What is the output you get if you open a terminal, and execute the commandls -l /Project1/images/quickGuideIcon.png
?
– JB Nizet
Nov 24 '18 at 18:18
eclipse most probably is showing the relative path to the file; Java is expecting the absolute one.... (will equal only ifProject1
is in the root, hardly possible unless you are using the root as workspace for eclipse)
– Carlos Heuberger
Nov 24 '18 at 18:35
1
1
What happens when you stop using setBounds(), and instead use a layout manager that respects the preferred size of the button? What is the output you get if you open a terminal, and execute the command
ls -l /Project1/images/quickGuideIcon.png
?– JB Nizet
Nov 24 '18 at 17:52
What happens when you stop using setBounds(), and instead use a layout manager that respects the preferred size of the button? What is the output you get if you open a terminal, and execute the command
ls -l /Project1/images/quickGuideIcon.png
?– JB Nizet
Nov 24 '18 at 17:52
The path: /Project1/images/quickGuideIcon.png looks suspicious. Are you on Linux?
– user10639668
Nov 24 '18 at 18:02
The path: /Project1/images/quickGuideIcon.png looks suspicious. Are you on Linux?
– user10639668
Nov 24 '18 at 18:02
I'm on Mac using Eclipse. That's the path that is shown in the properties of the image in Eclipse.
– Kev
Nov 24 '18 at 18:07
I'm on Mac using Eclipse. That's the path that is shown in the properties of the image in Eclipse.
– Kev
Nov 24 '18 at 18:07
1
1
Why don't you answer the question? What is the output you get if you open a terminal, and execute the command
ls -l /Project1/images/quickGuideIcon.png
?– JB Nizet
Nov 24 '18 at 18:18
Why don't you answer the question? What is the output you get if you open a terminal, and execute the command
ls -l /Project1/images/quickGuideIcon.png
?– JB Nizet
Nov 24 '18 at 18:18
eclipse most probably is showing the relative path to the file; Java is expecting the absolute one.... (will equal only if
Project1
is in the root, hardly possible unless you are using the root as workspace for eclipse)– Carlos Heuberger
Nov 24 '18 at 18:35
eclipse most probably is showing the relative path to the file; Java is expecting the absolute one.... (will equal only if
Project1
is in the root, hardly possible unless you are using the root as workspace for eclipse)– Carlos Heuberger
Nov 24 '18 at 18:35
|
show 2 more comments
1 Answer
1
active
oldest
votes
The Path you see in Eclipse is relative to the project's root:
Location tag contains the full path you need to provide.
You can also provide relative path to the location where the JVM is launched like this: "./relative/path/to/file"
. Notice the dot in front of the relative path.
If you plan to locate the image inside the jar file use:
new ImageIcon(getClass().getResource("images/quickGuideIcon.png"));
where the above location is classpath relative.
I plan on packing the image with the JAR file. On someone else's computer the location path won't be the same. Won't I have to use the path in the project folder because that is where the image will be?
– Kev
Nov 24 '18 at 18:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460839%2fimageicon-not-showing-in-jbutton%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The Path you see in Eclipse is relative to the project's root:
Location tag contains the full path you need to provide.
You can also provide relative path to the location where the JVM is launched like this: "./relative/path/to/file"
. Notice the dot in front of the relative path.
If you plan to locate the image inside the jar file use:
new ImageIcon(getClass().getResource("images/quickGuideIcon.png"));
where the above location is classpath relative.
I plan on packing the image with the JAR file. On someone else's computer the location path won't be the same. Won't I have to use the path in the project folder because that is where the image will be?
– Kev
Nov 24 '18 at 18:55
add a comment |
The Path you see in Eclipse is relative to the project's root:
Location tag contains the full path you need to provide.
You can also provide relative path to the location where the JVM is launched like this: "./relative/path/to/file"
. Notice the dot in front of the relative path.
If you plan to locate the image inside the jar file use:
new ImageIcon(getClass().getResource("images/quickGuideIcon.png"));
where the above location is classpath relative.
I plan on packing the image with the JAR file. On someone else's computer the location path won't be the same. Won't I have to use the path in the project folder because that is where the image will be?
– Kev
Nov 24 '18 at 18:55
add a comment |
The Path you see in Eclipse is relative to the project's root:
Location tag contains the full path you need to provide.
You can also provide relative path to the location where the JVM is launched like this: "./relative/path/to/file"
. Notice the dot in front of the relative path.
If you plan to locate the image inside the jar file use:
new ImageIcon(getClass().getResource("images/quickGuideIcon.png"));
where the above location is classpath relative.
The Path you see in Eclipse is relative to the project's root:
Location tag contains the full path you need to provide.
You can also provide relative path to the location where the JVM is launched like this: "./relative/path/to/file"
. Notice the dot in front of the relative path.
If you plan to locate the image inside the jar file use:
new ImageIcon(getClass().getResource("images/quickGuideIcon.png"));
where the above location is classpath relative.
edited Nov 24 '18 at 19:00
answered Nov 24 '18 at 18:19
user10639668
I plan on packing the image with the JAR file. On someone else's computer the location path won't be the same. Won't I have to use the path in the project folder because that is where the image will be?
– Kev
Nov 24 '18 at 18:55
add a comment |
I plan on packing the image with the JAR file. On someone else's computer the location path won't be the same. Won't I have to use the path in the project folder because that is where the image will be?
– Kev
Nov 24 '18 at 18:55
I plan on packing the image with the JAR file. On someone else's computer the location path won't be the same. Won't I have to use the path in the project folder because that is where the image will be?
– Kev
Nov 24 '18 at 18:55
I plan on packing the image with the JAR file. On someone else's computer the location path won't be the same. Won't I have to use the path in the project folder because that is where the image will be?
– Kev
Nov 24 '18 at 18:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460839%2fimageicon-not-showing-in-jbutton%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What happens when you stop using setBounds(), and instead use a layout manager that respects the preferred size of the button? What is the output you get if you open a terminal, and execute the command
ls -l /Project1/images/quickGuideIcon.png
?– JB Nizet
Nov 24 '18 at 17:52
The path: /Project1/images/quickGuideIcon.png looks suspicious. Are you on Linux?
– user10639668
Nov 24 '18 at 18:02
I'm on Mac using Eclipse. That's the path that is shown in the properties of the image in Eclipse.
– Kev
Nov 24 '18 at 18:07
1
Why don't you answer the question? What is the output you get if you open a terminal, and execute the command
ls -l /Project1/images/quickGuideIcon.png
?– JB Nizet
Nov 24 '18 at 18:18
eclipse most probably is showing the relative path to the file; Java is expecting the absolute one.... (will equal only if
Project1
is in the root, hardly possible unless you are using the root as workspace for eclipse)– Carlos Heuberger
Nov 24 '18 at 18:35