I am trying to programmatically change my layout when something is clicked. However, when one layout is visible, and I attempt to change it (via an ActionListener), both layouts are present, instead of only the new, desired one.
borderLayout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setBorderLayout();
}
});
flowLayout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
settingFlowLayout();
}
});
Here are the methods:
private void setBorderLayout() {
getContentPane().setLayout(null);
setLayout(new BorderLayout(3, 3));
b1 = new JButton("NORTH");
add(b1, BorderLayout.NORTH);
b2 = new JButton("SOUTH");
add(b2, BorderLayout.SOUTH);
b3 = new JButton("CENTER");
add(b3, BorderLayout.CENTER);
b4 = new JButton("EAST");
add(b4, BorderLayout.EAST);
b5 = new JButton("WEST");
add(b5, BorderLayout.WEST);
setVisible(true);
}
// Setting Flow Layout
private void settingFlowLayout() {
getContentPane().setLayout(null);
setLayout(new FlowLayout());
btn1 = new JButton("Button 1");
add(btn1);
btn2 = new JButton("This is Button 2");
add(btn2);
btn3 = new JButton("3");
add(btn3);
btn4 = new JButton("Another Button 4");
add(btn4);
btn5 = new JButton("Button 5");
add(btn5);
btn6 = new JButton("One More Button 6");
add(btn6);
setVisible(true);
}
What can I do so that I can only see one layout instead of both?