8

I have a class that extends JFrame, and it has a BorderLayout. It has two private instance variables of type JPanel. They represent panels of buttons and are called flipButton and confidenceButtons. When you click on the button, the panel of buttons is replaced by the other panel of buttons. That is, if you click on a button in flipButton, flipButton is replaced by confidenceButtons. 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();
    }
  }

The buttons in flipButton have FlipListeners and the ones in confidenceButtons have ColorListeners. When the program is run, clicking on a button will remove the panel, but nothing is 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 does work; the problem was a typo in another section of my code. >.< However, I've always had trouble using these methods, and CardLayout, I find, simplifies it for me. Thanks.

Shelley
  • 225
  • 2
  • 4
  • 8

3 Answers3

8

Use a CardLayout, as shown here.

Game view High Scores view

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
5

revalidate() + repaint() should be trick, example here

EDIT:

feel that you have got problem with that, examples for that here and here and again example by trashgod, feel free to built your question based on code again

another way is look at excelent example added by Andrew Thompson :-) +1

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I looked at the example but don't quite understand. Why is repaint necessary since I'm not using Graphics? Would I call all three methods on confidenceButtons? The example looks very specific and I do not really know how to adapt it to my code. – Shelley Jun 25 '11 at 06:53
  • +1 See also this [example](http://stackoverflow.com/questions/5812002/removeall-not-removing-at-next-validate/5812981#5812981). – trashgod Jun 25 '11 at 07:05
  • @Shelley example is specific avourt revalidate + validate + repaint, back to questin about repaint(), in most cases GUI works correctly and doesn't required for repaint(); hmmm and there isn't exists any Swing tutorial or detailed describtions about that :-) – mKorbel Jun 25 '11 at 07:09
  • @mKorbel I tried this: all.remove(flipButton); all.add(confidenceButtons,BorderLayout.SOUTH); all.validate(); all.revalidate(); all.repaint(); where all is a JPanel with a borderlayout that contains everything. I know something is wrong... but what should I type to make it right? – Shelley Jun 25 '11 at 07:14
  • @Shelley could you translate your question to the code, looks like as link posted by trashgod is best base for that, then just edit your question – mKorbel Jun 25 '11 at 07:24
  • @mKorbel Could you please give me a snippet of code that I might try with my program? I am lost as to what methods I am supposed to call etc. and where. – Shelley Jun 25 '11 at 08:24
1

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

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

Edit: this code bellow work 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();

    }

}
othman
  • 4,428
  • 6
  • 34
  • 45
  • if you can post your complete JFrame class code i can try fixing it. maybe try using another layout instead of Borederlayout. b/c BorderLayout sometimes causes some strange behavior – othman Jun 25 '11 at 11:20
  • I think it should not matter. I have read that the add, remove etc methods automatically add to the content pane when called alone, so getContentPane() is unnecessary. – Shelley Jun 25 '11 at 11:29
  • I tried using CardLayout and it works perfectly now (CardLayout turned out to be an easy solution). Thank you for your help. – Shelley Jun 25 '11 at 11:32
  • i have edit my answer with a code portion i wrote and tested which works fine for me. is that what you need? – othman Jun 25 '11 at 11:37
  • I just found a typo in my code, of all things, that might have been causing the problems I had with this earlier. Having fixed that, this does work now. Thank you! – Shelley Jun 25 '11 at 11:41