Reputation:
I have a JButton which I want to create a new JButton with when it is pressed I have added an ActionListener that looks like this, but it doesn't add another JButton.
public void actionPerformed(ActionEvent e){
Object command = e.getSource();
if(command.equals(play)){
ImageIcon i1 = new ImageIcon("NewGame.png");
width = i1.getIconWidth();
height = i1.getIconWidth();
newGame = new JButton(i1);
newGame.setBorderPainted(false);
newGame.setContentAreaFilled(false);
newGame.setSize(width, height);
newGame.setLocation(600,100);
add(newGame);
System.out.println("It Works");
}
}
How would I make it so that when I press the already existing JButton this one will be added?
Upvotes: 0
Views: 181
Reputation: 5655
if you are not getting exception, then you will need to refresh or repaint the container which will old the new button.
Upvotes: 0
Reputation: 159864
Make sure to revalidate
and repaint
after adding the button
revalidate();
repaint();
From the use of setSize
and setLocation
, it appears you are using a absolute positioning or null
layout. Use a layout manager.
Upvotes: 1