Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Image to a label  RSS feed

 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've studying swing for a month now and I am gradually beginning to understand swing's concept.

But,

I'm having problems on adding an image to a label a slight confusion,

The error says label1 and label2 needs to be initialized before I can use them to display images. Why is it so ? And is there any alternate methods to display images ?

My Code is:



Thanks
 
Rodion Gork
Ranch Hand
Posts: 47
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the error message looks exactly, please?

UPD. Ah, I see, it is "variable label1 might not have been initialized".

Surely, when your execution come to this line, label1 is not yet assigned a value. But you already try to use label1.RIGHT.

As it is a static field, you should use JLabel.RIGHT instead, or more precisely SwingConstants.RIGHT.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodion Gork wrote:As it is a static field, you should use JLabel.RIGHT instead, or more precisely SwingConstants.RIGHT.

Radion, you are right, but my code produces the label2 image, it's skipping the label1 image when I use JLabel.RIGHT.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestions ?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JFrame (or, more precisely, its contentPane) has a BorderLayout by default, and you're adding both labels to the (default) BorderLayout.CENTER position. Read more about layouts here: Laying Out Components Within a Container
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you should add all content to a GUI before, and not after, making it visible.

Rarely, when you do need to change the contents after a GUI is visible, that will need a revalidate() and often, a repaint().
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Also, you should add all content to a GUI before, and not after, making it visible

I have modified my code
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I have made line 12 comment , I came to know that that Line 7 where I have given the path, that does not read by the jvm, any idea ?
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And does that code work? You need to tell us what is happening. Where are those two image files? Also how large are they? I tried your code with some photos of my own, so I had to change the paths, and all I could see was blue sky, because the photos are 2848×4272px and overwhelm the size of the GUI. I had to use GIMP to reduce the size of the photos before I could see them properly.

If you comment out line 11 (not 12) you lose the declaration of label2 and line 13 won't compile. And you haven't taken any notice of what Darryl Burke said about layouts.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your last question I suggested you look at the Swing tutorial for example on using Swing components and on how to better structure your code.

You replied: Yes Rob, started to study them thoroughly.

So why does your code look nothing like the tutorial examples (there are examples that use images on labels)?

Why is your GUI not created on the Event Dispatch Thread?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
So why does your code look nothing like the tutorial examples (there are examples that use images on labels)?

Yes Rob, my program is very low level at the moment as I am trying to understand each component functionality separately.

And about examples of oracle documentation i was writing line by line the example of ButtonDemo.java -> http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ButtonDemoProject/src/components/ButtonDemo.java I was stuck on:



If you could explain the above code snippet I would gladly use that program(ButtonDemo.java)

Thanks,
Ray
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Why is your GUI not created on the Event Dispatch Thread?


And I haven't done multi threading yet, when I will understand the concept then I will of course use it.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And does that code work? You need to tell us what is happening. Where are those two image files? Also how large are they? I tried your code with some photos of my own, so I had to change the paths, and all I could see was blue sky, because the photos are 2848×4272px and overwhelm the size of the GUI. I had to use GIMP to reduce the size of the photos before I could see them properly.

If you comment out line 11 (not 12) you lose the declaration of label2 and line 13 won't compile. And you haven't taken any notice of what Darryl Burke said about layouts.

1. My First image file is on C:/Users/Rahul/Downloads/Image.jpeg SIZE: 1.15 MB
2. My Second image is on where my source file is saved. SIZE: 63 KB AND FORMAT .PNG
3. And I forget to check Darryl's LayoutManager advice really sorry about that.



This is my original code:
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray Anderson wrote: . . . And I haven't done multi threading yet, . . .
You do not multi‑thread GUIs. According to Horstmann and Cornell (Core JavaII 8/e) it has never been possible to create a thread‑safe GUI framework which runs faster than an arthritic snail swimming in treacle. So you ensure that (with the possible Exception of specialised classes like SwingWorker) everything runs on one thread, called the EDT as Rob told you. That is quite simple; there are instructions in the Java Tutorials.Since all the code for your class is in the constructor, there is no need to retain a reference to the object created.All code in the GUI classes unchanged.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand each component functionality separately.


Which is why you start with working examples from the tutorial.

Then you make changes. If it works, great. If not, then you know it is something your did. If you don't understand why it still doesn't work then you ask a question stating what you changed.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok understood and maybe I am getting too much dependent on you guys for asking silly questions, I should try seriously on my own and ask questions if there is a serious problem, yeah guys your right.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:A JFrame (or, more precisely, its contentPane) has a BorderLayout by default, and you're adding both labels to the (default) BorderLayout.CENTER position


Ah yes now I UNDERSTAND that we need to add a LayoutManger to sort out the components, Thanks Darryl.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we extend the JPanel class ?
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to override its paintComponent method.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!