Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: panel switching

  1. #1
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default 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.

  2. #2
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    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)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by gedas View Post
    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

  4. #4
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    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]

  5. #5
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    this can be achieve using scrollpane panel. check the documentation of scrollpane panel
    Keep Moving Forward

  6. #6
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    or you can put this on your JFrame

    public void setPanel(javax.swing.JPanel panel) {
    this.setContentPane(panel);
    this.setVisible(true);
    }
    all you have to do is pass the panel on this method
    Keep Moving Forward

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by nap_patague View Post
    or you can put this on your JFrame



    all you have to do is pass the panel on this method
    How is your recommendation better than using CardLayout? Please tell me one advantage.

  8. #8
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    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

  9. #9
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default 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 :

    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);*/
    
                    
               }
            });
    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  11. #11
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    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

  12. #12
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    saleAction.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent arg0)
                {
                    
                    setPanel(new SalesPanel());                
               }
            });
    the method is already in the jframe so you must call it.

    Java Code:
    public void setPanel(javax.swing.JPanel panel) {
    this.setContentPane(panel);
    this.setVisible(true);
    }
    put this code after the centerWindow method
    Keep Moving Forward

  13. #13
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    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:
    Java Code:
    SalesPanel spnl = new SalesPanel();
              BookStoreFrame.this.setContentPane(spnl);
    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.

  14. #14
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default It works.

    Fantastic! It works. Thanks a lot.

  15. #15
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    put this thread to solved.
    Keep Moving Forward

  16. #16
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Absolutly!

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  18. #18
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    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.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  20. #20
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Switching Screens
    By vmcg in forum New To Java
    Replies: 4
    Last Post: 07-08-2010, 07:34 PM
  2. Calculator Switching
    By dilpreet28 in forum New To Java
    Replies: 9
    Last Post: 06-08-2010, 02:05 AM
  3. Switching Frames
    By jonnytabpni in forum New To Java
    Replies: 1
    Last Post: 11-08-2009, 10:12 PM
  4. switching packages
    By DuceDuceExplorer in forum NetBeans
    Replies: 4
    Last Post: 07-09-2008, 11:11 PM
  5. switching between HTTP and HTTPS
    By mutuah in forum Advanced Java
    Replies: 6
    Last Post: 08-03-2007, 10:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •