Results 1 to 20 of 21
Thread: panel switching
- 03-26-2011, 05:36 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
panel switching
Is it possible to switch panels within the same frame?
I want to have a welcomePanel and another working panel. I want the welcome panel to come first and when I click on a button or select a menu item I want the welcome screen to go away and a new panel to load. I tried to set the welcome screen to invisible by adding the code "wPanel.setVisible(false);" to the action listener code of the menu selection and did not work.
Thanks for the help.
- 03-26-2011, 05:48 PM #2
Member
- Join Date
- Feb 2010
- Posts
- 51
- Rep Power
- 0
instead of using panels create two frames. one for welcome the other for your other information.
once your welcome frame shows and you click the button to continue set it to setVisible(false) and your other frame setVisible(true)
-
Original poster, you may be better off with your original idea of swapping JPanels. The best way to do this with Swing is use a CardLayout. Usually one JPanel acts as the card holder and uses CardLayout. You would then add the other JPanels, the ones that you want to swap, into the card holder JPanel along with a String constant. Then you can swap JPanels to your heart's content. Please check the tutorial on this useful layout here: How To Use CardLayouts
- 03-26-2011, 10:41 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
Java Code:[PHP][/PHP]Moderator, I like the idea but it turned out to be a little complicated to understand. If you can offer an insight how to apply CardLayout in the following situation, i would appreciate it. class BookStoreFrame extends JFrame { private int currentCard = 1; private JPanel cardPanel; private CardLayout cl; public BookStoreFrame() { setTitle("XX Book Store"); setPreferredSize(new Dimension(800, 400)); setResizable(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //this.add(new SalesPanel()); this.pack(); centerWindow(this); JMenuBar menuBar = new JMenuBar(); // Add the menubar to the frame setJMenuBar(menuBar); //JMenuBar menuBar = new JMenuBar(); JMenu saleMenu = new JMenu("Sales"); JMenu returnMenu = new JMenu("Returns"); JMenu inventoryMenu = new JMenu("Inventory"); JMenu printMenu = new JMenu("Print"); menuBar.add(saleMenu); menuBar.add(returnMenu); menuBar.add(inventoryMenu); menuBar.add(printMenu); // Create and add simple menu item to one of the drop down menu JMenuItem saleAction = new JMenuItem("Make sales"); JMenuItem returnAction = new JMenuItem("Process Returns"); JMenuItem addNewBookAction = new JMenuItem("Add New Book"); JMenuItem checkInventoryAction = new JMenuItem("Availability"); JMenuItem printDailyAction = new JMenuItem("Print Daily Report"); saleMenu.add(saleAction); returnMenu.add(returnAction); inventoryMenu.add(addNewBookAction); inventoryMenu.add(checkInventoryAction); printMenu.add(printDailyAction); saleAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //SalesPanel } }); returnAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //ReturnPanel } }); addNewBookAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //InventoryPanel } }); checkInventoryAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //InventoryPanel } }); printDailyAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); } private void centerWindow(Window w) { Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getScreenSize(); setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2); } }[/PHP]
- 03-27-2011, 12:59 AM #5
Member
- Join Date
- Mar 2011
- Location
- Bulacan, Philippines
- Posts
- 23
- Rep Power
- 0
this can be achieve using scrollpane panel. check the documentation of scrollpane panel
Keep Moving Forward
- 03-27-2011, 01:06 AM #6
Member
- Join Date
- Mar 2011
- Location
- Bulacan, Philippines
- Posts
- 23
- Rep Power
- 0
or you can put this on your JFrame
all you have to do is pass the panel on this methodpublic void setPanel(javax.swing.JPanel panel) {
this.setContentPane(panel);
this.setVisible(true);
}Keep Moving Forward
-
- 03-27-2011, 01:21 AM #8
Member
- Join Date
- Mar 2011
- Location
- Bulacan, Philippines
- Posts
- 23
- Rep Power
- 0
i use that on one of my project and it's working fine. the layout will be on every panel that you will pass on the method. the advantage is easier to use and much simpler than card layout. it only my suggesstions
Last edited by nap_patague; 03-27-2011 at 01:32 AM.
Keep Moving Forward
- 03-27-2011, 01:40 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
not sure how to apply either
I spent some time to figure out the cardLayout and I think I Will need more time learning how it works. The setContentPane, I tried :
and got "non-static method setContentPane(java.awt.Container) cannot be referenced from a static context" for the first and the commented part shows no error but does nothing either.Java Code:saleAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { BookStoreFrame.setContentPane(SalesPanel); /* BookStoreFrame bkstr= new BookStoreFrame(); SalesPanel spnl = new SalesPanel(); bkstr.setContentPane(spnl);*/ } });
-
BookStoreFrame is the class not an object and you can't call methods on it as if it were an object. To get the current BookStoreFrame object, you'll need to use "this", but since you're referencing it from within an inner class, you'll need to specify which "this" you mean, the inner class or the outer class. This is done by pre-pending the class name to this:
Java Code:BookStoreFrame.this.setContentPane(SalesPanel);
But again, you never answered my question, which thread is the active thread. Again, we should avoid needlessly splitting discussion if possible as it's not fair to the members of the forum.
- 03-27-2011, 01:49 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
sorry for the duplication but this will be the thread I will be following. If you have the authority you may delete the other one. Thanks
- 03-27-2011, 01:54 AM #12
Member
- Join Date
- Mar 2011
- Location
- Bulacan, Philippines
- Posts
- 23
- Rep Power
- 0
the method is already in the jframe so you must call it.Java Code:saleAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { setPanel(new SalesPanel()); } });
put this code after the centerWindow methodJava Code:public void setPanel(javax.swing.JPanel panel) { this.setContentPane(panel); this.setVisible(true); }Keep Moving Forward
- 03-27-2011, 02:00 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
It looks like it is going to work , somehow. When I put your code as it is I get
"cannot find symbol
symbol: variable SalesPanel" error.
However, I put this:
and I get a new frame with the desired panel. If there is a way how I can switch the panels without having to use a new frame it would be great.Java Code:SalesPanel spnl = new SalesPanel(); BookStoreFrame.this.setContentPane(spnl);
- 03-27-2011, 02:06 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
It works.
Fantastic! It works. Thanks a lot.
- 03-27-2011, 02:09 AM #15
Member
- Join Date
- Mar 2011
- Location
- Bulacan, Philippines
- Posts
- 23
- Rep Power
- 0
put this thread to solved.
Keep Moving Forward
- 03-27-2011, 02:10 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
Absolutly!
-
don't do this. use cardlayout as this is what it is built for. Seriously.
- 03-27-2011, 02:19 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
I would love to learn it. I just could not figure it out. I could not find a clear example or guide online. If you can help me out I am all for it.
-
Let's see your attempt with it. Have you tried anything out using it?
- 03-27-2011, 02:41 AM #20
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
I have rescinded all of my attempts from the code but I tried to follow the example in this website: Java Tips - How to use AWT CardLayout.
I will try it again and post my result tomorrow morning. I will be in the forum for most part of the day and I appreciate your tutorship.
Similar Threads
-
Switching Screens
By vmcg in forum New To JavaReplies: 4Last Post: 07-08-2010, 07:34 PM -
Calculator Switching
By dilpreet28 in forum New To JavaReplies: 9Last Post: 06-08-2010, 02:05 AM -
Switching Frames
By jonnytabpni in forum New To JavaReplies: 1Last Post: 11-08-2009, 10:12 PM -
switching packages
By DuceDuceExplorer in forum NetBeansReplies: 4Last Post: 07-09-2008, 11:11 PM -
switching between HTTP and HTTPS
By mutuah in forum Advanced JavaReplies: 6Last Post: 08-03-2007, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks