Results 1 to 7 of 7
  1. #1
    Ambergahill is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Smile Last JLabel I add to JPanel doesnt show up

    I am adding JLabels to my JPanel, and the last one always does not show up. For example if I add 3 JLabels, the 3rd will not show up. If i change it to add 4, the 4th will not show up, but the 3rd will now. Here is a snipit of what my code looks like:

    setLayout(new BorderLayout());
    for(int x=0; x<PEDALCOUNT; x++)
    {
    pedals[x].setBounds((x*PEDALWIDTH),0,PEDALWIDTH,PEDALHEIGHT );
    add(pedals[x]);
    }

    A workaround that I DO NOT want to keep doing is to put an extra fake JLabel at the end of the for loop, that way everything I want to show up does, but the fake one does not.

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

    Default

    I think that you'l need to create and post an SSCCE for us to understand what's wrong. Also, why the setBounds?

  3. #3
    Ambergahill is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default

    SSCCE? What is that?

    is setBounds a bad thing? I kinda just picked the first thing that worked for me and went with it. I havn't done JAVA GUI stuff in ages.

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

    Default

    For SSCCE, see the 3rd link in my signature.

    setBounds is used with null layout, a layout that should be avoided. If you're using BorderLayout, setBounds doesn't do squat and is misleading. In fact if using BorderLayout and adding components without a second parameter, only the last added component will show... which confuses me, hence the need for an SSCCE that we can run, test and modify. Else we'll not be able to see your bug.

  5. #5
    Ambergahill is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default

    Thanks for the tip. I changed it to null layout and it worked.

    Could you tell me why null layout is bad? I feel like I need it because I need my objects at very specific x/y coordinates, but maybe I am wrong.

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

    Default

    For one, it doesn't scale well with large projects. Imagine you have a complicated GUI with many components including a grouping of JRadioButtons in the mid left side. Then later you find out you need to add another JRadioButton, so now you need to manually adjust the layouts of all components below and to the right of the added button. Contrast that with proper use of layout managers, you simply add the new button and the layout managers do the rest.

    Other issues include running a GUI on different OS's with differing screen resolutions. With null layout it's easy to make a GUI that looks great on one and horrific on another. With layout managers, the GUI looks consistent from one OS and resolution to another.

  7. #7
    Roberthig is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Default payday loans guaranteed approved

    qmbyxqz, <a href="http://www.snakepaydayloans.co.uk">payday loans no checks no brokers</a> , qmbpztlovuemq

Similar Threads

  1. combobox doesnt show!
    By LennyKosmos in forum New To Java
    Replies: 2
    Last Post: 10-26-2010, 08:07 PM
  2. doesnt show exact size
    By clydedoris in forum NetBeans
    Replies: 3
    Last Post: 07-13-2010, 06:59 AM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  4. JTable doesnt show columun names!
    By phil128 in forum AWT / Swing
    Replies: 3
    Last Post: 03-08-2009, 10:39 PM
  5. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 01:32 PM

Tags for this Thread

Posting Permissions

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