Results 1 to 14 of 14
- 03-27-2011, 10:24 AM #1
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
How to close a form with a panel with buttons
I have a from and in this form a panel with buttons.
When i click on the startbutton i will start my game.
But i want the first frame to close within the startbutton functionality of the panel.
I have made an instance to the startform and tried:
1. form.SetVisibility(false);
2. form.dispose();
See code, but i won't work, what am i missing here?
Java Code:package view; import javax.swing.*; import java.awt.event.*; import controller.StartHandler; public class StartForm extends JFrame { public static void main( String args[] ) { JFrame frame = new StartForm(); frame.setSize(800, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Sokoban"); frame.setContentPane(new Paneel()); frame.setVisible(true); } } class Paneel extends JPanel { //variabelen van componenten private JTextField naamTextField; private JLabel naamLabel, scoreLabel, moeilijkheidsgraadLabel, huidigeSpelerLabel, getSpelerLabel; private JButton startButton, stopButton, saveNameButton; private JRadioButton makkelijkRadioButton, gemiddeldRadioButton, moeilijkRadioButton; private JTextArea scoreLijst; private ButtonGroup buttonGroup, radioButtonGroup; /* * @type: constructor */ public Paneel() { setLayout(null); // maak labels naamLabel = new JLabel("Naam:"); scoreLabel = new JLabel("Score:"); moeilijkheidsgraadLabel = new JLabel("Moeilijkheidsgraad:"); huidigeSpelerLabel = new JLabel("Welkom "); getSpelerLabel = new JLabel("", SwingConstants.LEFT); // maak naamTextField naamTextField = new JTextField(); naamTextField.setHorizontalAlignment(JTextField.LEFT); // maak scoreLijst scoreLijst = new JTextArea(); // maak knoppen buttonGroup = new ButtonGroup(); startButton = new JButton("Start"); startButton.addActionListener(new StartButtonHandler()); stopButton = new JButton("Stop"); stopButton.addActionListener(new StopButtonHandler()); saveNameButton = new JButton("Bewaar"); saveNameButton.addActionListener( new SaveButtonHandler()); // maak radiobuttons radioButtonGroup = new ButtonGroup(); makkelijkRadioButton = new JRadioButton("Makkelijk", true); //standaard gemiddeldRadioButton = new JRadioButton("Gemiddeld" ); moeilijkRadioButton = new JRadioButton("Moeilijk"); /* * zet posities van de objecten * #int x, int y, int width, int height */ naamLabel.setBounds(40, 40, 50, 20); naamTextField.setBounds(80, 40, 100, 20); saveNameButton.setBounds(200, 40, 80, 20); scoreLabel.setBounds(40, 80, 50, 20); scoreLijst.setBounds(40, 120, 250, 200); moeilijkheidsgraadLabel.setBounds(500, 100, 150, 20); makkelijkRadioButton.setBounds(500, 140, 100, 20); gemiddeldRadioButton.setBounds(500, 160, 100, 20); moeilijkRadioButton.setBounds(500, 180, 100, 20); huidigeSpelerLabel.setBounds(500, 40, 150, 20); startButton.setBounds(500, 270, 100, 20); stopButton.setBounds(500, 300, 100, 20); add(naamTextField); add(naamLabel); add(scoreLabel); add(scoreLijst); add(huidigeSpelerLabel); buttonGroup.add(startButton); buttonGroup.add(stopButton); add(startButton); add(stopButton); add(saveNameButton); radioButtonGroup.add(makkelijkRadioButton); radioButtonGroup.add(gemiddeldRadioButton); radioButtonGroup.add(moeilijkRadioButton); add(moeilijkheidsgraadLabel); add(makkelijkRadioButton); add(gemiddeldRadioButton); add(moeilijkRadioButton); } /* * @type constructor */ class SaveButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { StartHandler save = new StartHandler(); if(save.checkNaam(naamTextField.getText())) { add(getSpelerLabel); getSpelerLabel.setText(naamTextField.getText()); getSpelerLabel.setBounds(560, 40, 150, 20); } } } /* * @type: constructor */ class StartButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { StartForm frame = new StartForm(); frame.setVisible(false); frame.dispose(); //Controle moeilijkheidsgraad int i = (makkelijkRadioButton.isSelected() ? 0 : (gemiddeldRadioButton.isSelected() ? 1 : 2)); StartHandler start = new StartHandler(); start.startGame(naamTextField.getText(), i); } } /* * @type: constructor */ class StopButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e){ System.exit(0); } } }
- 03-27-2011, 10:29 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
frameName.dispose(); should work.
- 03-27-2011, 10:33 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
frame.dispose();
- 03-27-2011, 10:44 AM #4
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
I have used the startbutton (see code) to test this, but it does nothing.
Its a custom made form, and it works fine with the other functions
Java Code:package view; import javax.swing.*; import java.awt.event.*; import controller.StartHandler; public class StartForm extends JFrame { private String naam; public StartForm(String naam) { this.naam = naam; } public static void main( String args[] ) { StartForm frame = new StartForm("test"); frame.setSize(800, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Sokoban"); frame.setContentPane(new Paneel()); frame.setVisible(true); } public String getNaam(){ return this.naam; } } class Paneel extends JPanel { //variabelen van componenten private JTextField naamTextField; private JLabel naamLabel, scoreLabel, moeilijkheidsgraadLabel, huidigeSpelerLabel, getSpelerLabel; private JButton startButton, stopButton, saveNameButton; private JRadioButton makkelijkRadioButton, gemiddeldRadioButton, moeilijkRadioButton; private JTextArea scoreLijst; private ButtonGroup buttonGroup, radioButtonGroup; /* * @type: constructor */ public Paneel() { setLayout(null); // maak labels naamLabel = new JLabel("Naam:"); scoreLabel = new JLabel("Score:"); moeilijkheidsgraadLabel = new JLabel("Moeilijkheidsgraad:"); huidigeSpelerLabel = new JLabel("Welkom "); getSpelerLabel = new JLabel("", SwingConstants.LEFT); // maak naamTextField naamTextField = new JTextField(); naamTextField.setHorizontalAlignment(JTextField.LEFT); // maak scoreLijst scoreLijst = new JTextArea(); // maak knoppen buttonGroup = new ButtonGroup(); startButton = new JButton("Start"); startButton.addActionListener(new StartButtonHandler()); stopButton = new JButton("Stop"); stopButton.addActionListener(new StopButtonHandler()); saveNameButton = new JButton("Bewaar"); saveNameButton.addActionListener( new SaveButtonHandler()); // maak radiobuttons radioButtonGroup = new ButtonGroup(); makkelijkRadioButton = new JRadioButton("Makkelijk", true); //standaard gemiddeldRadioButton = new JRadioButton("Gemiddeld" ); moeilijkRadioButton = new JRadioButton("Moeilijk"); /* * zet posities van de objecten * #int x, int y, int width, int height */ naamLabel.setBounds(40, 40, 50, 20); naamTextField.setBounds(80, 40, 100, 20); saveNameButton.setBounds(200, 40, 80, 20); scoreLabel.setBounds(40, 80, 50, 20); scoreLijst.setBounds(40, 120, 250, 200); moeilijkheidsgraadLabel.setBounds(500, 100, 150, 20); makkelijkRadioButton.setBounds(500, 140, 100, 20); gemiddeldRadioButton.setBounds(500, 160, 100, 20); moeilijkRadioButton.setBounds(500, 180, 100, 20); huidigeSpelerLabel.setBounds(500, 40, 150, 20); startButton.setBounds(500, 270, 100, 20); stopButton.setBounds(500, 300, 100, 20); add(naamTextField); add(naamLabel); add(scoreLabel); add(scoreLijst); add(huidigeSpelerLabel); buttonGroup.add(startButton); buttonGroup.add(stopButton); add(startButton); add(stopButton); add(saveNameButton); radioButtonGroup.add(makkelijkRadioButton); radioButtonGroup.add(gemiddeldRadioButton); radioButtonGroup.add(moeilijkRadioButton); add(moeilijkheidsgraadLabel); add(makkelijkRadioButton); add(gemiddeldRadioButton); add(moeilijkRadioButton); } /* * @type constructor */ class SaveButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { StartHandler save = new StartHandler(); if(save.checkNaam(naamTextField.getText())) { add(getSpelerLabel); getSpelerLabel.setText(naamTextField.getText()); getSpelerLabel.setBounds(560, 40, 150, 20); } } } /* * @type: constructor */ class StartButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { StartForm frame = new StartForm("test"); frame.dispose(); //Controle moeilijkheidsgraad // int i = (makkelijkRadioButton.isSelected() ? 0 : (gemiddeldRadioButton.isSelected() ? 1 : 2)); // StartHandler start = new StartHandler(); // start.startGame(naamTextField.getText(), i); } } /* * @type: constructor */ class StopButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e){ System.exit(0); } } }
- 03-27-2011, 10:47 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The start button needs to have access to and call dispose on the frame created in main.
- 03-27-2011, 10:52 AM #6
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
i have made a public class for the Start button. But how do i dispose the allready active frame (the frame created in main)?
- 03-27-2011, 10:55 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You may want to supply a constructor for startframe which does all the setup stuff in main and adds the panel to the start frame.
The button listener needs to be able call dispose on the frame object which holds your custom panel. I tend to do GUIs differently so i am trying to think of the exact way to do this.
You can also try to give the panel class a reference to a parent component, in this case the frame. Which the button listener calls dispose on. Then when you create the paneel you can do
Java Code:JPanel p = new Paneel(startframe);
Last edited by sunde887; 03-27-2011 at 11:01 AM.
- 03-27-2011, 11:04 AM #8
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Thanks for your answers.
When i make a constructor i need a var to use it in the main.
Then i get the static context error. And the var i cannot make static because i will get a NullPointer.
- 03-27-2011, 11:14 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It's hard to explain, the best way would be to pass a reference to the panel and listener class, this will require almost no changes to your code.
The listener constructor will set the frame instance variable and the panel will get the frame from the main class, here is a quick example.
some of this is paraphrased and not totally correct but it's meant as an example to illustrate what I mean to you.Java Code:main(String[] args){ StartFrame sf = new StartFrame(); sf.add(new Paneel(/*args*/, sf); } class Paneel{ private JFrame frame; //other instance variables public Paneel(/*arts*/, frame){ this.frame = frame; //other stuff } } public listener implements ActionListener{ private JFrame frame; public listener(frame){ this.frame = frame; } actionPerformed(ActionEvent e){ frame.dispose(); } }
- 03-27-2011, 11:54 AM #10
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Its getting more clear in code, but i'm getting a NullPointer now.
Can you explain the mistake i made
Java Code:package view; import javax.swing.*; import java.awt.event.*; import controller.StartHandler; public class StartForm extends JFrame { public static void main( String args[] ) { StartForm frame = new StartForm(); frame.setSize(800, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Sokoban"); frame.setContentPane(new Paneel(frame)); frame.setVisible(true); } } class Paneel extends JPanel { public Paneel(StartForm frame) { this.frame = frame; setLayout(null); // maak labels naamLabel = new JLabel("Naam:"); scoreLabel = new JLabel("Score:"); moeilijkheidsgraadLabel = new JLabel("Moeilijkheidsgraad:"); huidigeSpelerLabel = new JLabel("Welkom "); getSpelerLabel = new JLabel("", SwingConstants.LEFT); // maak naamTextField naamTextField = new JTextField(); naamTextField.setHorizontalAlignment(JTextField.LEFT); // maak scoreLijst scoreLijst = new JTextArea(); // maak knoppen buttonGroup = new ButtonGroup(); startButton = new JButton("Start"); startButton.addActionListener(new StartButtonHandler()); stopButton = new JButton("Stop"); stopButton.addActionListener(new StopButtonHandler()); saveNameButton = new JButton("Bewaar"); saveNameButton.addActionListener( new SaveButtonHandler()); // maak radiobuttons radioButtonGroup = new ButtonGroup(); makkelijkRadioButton = new JRadioButton("Makkelijk", true); //standaard gemiddeldRadioButton = new JRadioButton("Gemiddeld" ); moeilijkRadioButton = new JRadioButton("Moeilijk"); /* * zet posities van de objecten * #int x, int y, int width, int height */ naamLabel.setBounds(40, 40, 50, 20); naamTextField.setBounds(80, 40, 100, 20); saveNameButton.setBounds(200, 40, 80, 20); scoreLabel.setBounds(40, 80, 50, 20); scoreLijst.setBounds(40, 120, 250, 200); moeilijkheidsgraadLabel.setBounds(500, 100, 150, 20); makkelijkRadioButton.setBounds(500, 140, 100, 20); gemiddeldRadioButton.setBounds(500, 160, 100, 20); moeilijkRadioButton.setBounds(500, 180, 100, 20); huidigeSpelerLabel.setBounds(500, 40, 150, 20); startButton.setBounds(500, 270, 100, 20); stopButton.setBounds(500, 300, 100, 20); add(naamTextField); add(naamLabel); add(scoreLabel); add(scoreLijst); add(huidigeSpelerLabel); buttonGroup.add(startButton); buttonGroup.add(stopButton); add(startButton); add(stopButton); add(saveNameButton); radioButtonGroup.add(makkelijkRadioButton); radioButtonGroup.add(gemiddeldRadioButton); radioButtonGroup.add(moeilijkRadioButton); add(moeilijkheidsgraadLabel); add(makkelijkRadioButton); add(gemiddeldRadioButton); add(moeilijkRadioButton); } /* * @type constructor */ class SaveButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { StartHandler save = new StartHandler(); if(save.checkNaam(naamTextField.getText())) { add(getSpelerLabel); getSpelerLabel.setText(naamTextField.getText()); getSpelerLabel.setBounds(560, 40, 150, 20); } } } /* * @type: constructor */ public class StartButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ private StartForm frame; public void listener(StartForm frame) { this.frame=frame; } public void actionPerformed(ActionEvent e){ frame.dispose(); // StartHandler start = new StartHandler(); // start.startGame(naamTextField.getText(), i); } } /* * @type: constructor */ class StopButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e){ System.exit(0); } } }Last edited by aborgeld; 03-27-2011 at 01:58 PM.
- 03-27-2011, 01:07 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The method listener in startbuttonlistener should be a constructor. When you add the action listener to the button you want to provide the frame.
You are essentially passing the frame to each class to use.Java Code:button.addActionListener(new StartButtonListener(frame));
There may be other ways to do this, however; I like this approach.
- 03-27-2011, 01:52 PM #12
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Thanks very much for helping me again and thinking with me through this.
I will power up your rep ;-)
I did use the frame var everywhere. Then it becomes null and you'l get the Nullpointer
Now i only pass it, and with the constuctor var on the panel i can close the frame. It works, i'm gladdddddddddddd.........................:-)
If your interested, here is the code once more, it works. I'm glad.
Java Code:package view; import javax.swing.*; import java.awt.event.*; import controller.StartHandler; public class StartForm extends JFrame { public static void main( String args[] ) { StartForm frame = new StartForm(); frame.setSize(800, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Sokoban"); frame.setContentPane(new Paneel(frame)); frame.setVisible(true); } } class Paneel extends JPanel { public Paneel(StartForm startForm) { this.startForm = startForm; setLayout(null); // maak labels naamLabel = new JLabel("Naam:"); scoreLabel = new JLabel("Score:"); moeilijkheidsgraadLabel = new JLabel("Moeilijkheidsgraad:"); huidigeSpelerLabel = new JLabel("Welkom "); getSpelerLabel = new JLabel("", SwingConstants.LEFT); // maak naamTextField naamTextField = new JTextField(); naamTextField.setHorizontalAlignment(JTextField.LEFT); // maak scoreLijst scoreLijst = new JTextArea(); // maak knoppen buttonGroup = new ButtonGroup(); startButton = new JButton("Start"); startButton.addActionListener(new StartButtonHandler()); stopButton = new JButton("Stop"); stopButton.addActionListener(new StopButtonHandler()); saveNameButton = new JButton("Bewaar"); saveNameButton.addActionListener( new SaveButtonHandler()); // maak radiobuttons radioButtonGroup = new ButtonGroup(); makkelijkRadioButton = new JRadioButton("Makkelijk", true); //standaard gemiddeldRadioButton = new JRadioButton("Gemiddeld" ); moeilijkRadioButton = new JRadioButton("Moeilijk"); /* * zet posities van de objecten * #int x, int y, int width, int height */ naamLabel.setBounds(40, 40, 50, 20); naamTextField.setBounds(80, 40, 100, 20); saveNameButton.setBounds(200, 40, 80, 20); scoreLabel.setBounds(40, 80, 50, 20); scoreLijst.setBounds(40, 120, 250, 200); moeilijkheidsgraadLabel.setBounds(500, 100, 150, 20); makkelijkRadioButton.setBounds(500, 140, 100, 20); gemiddeldRadioButton.setBounds(500, 160, 100, 20); moeilijkRadioButton.setBounds(500, 180, 100, 20); huidigeSpelerLabel.setBounds(500, 40, 150, 20); startButton.setBounds(500, 270, 100, 20); stopButton.setBounds(500, 300, 100, 20); add(naamTextField); add(naamLabel); add(scoreLabel); add(scoreLijst); add(huidigeSpelerLabel); buttonGroup.add(startButton); buttonGroup.add(stopButton); add(startButton); add(stopButton); add(saveNameButton); radioButtonGroup.add(makkelijkRadioButton); radioButtonGroup.add(gemiddeldRadioButton); radioButtonGroup.add(moeilijkRadioButton); add(moeilijkheidsgraadLabel); add(makkelijkRadioButton); add(gemiddeldRadioButton); add(moeilijkRadioButton); } /* * @type constructor */ class SaveButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { StartHandler save = new StartHandler(); if(save.checkNaam(naamTextField.getText())) { add(getSpelerLabel); getSpelerLabel.setText(naamTextField.getText()); getSpelerLabel.setBounds(560, 40, 150, 20); } } } /* * @type: constructor */ public class StartButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e){ startForm.setVisible(false); // StartHandler start = new StartHandler(); // start.startGame(naamTextField.getText(), i); } } /* * @type: constructor */ class StopButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e){ System.exit(0); } } }Last edited by aborgeld; 03-27-2011 at 01:59 PM.
- 03-27-2011, 01:56 PM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Haha, you are very welcome, glad you solved it. I actually didn't realize the listeners were inner classes. Please mark your thread solved with the thread tools at the top.
- 03-27-2011, 01:57 PM #14
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Similar Threads
-
run actions, submit form and close on click
By marckamga in forum Java ServletReplies: 3Last Post: 09-29-2010, 11:21 AM -
Will the connection.close() and statement.close() ever be called???
By Stephen Douglas in forum New To JavaReplies: 13Last Post: 04-09-2010, 11:15 AM -
looking for a form pane or form group panel for the jpanel to complete tutorial
By nadeemshafi9 in forum AWT / SwingReplies: 1Last Post: 03-22-2010, 09:03 AM -
[SOLVED] How to close the current form when i open a new form?
By tpyq in forum NetBeansReplies: 6Last Post: 11-28-2008, 06:55 AM -
Spacing buttons on a panel
By 2o2 in forum New To JavaReplies: 8Last Post: 10-20-2008, 10:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks