Results 1 to 14 of 14
Thread: Making this JLabel Appear
- 06-12-2011, 12:30 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Making this JLabel Appear
My friends and are are trying to program a smaller version of the pokemon safari zone. I'm attempting to output a small, 7x6 image of a grassy area, but this isn't outputting an image, can someone please tell me what I am doing wrong?
Java Code:import javax.swing.*; import java.awt.*; import java.util.*; /** *Grid Component */ public class Grid extends JFrame { private JLabel mapLabel; public Grid() { super("Safari Zone"); setSize(300,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel imagePanel = new JPanel(); add(imagePanel); ImageIcon zone = new ImageIcon("C:\\Documents and Settings\\Bradley\\My Documents\\BradleyWork2010\\AP Java\\ClassWork\\Final\\Map.jpeg"); mapLabel = new JLabel(zone); imagePanel.add(mapLabel); setVisible(true); } }Last edited by Fubarable; 06-12-2011 at 12:31 AM. Reason: code tags added
- 06-12-2011, 12:41 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I would guess the image isn't found.
Also make sure you create the GUI on the Event Dispatch Thread. See: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
- 06-12-2011, 05:12 AM #3
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
maybe try using .validate() or .repaint() on the JLabel?
- 06-12-2011, 06:14 AM #4
- 06-12-2011, 10:47 AM #5
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
After the setVisible(true) line add:
imagePanel.repaint();
imagePanel.updateUI();Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 06-12-2011, 11:21 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Why not imagePanel.validate() as well? It can't do any harm. The OP could also consider sacrificing a small goat.
Or he or she could consider whether or not the image as specified was found. And create the GUI on the Event Dispatch Thread. This involves putting some faith in what camickr says about things Swing: but it's a sure winner for the goats.
-
- 06-12-2011, 05:27 PM #8
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
- 06-12-2011, 05:46 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Because this is the initial display of the GUI and you don't need to do anything like that.how come it's irrelevant?
repaint() is when you've changed a property of a visible component.
updateUI() is when you change the LAF of a visible component. You should never use this method. See How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel) for the proper way to change the LAF on a visible GUI.
Its a brute force solution that some people use because they don't understand how Swing works and should NOT be used or recommended. The proper way to tell Swing that components have been added is to use:i often found when they weren't showing up it was because I needed to update them with one of those methods so I thought maybe that'd help him too.
Java Code:panel.add(...); panel.revalidate(); panel.repaint();
- 06-12-2011, 11:33 PM #10
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
I changed the image from JPEG to PNG and it worked, just wondering why this happened
- 06-12-2011, 11:48 PM #11
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
-
-
I thought camickr already explained this. What about his explanation do you not understand?
Some additional thoughts:
Here you're advocating calling validate or repaint on the label. Since the label isn't acting as a container, validate makes no sense, and since if you set it's text or image it will display it automatically, repaint is completely unnecessary.
Then you had a bug in that code that you don't yet understand (and neither do we as we don't have access to your code).
And since you admittedly don't understand Swing which is pretty darn complex, perhaps you might want to study up on Swing before giving advice so that your understand and advice are both a bit better. The Swing tutorials are a good place to start and the source code would be the next place to have a look.Last edited by Fubarable; 06-13-2011 at 02:41 AM.
- 06-13-2011, 05:53 AM #14
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
well he just said why it shouldn't be used in this case but he seemed very adament about not using it at all and so i was just wondering why that was
I'm sorry, i was just trying to give some advice since I knew that helped me, didn't realize it would do any harmAnd since you admittedly don't understand Swing which is pretty darn complex, perhaps you might want to study up on Swing before giving advice so that your understand and advice are both a bit better. The Swing tutorials are a good place to start and the source code would be the next place to have a look.
i'll definitely start reading up on swing though, thank you!
Similar Threads
-
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
jLabel
By Matty in forum AWT / SwingReplies: 3Last Post: 09-22-2008, 10:22 PM -
GUI - JLabel
By Azndaddy in forum New To JavaReplies: 8Last Post: 05-02-2008, 07:03 AM -
JLabel
By Jack in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 01:55 PM -
JLabel
By Freddie in forum AWT / SwingReplies: 2Last Post: 05-29-2007, 02:19 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks