user2598911
user2598911

Reputation: 379

Adding jpanels created from the same class to a cardlayout frame

I have a class that reads an excelfile and creates frame with a graph inside a jpanel. I invoke this class through an actionlistener in a jmenuitem. Then I have another jmenuitem that invokes the same class that opens the same file, but reads a different excel sheet, and gives a different graph (thats the only string that changes in the class). The jmenubar that has these jmenitems belong to a jframe from which the program starts. I would like to know if it is possible every time I click the jmenuitems that create the graphs to be added in a new jframe cardlayout, so that I can roll on them. Thanks in advance

This is the code I am currently using to open a graph in a jframe when the jmenuitem is clicked:

public class startup extends JFrame  {   // creates a jframe with some stuff and the jmenubar

public void menu() {
...


                    menuItem.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event2) {

                        new Thread(new Runnable() {
                                @Override
                                public void run() {
                          new ReadExcel();
                                    ReadExcel.excel(".xls", 0);  // this jmenuitem invokes the class to read the excelfile sheet 0
graphgen.main(null);
                                }
                            }).start();
                        }
                    });

                    subsubmenu1.add(menuItem);


menuItem.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event2) {

                        new Thread(new Runnable() {
                                @Override
                                public void run() {
                          new ReadExcel();
                                    ReadExcel.excel(".xls", 1);  // this jmenuitem invokes the class to read the excelfile sheet 1

graphgen.main(null);
                                }
                            }).start();
                        }
                    });

                    subsubmenu1.add(menuItem);

....
}


          public static void main(String[] args)
            {


                GUIquery frame = new GUIquery();
                p.add(graphComponent, BorderLayout.CENTER);
                frame.setLayout(new BorderLayout());
                frame.add(p, BorderLayout.CENTER);
                frame.setJMenuBar(GUIquery.createMenuBar());
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(true);
                frame.setSize(1600, 1200);
                frame.setVisible(true);


            }



    }

The readexcel class just reads an excelsheet of an excelfile and returns some arraylist which are processed in the graphgen class.

public class graphgen extends JFrame {

public graphgen() {

        super("Results");



        gen();

    }

    public void gen(){

//creates the graphcomponent

getContentPane().add(graphComponent);
    add(graphComponent);

}

    public static void main(String[] args)
    {


        graphgen frame = new graphgen();
        p2.add(graphComponent, BorderLayout.CENTER);

    frame.add(p2, BorderLayout.CENTER);
        frame.pack();
        frame.setResizable(true);
        frame.setSize(1600, 1200);
        frame.setVisible(true);

}

Upvotes: 0

Views: 122

Answers (1)

trashgod
trashgod

Reputation: 205785

Use Action to encapsulate the target component, file and sheet, as shown here and here. Add a method to update state of the class based on the chosen sheet. Examples of navigating among cards are seen here and here. See also Card Layout Actions, cited here.

Upvotes: 1

Related Questions