2

I want to remove JButton when user click JButton.

I know that I should use remove method, but it did not work.

How can I do this?

Here is my code:

class Game implements ActionListener {

JFrame gameFrame;
JButton tmpButton;
JLabel tmpLabel1, tmpLabel2, tmpLabel3, tmpLabel4;

public void actionPerformed(ActionEvent e) {
    gameFrame.remove(tmpLabel1);
    gameFrame.getContentPane().validate();
    return;
}

Game(String title) {
    gameFrame = new JFrame(title);
    gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gameFrame.setBounds(100, 100, 300, 500);
    gameFrame.setResizable(false);
    gameFrame.getContentPane().setLayout(null);

    tmpLabel4 = new JLabel(new ImageIcon("./images/bomber.jpg"));
    tmpLabel4.setSize(200, 200);
    tmpLabel4.setLocation(50, 100);
    tmpButton = new JButton("Play");
    tmpButton.setSize(100, 50);
    tmpButton.setLocation(100, 350);
    tmpButton.addActionListener(this);

    gameFrame.getContentPane().add(tmpLabel4);
    gameFrame.getContentPane().add(tmpButton);
    gameFrame.setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Won Seok Lee
  • 51
  • 1
  • 1
  • 4
  • You need to first call `revalidate()/repaint()` on the already visible container, when the `JButton` is removed. Moreover, stop using Absolute Positioning, instead use a genuine Layout Manager. – nIcE cOw Dec 21 '14 at 05:47
  • @GáborBakos Are you aware that `JFrame#remove` can delegate to the call to the `contentPane`, just like `JFrame#add`? Not saying it's always a good idea ;) – MadProgrammer Dec 21 '14 at 05:49
  • 2
    Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Dec 21 '14 at 06:14
  • *"I know that I should use remove method.."* It would probably be **better** to use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Dec 21 '14 at 06:21
  • Quote from Java Doc: " Lightweight and heavyweight components may be mixed in a single component hierarchy. However, for correct operating of such a mixed hierarchy of components, the whole hierarchy must be valid. When the hierarchy gets invalidated, like after changing the bounds of components, or adding/removing components to/from containers, the whole hierarchy must be validated afterwards by means of the Container.validate() method invoked on the top-most invalid container of the hierarchy. " http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html – Lalith J. Dec 21 '14 at 06:57

4 Answers4

6

If hiding the button instead of removing works for your code then you can use:

public void actionPerformed(ActionEvent event){
   tmpButton.setVisible(false);
 }

for the button.But the button is just hidden not removed.

hermit
  • 1,048
  • 1
  • 6
  • 16
3

The simplest solution might be to...

For example...

Container parent = buttonThatWasClicked.getParent();
parent.remove(buttonThatWasClicked);
parent.revaidate();
parent.repaint();

As some ideas...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

First of all in your actionPerformed method you need to check that the button is clicked or not. And if the button is clicked, remove it. Here's how :

if(e.getSource() == tmpButton){
   gameFrame.getContentPane().remove(tmpButton);
}

add this to your actionPerformed Method

varun
  • 1,473
  • 1
  • 9
  • 15
0

don't add your button to jframe but add each component you want!

public void actionPerformed(ActionEvent event)
{
   //gameFrame.getContentPane().add(tmpButton); -=> "Commented Area"
   gameFrame.getContentPane().validate();
}

or hide your button like this

public void actionPerformed(ActionEvent event)
{
   tmpButton.setVisible(false);
}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27