Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JLabel consume space when not visible?

Tags:

java

swing

I have a program which creates 2 Panels and then places a label and two buttons in them. The label is set to invisible setVisible(false) and then the two buttons are added and the frame is packed. When i click the first button, the label is shown, setVisible(true), and the seccond one hides it again, setVisible(false). When i click each button, they move to fill the space of the label as it hides, and move again to get out of the way of the label as it is shown. I want to stop this from happening and have the buttons stay in the same place even when the label is hidden.

Here is the code:

public class MainFrame extends JFrame{
  public JLabel statusLabel;
  public JButton show;
  public JButton hide;  

  public MainFrame(){
    super("MagicLabel");

    JPanel topPanel = new JPanel();  //Create Top Panel
    statusLabel = new JLabel("");    //Init label
    statusLabel.setVisible(false);   //Hide label at startup
    topPanel.setSize(400, 150);      //Set the size of the panel, Doesn't work
    topPanel.add(statusLabel);       //Add label to panel

    JPanel middlePanel = new JPanel(); //Create Middle Panel
    show= new JButton("Show");       //Create show button
    hide= new JButton("Hide");       //Create hide button
    middlePanel.setSize(400, 50);    //Set the size of the panel, Doesn't work
    middlePanel.add(show);           //Add show button
    middlePanel.add(hide);           //Add hide button

    this.add(topPanel, "North");     //Add Top Panel to North
    this.add(middlePanel, "Center"); //Add Middle Panel to Center
    addActionListeners();            //void:adds action listeners to buttons
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(100, 100, 512, 400);
    this.setPreferredSize(new Dimension(400,200)); //Set size of frame, Does work
    this.pack();
    this.setVisible(true);
  }

  public void animateInstall(boolean var0){  //Void to show and hide label from action listeners
    statusLabel.setVisible(var0);
    sendWorkingMessage("Boo!");
  }
  public void sendWorkingMessage(String message){ //Void to set text of label
    this.statusLabel.setForeground(new Color(225, 225, 0));
    this.statusLabel.setText(message);
  }

  void addActionListeners(){
    show.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            animateInstall(true);
        }
    });
    hide.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            animateInstall(false);
        }
    });
  }
like image 471
TankWhoCriedTom Avatar asked Jan 04 '13 06:01

TankWhoCriedTom


4 Answers

Use CardLayout. Add the JLabel and empty JPanel. Instead of seting it visible/invisible swap the cards showing the JLabel or the JPanel when necesary.

like image 41
StanislavL Avatar answered Sep 17 '22 05:09

StanislavL


Extending JFrame is not advisable, better extend JPanel put all your components inside and then add it to a JFrame

You need to learn how to use SwingUtilities.invokeLater(): See example how your should look like

You need to learn about Layout: Tutorial

Very dumb and easy approach in your code would be:

 this.statusLabel.setForeground(bgColor); //background color
 this.statusLabel.setText("           "); //some number of characters
like image 27
Nikolay Kuznetsov Avatar answered Sep 17 '22 05:09

Nikolay Kuznetsov


this.setPreferredSize(new Dimension(400,200));
this.setMinimumSize(new Dimension(400,200));

So pack() cannot interfere.

like image 81
Joop Eggen Avatar answered Sep 19 '22 05:09

Joop Eggen


By default for you frame you are using BorderLayout. You can try to have like:

this.add(topPanel, BorderLayout.NORTH);     //Add Top Panel to North
this.add(middlePanel, BorderLayout.SOUTH); //Add Middle Panel to South

rather than at center.

Or you can create an intermediate container panel for these 2 panels, or consider other layout managers like BoxLayout, etc

like image 40
vishal_aim Avatar answered Sep 19 '22 05:09

vishal_aim