0

I want to increase and decrease the size of JButton on its focus gain and lost event. Simultaneously I want to change the JLabel with the text of focussed JButton.

If I'm not changing the JLabel text, I'm able to change the size of buttons, but when I'm changing the label simultaneously the size of JButton does not change.

Here is the code:

public class Main extends JFrame implements FocusListener {

    JButton b1, b2;
    JLabel lbl;
    private static final long serialVersionUID = 1L;

    public Main() {

        setSize(600, 600);//Size of JFrame
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);//Sets if its visible.

        JPanel panel = new JPanel();

        b1 = new JButton("Start");//The JButton name.
        b1.setRequestFocusEnabled(false);
        b1.addFocusListener(this);
        panel.add(b1);

        b2 = new JButton("End");//The JButton name.
        b2.setRequestFocusEnabled(false);
        b2.addFocusListener(this);

        panel.add(b2);

        add(panel, BorderLayout.CENTER);
        lbl = new JLabel("       ");
        add(lbl, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        new Main();//Reads method main()
    }

    /*
    * What the button does.
    */
    @Override
    public void focusLost(FocusEvent ae) {

        if (ae.getSource() == b2) {
            b2.setSize(55, 26);
        } else if (ae.getSource() == b1) {
            b1.setSize(55, 26);
        }
    }

    @Override
    public void focusGained(FocusEvent ae) {

        if (ae.getSource() == b2) {
            lbl.setText("End");
            b2.setSize(55, 40);
        } else if (ae.getSource() == b1) {
            lbl.setText("Start");
            b1.setSize(55, 40);
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mayank Singh
  • 109
  • 1
  • 11

2 Answers2

3

The problem is that you are mixing absolute-layout (or null-layout) with an implicit LayoutManager (JPanel comes with a FlowLayout by default).

When you call setText on a JLabel (and that text changes), it automatically calls revalidate which will eventually trigger the LayoutManager to lay out the components.

Either use a LayoutManager, optionally set some constraints and pref/min/max size (but the latter is not recommended) (and you never call setLocation/setSize/setBounds), or use null-layout and set yourself the location and size of the components (and in this case you must call yourself setSize/setLocation/setBounds).

Seriously consider reading the LayoutManager tutorial. There is a dedicated chapter on "Doing Without a Layout Manager (Absolute Positioning)".

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0

I think this is what you are trying to achieve.

public class Main extends JFrame implements FocusListener {

JButton b1, b2;
JLabel lbl;
private static final long serialVersionUID = 1L;

public Main() {

    setSize(600, 600);// Size of JFrame
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);// Sets if its visible.

    JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setSize(this.getSize());
    b1 = new JButton("Start");// The JButton name.
    b1.setRequestFocusEnabled(false);
    b1.addFocusListener(this);
    b1.setLocation(10, 12);
    panel.add(b1);

    b2 = new JButton("End");// The JButton name.
    b2.setRequestFocusEnabled(false);
    b2.addFocusListener(this);
    b2.setLocation(70, 12);
    panel.add(b2);

    add(panel, BorderLayout.CENTER);
    lbl = new JLabel("       ");
    add(lbl, BorderLayout.SOUTH);
}

public static void main(String[] args) {
    new Main();// Reads method main()
}

/*
 * What the button does.
 */
@Override
public void focusLost(FocusEvent ae) {

    if (ae.getSource() == b2) {
        b2.setSize(55, 26);
    } else if (ae.getSource() == b1) {
        b1.setSize(55, 26);
    }

}

@Override
public void focusGained(FocusEvent ae) {

    if (ae.getSource() == b2) {
        lbl.setText("End");
        b2.setSize(55, 40);
    } else if (ae.getSource() == b1) {
        lbl.setText("Start");
        b1.setSize(55, 40);
    }

}
}
Sunil luitel
  • 930
  • 1
  • 8
  • 16