Replacing JPanel with JPanel in JFrame

I have a class that extends JFrame and has a BorderLayout. It has two private instance variables of type JPanel. They are button panels and are called flipButton and trustButtons. When you click on a button, the button bar is replaced with another button bar. That is, if you click the button in flipButton, flipButton will be replaced with trustButtons. I tried to do it like this:

  private class FlipListener implements ActionListener {
     public void actionPerformed (ActionEvent e) {
       remove (flipButton); 
       add (confidenceButtons, BorderLayout.SOUTH);
       validate ();
       ...
     }
   } 
   private class ColorListener implements ActionListener {
     ...
     public void actionPerformed (ActionEvent e) {
       ...
       remove (confidenceButtons); 
       add (flipButton, BorderLayout.SOUTH);
       validate ();
     }
   }

Buttons in flipButton have FlipListeners, and those in which they are sure. Buttons have ColorListeners. When the program starts, clicking on the button will remove the panel, but nothing will be added to replace it. What am I doing wrong?

EDIT

CardLayout turned out to be a simple and easy solution. It turns out that the above code really works; The problem was a typo in another section of my code. > & L .; However, I have always had problems using these methods, and CardLayout, I believe, simplifies it for me. Thank you

+7
source share
3 answers

Use CardLayout as shown here .

Game viewHigh scores view

+6
source

revalidate () + repaint () should be a trick, like here

EDIT:

feel that you have problems with this, examples for here and here and again an example using trashgod , feel free to build your question again based on code

another way is to look at a great example added by Andrew Thompson :-) +1

+5
source

try using getContentPane () to call the remove (), add () ect .. methods:

getContentPane().remove(flipButton); getContentPane().add(confidenceButtons,BorderLayout.SOUTH); getContentPane().revalidate(); getContentPane().repaint(); 

Edit: this code below works for me:

 import java.awt.BorderLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Frame extends JFrame { JPanel flipButton =new JPanel(); JPanel confidenceButtons =new JPanel(); public Frame() throws HeadlessException { super(); this.setLayout(new BorderLayout()); JButton b1=new JButton("flip"); b1.addActionListener(new FlipListener()); flipButton.add(b1); JButton b2=new JButton("color"); b2.addActionListener(new ColorListener()); confidenceButtons.add(b2); this.getContentPane().add(flipButton,BorderLayout.SOUTH); this.setSize(250,250); this.pack(); this.setVisible(true); } private class FlipListener implements ActionListener{ public void actionPerformed(ActionEvent e){ remove(flipButton); add(confidenceButtons,BorderLayout.SOUTH); validate(); repaint(); } } private class ColorListener implements ActionListener{ public void actionPerformed(ActionEvent e){ remove(confidenceButtons); add(flipButton,BorderLayout.SOUTH); validate(); repaint(); } } /** * @param args */ public static void main(String[] args) { new Frame(); } } 
+1
source

All Articles