• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Adding JLabel from another class to my main frame  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a basic Java program that has a number of different Swing components on a single JFrame. One of those components is a JPanel that has some buttons and a JLabel on it. The program will get pictures from a directory and display them in the just mentioned JLabel. I have 4 instances of JPanel's attached to my JFrame. (Attached screenshot below bc "a picture is worth 1000 words")

At first, I had all this code in 1 large class but, realizing there was a lot of redundancy, decided to make a class called PicDisplay which would extend JPanel and that I could call from my main program. This class would create the JPanel, all the buttons and label, and hold the methods that allow me to manipulate the Icon (in the label). However, when I got to the point where I needed to attach some of the buttons and labels to the JPanel I realized I didn't know how to do this.


java_screenshot.png
[Thumbnail for java_screenshot.png]
What the app looks like
 
Master Rancher
Posts: 3267
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

to attach some of the buttons and labels to the JPanel


By attach I assume you mean add a Component Like button or label) to a Container(like JPanel).  If you have a reference to the button or label objects,
then use the JPanel's add method to add that component to the JPanel.
 
Chris Whitcomb
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I do want to attach a label to the Container (JPanel). I'm not sure I understand your response. I apologize, I'm very much a beginner. I tried to add a line like (at 32) PicDisplay.add(archiveOrTrashText); but I'm getting an error.
 
Norm Radder
Master Rancher
Posts: 3267
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but I'm getting an error.


Please copy the full text of the error message and paste it here. It has important info about the error.

Also post the code that has the error.


The use of PicDisplay is wrong.  Since the code is inside of a class that extends JPanel, the code can call the JPanel's class's directly: 

Also that syntax is incorrect.  To call a class's method you would use a reference to an instance of the class:
 
Chris Whitcomb
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense to me. Thanks for explaining. I'm not getting an error anymore but I'm also not seeing the JLabel. Here is my program that contains the main() method and calls the PicDisplay object:



And here is the code for PicDisplay:


I've attached an image of the result I get when I run the code.

Any help would be appreciated and thank you for the help so far.
When-I-run-program.png
[Thumbnail for When-I-run-program.png]
 
Master Rancher
Posts: 2758
93
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Chris,

there are some remarks that can be made about your code (for instance using no layoutmanager and restricting your panel to just one image), but to see the text in your JLabel, add this line to the constructor of your PicView (for instance just before the call to setVisible):
Since you don;t invoke that method, you only see the panel, but not the JLabel.
 
Chris Whitcomb
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That worked! I am adding a few more things to the JPanel and have some functions that I also have to add but wanted to keep the code here simple so people didn't have to wade through a ton of it. I haven't had much experience with layout managers yet but that's next on the list to conquer. Thanks again!
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, carry on! I certainly liked the screenshot you showed us.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!