Results 1 to 18 of 18
  1. #1
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default 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.
    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);
    
        }
    theLevel is a JPanel that is inside a JScrollPane.

    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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)

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    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.

  5. #5
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    After adding or deleting a label to/from your JPanel try a revalidate() on the panel.

    kind regards,

    Jos

  7. #7
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    thank you for the help, but still no luck, i tried repaint and revalidate, nothing seems to work :(

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I can paste my entire project, it isn't very large.
    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.

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Bongeh View Post
    thank you for the help, but still no luck, i tried repaint and revalidate, nothing seems to work :(

    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.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    if you need an example, i can code a small jpanel with jscrollpane, buttons and labels.
    There are plenty of examples of doing this in the Swing tutorials.

    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.

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by camickr View Post
    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.

    can you post the code that define theLevel or can you even post all the code that is envolved in the jscrollpane?

  12. #12
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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))
            );

  13. #13
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    then afterwards i can use setBounds all i want to alter the position of the labels.
    I doubt that works. Try resizing the frame after you've used the setBounds() method. The components will snap back into there original positions.

  14. #14
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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

  15. #15
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    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.

  16. #16
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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.

  17. #17
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    but it wasn't getting me anywhere fast.
    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.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by camickr View Post
    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.
    Strongly seconded

Similar Threads

  1. [SOLVED] Adding JLabel to JFrame
    By mlfatty in forum AWT / Swing
    Replies: 3
    Last Post: 03-04-2009, 11:33 PM
  2. Adding wav sound into a JLabel.
    By hitmen in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2009, 05:59 AM
  3. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 01:32 PM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 01:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •