Web Analytics Made Easy -
StatCounter JLabel visibility on JPanel - CodingForum


No announcement yet.

JLabel visibility on JPanel

  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved 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.
    //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
    //Add label B
    this.add(info, -1);
    //Make label B visible (redundant)

    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; Sep 7, 2011, 05:55 PM. Reason: Resolved

  • #2
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


    • #3
      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; Sep 7, 2011, 05:11 PM.


      • #4
        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.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


        • #5
          Excellent. This was what I was looking for. Thanks