Reputation: 15
When I press button labeled "one", my popup window freezes up, and I think it's because I'm trying to put a button with an action listener into the action listener of another button. Is that possible?
//code...
one = new JButton("Customize Race");
one.setBounds(30,200,200,75);
one.addActionListener(new ActionListener(){
@Override
public void actionPerformed( ActionEvent e ) {
one.setVisible(false);
Boolean pic = true;
String Player1 = "Player1";
while (pic == true)
{
p1 = new JButton(Player1);
p1.setBounds(50, 50, 200, 100);
p1.addActionListener(new ActionListener(){
@Override
public void actionPerformed( ActionEvent e ) {
// code that will pull up menu to
customize string value of Player1
}
});
next1 = new JButton("Next =>");
next1.setBounds(50, 375, 450, 50);
next1.addActionListener(new ActionListener(){
@Override
public void actionPerformed( ActionEvent e ) {
Boolean pic = false;
}
});
panel.add(p1);
panel.add(next1);
}
p1.setVisible(false);
}
});
panel.add(one);
frame.setVisible(true);
Upvotes: 1
Views: 924
Reputation: 324108
Can you put an action listener inside an action listener?
Yes you can create a component in your ActionListener and add an ActionListener to the new component.
That is not the problem.
my popup window freezes up,
while (pic == true)
The problem is you have a while loop that continues to execute.
Your ActionListener is continually creating components and adding them to the frame and the loop never ends.
Get rid of the while loop!!!
Also, when you add components to a visible frame the code should be:
panel.add(....);
panel.revalidate();
panel.repaint();
The revalidate() invokes the layout manager so the component can be positioned properly and the repaint() just makes sure all the components are repainted.
Upvotes: 1