4 Replies - 4641 Views - Last Post: 06 September 2010 - 06:20 PM Rate Topic: -----

#1 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 273
  • Joined: 02-February 09

JPanel won't close

Posted 06 September 2010 - 05:05 PM

Hi,

I have got this JFrame which contain two JPanels directly on top of each other. The purpose is that when the user clicks the startButton on the top panel, it's going to disappear and the second panel is going to be displayed.

Bu whenever I click the startButton, all thats happening is that the button just freezes, and the JPanel isn't removed. I guess the whole app is freezing...

public class GameController implements ActionListener {

	private JFrame frame;
	private JPanel startPanel, ballPanel;
	private JButton startButton;

	public GameController(GameGUI gameGUI) {

		this.frame = gameGUI.getFrame();
		this.startPanel = gameGUI.getStartPanel();
		this.ballPanel = gameGUI.getBallPanel();
		this.startButton = gameGUI.getStartButton();

		startButton.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if(e.getSource().equals(startButton)) {

			//frame.remove(startButton);
			frame.remove(startPanel);
		}
	}



}



Is This A Good Question/Topic? 0
  • +

Replies To: JPanel won't close

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: JPanel won't close

Posted 06 September 2010 - 05:32 PM

Have a look at CardLayout. It already does the work for you. Just set the main JPanel's layout to CardLayout, and use the ActionListener to interact with the CardLayout to flip to the next JPanel.
Was This Post Helpful? 1
  • +
  • -

#3 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 273
  • Joined: 02-February 09

Re: JPanel won't close

Posted 06 September 2010 - 05:52 PM

ah! thanks! :)
Was This Post Helpful? 0
  • +
  • -

#4 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 273
  • Joined: 02-February 09

Re: JPanel won't close

Posted 06 September 2010 - 06:16 PM

I'm having some trouble using JButtons to change cards though.. have you got any ideas? :)

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: JPanel won't close

Posted 06 September 2010 - 06:20 PM

The Sun Tutorial comes with example code illustrating this concept with a JComboBox. They key is that you add the Cards/JPanels using a String as the constraint/identifying key, and you use that same key to show() the other JPanel(s).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1