Question

I can only see the JPanel added at last. Previous ones seem to be replaced by the last one. Why is this? Here's my code. Thanks in advance.

...
class GUIController {



    BaseFrame bf = new BaseFrame(); //is a JFrame




    public void showFrame(final String frameName, final JPanel... panels){

        EventQueue.invokeLater(new Runnable() {
                public void run() {
                     try {
                            Class c;
                            Constructor ctr;
                            c = Class.forName("com.mytunes.client.views.frames." + frameName + "Frame");

                            ctr = c.getConstructor();

                            bf.removeAll();

                            bf = (BaseFrame) ctr.newInstance(); // no issue with getting the JFrame dynamically created!


                            for(JPanel panel:panels){

                                switch(i){
                                    case 1: bf.getContentPane().add(panel,BorderLayout.PAGE_START);

                                        break;

                                         case 2:
                                        bf.getContentPane().add(panel,BorderLayout.CENTER);
                                       break;

                                         case 3:
                                        bf.getContentPane().add(panel,BorderLayout.PAGE_END);
                                        break;

                                }

                            }
                            bf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                            bf.pack();
                            bf.setVisible(true);

                        } catch (InstantiationException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalArgumentException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InvocationTargetException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NoSuchMethodException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (SecurityException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        }
           }

       });



    }


    public void showEnterCard(String frame){

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();

        showFrame(frame, p1,p2, p3);
    }

}

class Main{
    public static void main(String args[]){
        GUIController c = new GUIController();

        c.showEnterCard("FirstFrame");
    }
}

EDIT: (solution)

Loop changed as follows:

for(int i = 1; i <= panels.length; i++){
     switch(i){
        case 1: 
              bf.getContentPane().add(panels[0],BorderLayout.PAGE_START);
        break;
         ...
     }
}
Was it helpful?

Solution

you never change i, it is always one, so case 1: in the switch is always true, and the JPanel is always added to the same place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top