This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to program a "go again" button  RSS feed

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());


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

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

}//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
Posts: 57499
  • 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.
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.
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.repaint();//sometimes required, sometimes not
The City calls upon her steadfast protectors. Now for a tiny ad:
ScroogeXHTML 7.2 - RTF to HTML5 / XHTML converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!