1 Attachment(s)
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:
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
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
Attachment 3083
Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?
I mean, everything freezes :)
Sorry.
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..
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.
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 ?
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 ?
Re: How to remove a JPanel from a JFrame after a button click, and add a new one ?
Quote:
Originally Posted by
fatabass
But I do not want the English and Turkish buttons to remain on the page.
Shall I still use a CardLayout ?
Yes.
Quote:
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.
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.
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)