Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to program a "go again" button  RSS feed

 
Ian Dunsirn
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some code where I ask the user to enter in a number of buttons the user wants on a panel. The code will then create a window with the specified number of buttons, along with a button labelled "New Panel" (the button is called goAgain in the code). What I want is when goAgain is pressed, it will start the process all over again, creating a new window with a different number of buttons, closing the original window in the process. Can this be done at all? The code follows:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoopExample implements ActionListener {
int thisValue = 0;
JFrame panel = new JFrame("Example");
JButton[] subtract;
JButton goAgain = new JButton("New Panel");

public LoopExample() {
panel.setLayout(new FlowLayout());
panel.setSize(300,100);

goAgain.addActionListener(this);

thisValue = Integer.parseInt(JOptionPane.showInputDialog(null, "How many buttons?",
"", JOptionPane.QUESTION_MESSAGE));

subtract = new JButton[thisValue];
for (int i = 0; i < thisValue; i++) {
subtract[i] = new JButton("Button " + (i + 1));
subtract[i].addActionListener(this);
panel.add(subtract[i]);
}//end for

panel.add(goAgain);
panel.setVisible(true);
}//end constructor for LoopExample

public static void main(String args[]) {
LoopExample driver = new LoopExample();
}//end method main

public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == goAgain)
JOptionPane.showMessageDialog(null, "New panel code goes here.",
"Confusion reigns", JOptionPane.PLAIN_MESSAGE);
}//end actionPerformed
}//end class LoopExample
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, can it be done? Have you compiled your code? Did it run? What happened?
And welcome to the Ranch.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks like you're adding eveything directly to the frame.

- instead of this, create a separate panel for your 'subtract' buttons
- leave the frame's layout at the default borderlayout
- the subtractButtonPanel will need to be an instance variable, so it can be
'seen' by the actionListener
- the subtractButtonPanel will be added to the frame's borderlayout.NORTH
- the goAgain button will be added to the frame's borderlayout.SOUTH
(goAgain button may need to be in a separate panel - to take up the space)

now put all of the code to ask for number of buttons, and add them to subtractButtonPanel
in a separate method, and call this method from goAgain button's actionperformed.
note:
the first line of this method will be
subtractButtonPanel.removeAll();//get rid if any existing buttons
and the last line (possibly 2) will be
subtractButtonPanel.revalidate();
subtractButtonPanel.repaint();//sometimes required, sometimes not
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!