Results 1 to 18 of 18
- 04-04-2010, 11:28 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Adding a JLabel to a JPanel - jlabel not showing
Hi guys,
i'm trying to add some labels to a jpanel when a button is clicked, the labels ideally have icons set to them, but as that wasnt working, im just trying to get labels with text appear but to no avail.
The code works perfectly if i add new JButtons, but if i try to add a JLabel nothing happens.
theLevel is a JPanel that is inside a JScrollPane.Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { theStack.add(0, jComboBox1.getSelectedItem()); b1 = new javax.swing.JButton(); b1.setText(jComboBox1.getSelectedItem().toString()); theLevel.add(b1); Dimension size = b1.getPreferredSize(); b1.setBounds(0,0, size.width, size.height); objectList.add(0, b1); jTextPane1.setText(objectList.toString()); l1 = new javax.swing.JLabel("title"); l1.setIcon(new javax.swing.ImageIcon(getClass().getResource("image.png"))); theLevel.add(l1); Dimension size2 = l1.getPreferredSize(); l1.setPreferredSize(new Dimension(100,100)); l1.setBounds(0,0, size2.width, size2.height); l1.setVisible(true); }
When this code runs, a button is added no problem, with text inside, but the JLabel just won't show up. I've looked all over google and can't seem to fix it myself :(
Any ideas?
-
Have you tried calling repaint() on your JPanel/container, theLevel after adding components?
It looks as if you're trying to the JLabel and the JButton to the exact same spot -- 0, 0 -- and that you're using a null layout, both of which may be places where you want to improve your code.
- 04-04-2010, 11:37 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
yeah i tried repaint, but have found that is only nessasary when removing items (buttons).
i need the objects to be able to overlap (the labels will contain images)
- 04-05-2010, 12:46 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You need to understand how Z-ordering works. Basically the last component added is painted first. Since your label is last added, I'm guessing other components are painted on top of it. The Overlap Layout writeup tries to explain this in a little more detail.
Another approach might be to use layered panes. Read the Swing tutorial for more information.
If you need more help post your SSCCE that demonstrates the problem.
- 04-05-2010, 10:06 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
thank you for the help but this is definitly not the issue, i have code that lets me move the buttons out of the way, and the label is not underneath, and even when i try set the label to a different possition it still doesnt come up.
I am using netbeans, and if i try to place labels all over my jpanel, they don't show up. When i try to place jbuttons, they show up but with no text, only jbuttons i have created with script have text in them..
I can paste my entire project, it isn't very large.
- 04-05-2010, 11:49 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
After adding or deleting a label to/from your JPanel try a revalidate() on the panel.
kind regards,
Jos
- 04-05-2010, 04:03 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
thank you for the help, but still no luck, i tried repaint and revalidate, nothing seems to work :(
- 04-05-2010, 04:32 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I didn't ask for you entire project. If you don't want to post a SSCCE I guess you really don't want any help. Good luck.I can paste my entire project, it isn't very large.
- 04-05-2010, 05:10 PM #9
java programming has nothing to do with "luck". before orchestrating different components you have to build and run the single components. i presume Fubarable has right with his assumption of null layout. i also suggest to try with add(component, BorderLayout.PLACE) to add your components. if you need an example, i can code a small jpanel with jscrollpane, buttons and labels.
- 04-05-2010, 06:15 PM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
There are plenty of examples of doing this in the Swing tutorials.if you need an example, i can code a small jpanel with jscrollpane, buttons and labels.
That is why the OP has been asked to post his SSCCE because we can't guess what he is doing differently than the examples in the tutorial.
- 04-05-2010, 06:54 PM #11
- 04-06-2010, 12:04 PM #12
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Hi guys, thank you for your time, i've managed to fix it myself. I just needed to add this code first, then afterwards i can use setBounds all i want to alter the position of the labels.
My project was so small at this point that it was practically a SCEE. Sorry for confusion/frustration.
Thanks again.
Java Code:javax.swing.GroupLayout jPanel1Layout2 = new javax.swing.GroupLayout(theLevel); theLevel.setLayout(jPanel1Layout2); jPanel1Layout2.setHorizontalGroup( jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout2.createSequentialGroup() .addGap(0, 0, 0) .addComponent(l1) .addContainerGap(213, Short.MAX_VALUE)) ); jPanel1Layout2.setVerticalGroup( jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout2.createSequentialGroup() .addGap(0, 0, 0) .addComponent(l1) .addContainerGap(283, Short.MAX_VALUE)) );
- 04-06-2010, 04:18 PM #13
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I doubt that works. Try resizing the frame after you've used the setBounds() method. The components will snap back into there original positions.then afterwards i can use setBounds all i want to alter the position of the labels.
- 04-06-2010, 04:36 PM #14
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
only the last item created snaps back into place, all the others stay put.
It's not desirable but its only 1 label, and considering the application is geared around moving objects around it should be fine.
It's an inhouse thing anyway, so ill just mention it to colleagues.
Serializing data now, fun fun. Thanks for the help anyway
- 04-06-2010, 04:39 PM #15
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
That because you only set the bounds on the last component.
Apparently you don't want to learn the proper way to do things. You should never use setBounds() when using a layout managers.
Why do you ask a question if you ignore the advice given? I guess next time I won't bother giving advice since it won't be used any way. Good luck.
- 04-06-2010, 05:05 PM #16
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
I apologise for being dismissive with your help, but it wasn't getting me anywhere fast. I plan to explore java properly and more indepth another time, but for now, i had about 2weeks to slap together a very quick application. It currently does the job it was required to do, with 1 negligable error.
I didn't want to use a layout manager, but the labels wouldnt display without them.
- 04-06-2010, 05:13 PM #17
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Thats because you didn't provide us with the information we needed to solve the problem. You asked your question 2 days ago. If you would have taken the 5 minutes to put together a simple SSCCE we could have found your problem and solved it for you, thereby actually saving you time and improving your Java understanding.but it wasn't getting me anywhere fast.
-
Similar Threads
-
[SOLVED] Adding JLabel to JFrame
By mlfatty in forum AWT / SwingReplies: 3Last Post: 03-04-2009, 11:33 PM -
Adding wav sound into a JLabel.
By hitmen in forum AWT / SwingReplies: 2Last Post: 02-17-2009, 05:59 AM -
[SOLVED] JLabel not showing on JPanel
By onefootswill in forum New To JavaReplies: 11Last Post: 08-23-2008, 01:32 PM -
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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks