Results 1 to 9 of 9
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default How to remove a JPanel from a JFrame after a button click, and add a new one ?

    I have a class MainFrame, which holds a JPanel langPanel.
    langPanel has two buttons: English and Turkish. Once you click English, I want the langPanel to be removed, and the secondPanel to be shown in my MainFrame.

    Here is my code:

    Java Code:
    public class MainFrame extends JFrame
    {
    	private MainMenuBar menuBar = new MainMenuBar();
    	LanguagePanel langPanel;
    	
    	//MainFrame constructor
    	MainFrame()
    	{
    		// add a LanguagePanel and pass this as the frame
    		langPanel = new LanguagePanel(this);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setResizable(false);
    		this.setSize(800,400);
    		this.setLocationRelativeTo(null);
    		//this.setUndecorated(true);
    		
    		this.setJMenuBar(menuBar);
    		
    		this.add(langPanel);
    		
    		this.setVisible(true);
    	} // end MainFrame constructor
    } // end class MainFrame
    Java Code:
    public class LanguagePanel extends JPanel
    {
    	private JButton turkishButton = new JButton("Turkish");
    	private JButton englishButton = new JButton("English");
    	private MainFrame mainFrame;
    	
    	LanguagePanel(MainFrame frame)
    	{
    		this.mainFrame = frame;
    		englishButton.addActionListener(new EnglishButtonListener());
    		this.add(englishButton);
    		this.add(turkishButton);
    	}
    	
    	class EnglishButtonListener implements ActionListener
    	{
    
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			mainFrame.remove(mainFrame.langPanel);
    			mainFrame.validate();
    		}
    		
    	}
    }
    Nothing happens when I click button: English

    How to remove a JPanel from a JFrame after a button click, and add a new one ?-01.png

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    I mean, everything freezes :)

    Sorry.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    I added the line:

    mainFrame.remove(mainFrame.langPanel);
    mainFrame.add(new SecondPanel());
    mainFrame.validate();

    and its ok now..

    Thanks..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    Don't forget to call repaint() after validate (or better perhaps -- revalidate() on the contentPane which is a JPanel).

    Better still is to simply use a CardLayout.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    But I do not want the English and Turkish buttons to remain on the page.

    Shall I still use a CardLayout ?

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    What is the disadvantage of doing it this way ?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    Quote Originally Posted by fatabass View Post
    But I do not want the English and Turkish buttons to remain on the page.

    Shall I still use a CardLayout ?
    Yes.

    What is the disadvantage of doing it this way ?
    For one, it scales better. In the future you may be wanting to have more language panels or other swapping, and CardLayout is easier and cleaner to do than swapping yourself. Also the CardLayout will automatically set the preferredSize of the card-holder container to that of the largest card.

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    Thanks.

    I will stick with this for now, but I will look at the Card Layout as well.

    Thank you.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,834
    Rep Power
    8

    Default Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?

    Yep, judging from your other post(s), CardLayout is the way to go. It's much easier than your approach, and it will take less time to learn it than you've already spent waiting on replies about your current approach.

    How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How do i remove images from a Jpanel?
    By rajkobie in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 02:44 PM
  2. JPanel add/remove in a JFrame
    By Aldane in forum AWT / Swing
    Replies: 2
    Last Post: 03-28-2011, 07:28 PM
  3. Button click GUI question
    By ZambonieDrivor in forum New To Java
    Replies: 2
    Last Post: 11-29-2010, 07:48 AM
  4. How can I display on Button click?
    By ntagrafix in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 12:05 AM
  5. deselecting a button after the click.
    By ramsrocker in forum New To Java
    Replies: 10
    Last Post: 02-15-2009, 06:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •