¿Navegar entre JPanels?

I have a problem when I click a button to change the content of right_pan(JPanel) nothing happens; I have 3 JPanel: pan_glob therein lies the other two: right_pan and left_pan(are integrated into the main JFrame named "main" see screenshot),and another external JPanel named cree_client (contains text box).

enter image description here

I'd like when I click a button, the content of cree_client(JPanel) is loaded into right_pan(JPanel).

I tried this but it does not work :

  private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {

  cree_client cc=new cree_client();
  this.right_pan.removeAll();
  this.right_pan.validate();
  this.right_pan.add(cc);
  this.right_pan.revalidate();

}

ANY helps ?

preguntado el 30 de enero de 12 a las 19:01

it is a name generated by netbeans is not a problem, this name is abstract for me, does not cause worries.. -

2 Respuestas

First rule of using any UI toolkit: don't remove/add children to control visibility. It never works very well in almost any UI toolkit. It requires more work on the toolkit to properly handle it, and that goes double for Swing. Instead use either visibility, or I bet what you should use is a CardLayout. A panel with a CardLayout will allow you to flip between several screens:

public class StackPanel extends JPanel {

    CardLayout layout = new CardLayout();
    public StackPanel() {
        super();
        setLayout( layout );
    }

    public addStack( String name, JComponent child ) {
        add( name, child );
    }

    public void showStack( String name ) {
        layout.show( this, name );
    }
}


CardLayout layout = new CardLayout();
StackPanel stack = new StackPanel();
stack.addStack( "panelOne", panelOne );
stack.addStack( "panelTwo", panelTwo );
...
JButton someButton = new JButton( new AbstractAction("Next" ) {
    public void actionPerformed(ActionEvent evt ) {
       stack.showStack( "panelTwo" );
    }
} );

If you don't want to flip between two or more screens, then you can use setVisible() to hide and show the different screens just as well.

Respondido el 19 de junio de 13 a las 19:06

Thank you for reply, I am currently in netbeans (using The designer), it integrates automatically "GroupLayout" as a default layout,coding an application like this such is very long and tedious so I use the designer of netbeans. any solution ? - Marwen Trabelsi

You can use setVisible() on each panel if they are inside a GroupLayout and it will hide/show those appropriately just like CardLayout. The difference is CardLayout will only display one child at a time where GroupLayout would allow you to display multiple. It's up to your visual layout, and I suspect using CardLayout would be easier for your user, UX, etc, but it's up to you. - chubbsondubs

Hi, First of all I thank you for the support, setVisible () works fine, same for CardLayout, but I use setVisible().... ^^ - Marwen Trabelsi

First of all, have you actually checked that the code in your action listener is being performed? I'm not that familiar with JMenus but I think that method should be more like

public void ActionPerformed(ActionEvent evt){...}

and the action listener should be added to JMenuItem itself - a quick System.out.println() statement in the body of the action performed method should help you determine whether the code to change the JPanel is executing or not!

Secondly, if your actionPerformed method is running you may want to try running the Component.repaint() method after you revalidate. Also, it may be a thought to try 'revalidating' and 'repainting' the main JPanel that houses the one you are changing (in your case the parent of right_pan)!

Respondido el 06 de enero de 13 a las 14:01

Thank you for reply, yes I have already tested and it works, Component.repaint() after revalidate it does not work... thank you anyway - Marwen Trabelsi

Did you try to repaint and revalidate the JPanel in which you added right_pan? - Andy

yes,but it does not work(see the first question),I already wrote the code ^^ Thanks.. - Marwen Trabelsi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.