Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JLabel visibility on JPanel

    I need someone familiar with the properties of JLabel to understand the way around this problem. Basically, I have two labels, label A and label B. When the user clicks label A it is supposed to show label B next to it. Simple enough. However, the only way I can get B to show us is by setting A's visible modifier to false. Once A is visible, B is no longer shown. No, the labels do not overlap.

    Here is the specific code in question. It is located in the repaint method of my extension of JPanel. Don't bother looking at the various variables. its the commented line that I have a problem with. If I put it in place this code works, if I do not the last add line might add the label but it doesn't show up in the Panel.
    Code:
     
    //Label B -- The Label to be shown when Label A is clicked (called info)
    info.setBounds(originX+nations[INFO_PLACE].Xcord(year)/zoomFactor)+10, originY+(nations[INFO_PLACE].Ycord(year)/zoomFactor),              info.getWidth(), info.getHeight());              
    
    //Label A -- the Label to be clicked, stored in an Array called nationLabels (changing its icon to the selected icon)                nationLabels[INFO_PLACE].setIcon(nations[INFO_PLACE].image_selected(year));
    
    //Set Label A to invisible
    //nationLabels[INFO_PLACE].setVisible(false);
    
    //Add label B
    this.add(info, -1);
    
    //Make label B visible (redundant)
    info.setVisible(true);

    Finally, as a side note, I also have a slightly smaller problem.. if anyone knows how to fix it great. My zoom buttons only seem to work in the Panel which they affect. Once I move them to a separate Panel and call the zoom methods remotely, nothing happens. I have tried calling the repaint method in the zoom methods, and have also tried having the parent of the Panel to be zoomed to be repainted. Nothing. I can't even repaint the Panel remotely from a separate Panel, even though they are in the same Frame.

    But as I said, my biggest problem is with the Label visibility problem.
    Last edited by WTell; 09-07-2011 at 11:55 PM. Reason: Resolved

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    How is label A added to the JPanel in context with 'this'?
    And are you using the BorderLayout manager? I wouldn't expect that -1 will work with the border layout, but assuming that it is, and that Label A was also added with the same criteria as label b (this.add(component, -1)), then you are stacking components in the same area when you are only capable of adding one. To do so, you'll need to subdivide the northern section into another layout frame and add accordingly, or use a different layout manager.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That sounds like exactly what I need. I am adding the labels via this due to the fact that the class they are in extends JPanel, allowing it to be added directly to my mainframe.

    What is the layout component called that I need to add to the Panel? Is there a Layout class or similar that that I can add? And, if so, is it flexible enough to accommodate random label placement? My labels are not arranged in normal rows and columns, but rather scattered haphazardly across the Panel
    Last edited by WTell; 09-07-2011 at 11:11 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    There are several layout managers built into Java; BorderLayout is the default if none is specified. You can view more information on these here: http://download.oracle.com/javase/tu...ut/visual.html
    Some managers are better suited for 'random' placement, or expanding placements such as rows and columns. There is nothing wrong with specifying a location using an absolute placement as well, but you cannot stack multiple components using the BorderLayout manager (you can, but the newest on the stack will win). Assuming you don't actually mean that they are just laying wherever, you may find that the GroupLayout manager is most suiting to what you are looking to use. It creates a professional look, but maintains a lot of flexibility in the placement of components.

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent. This was what I was looking for. Thanks


  •  

    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
    •