Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: Multi-Jpanels in single JFrame

  1. #1
    _-Blackhawk-_ is offline Member
    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0

    Default Multi-Jpanels in single JFrame

    I'm trying to get a single JFrame with several (for now 2 but will be expanded)Jpanels.

    There should only be one Jpanel visible at any given time or a small extra JPanel at the bottem or to that holds al the Buttons to switch between the main JPanels

    I have managed to create a small noob program that creates the Jframe and 2 Jpanels with 2 buttons in the first but i can't get it to switch to the empty one.

    NOTE: When i changed the code to post it here i exidentaly changed something and now the buttons won't appear any more. But I hope you will understand the meaning.

    Any Help would be mutch appreciatie!! A link to a working example with all the course code would also be oke!!

    Java Code:
    package urenreg;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class UrenReg extends JFrame
    {
        ScreenSwitch b;
        public static void main(String[] args) 
        {
            ScreenSwitch b = new ScreenSwitch(1);
            JFrame scherm1 = new UrenReg();
            scherm1.setSize(900, 750);
            scherm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            scherm1.setTitle("Login");
    
            JPanel login = new Login();
            scherm1.setContentPane(login);
            
            JFrame scherm2 = new UrenReg();
            scherm2.setSize(900, 750);
            scherm2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            scherm2.setTitle("Menu");
    
            JPanel menu = new Menu();
            scherm2.setContentPane(menu);
            
            switch(b.schakelaar)
            {
                case 0: scherm1.setVisible(false); 
                        scherm2.setVisible(true);
                        break;
                case 1: scherm1.setVisible(true); 
                        scherm2.setVisible(false);
                        break;
            }
        }
    }
    class Login extends JPanel
    {
        Button loginScreen;
        Button menuScreen;
        int screenChange = 0;
    
        
        public int Login()
        {
            setLayout(null);
            loginScreen = new Button("login");
            loginScreen.addActionListener ( new LoginHandler() );
            loginScreen.setBounds(345, 850, 100, 50);
            add(loginScreen);
            menuScreen = new Button("menu");
            menuScreen.addActionListener ( new MenuHandler() );
            menuScreen.setBounds(455, 850, 100, 50);
            add(menuScreen);
    
            return screenChange;
          }
        class MenuHandler implements ActionListener
        {
            public void actionPerformed( ActionEvent e )
            {
                screenChange = 0;
            }
        }
        class LoginHandler implements ActionListener
        {
            public void actionPerformed( ActionEvent e )
            {
                screenChange = 1;
            }
        }
    }
    class Menu extends JPanel
    {
        
    }
    class ScreenSwitch
    {
        int schakelaar = 0;
        
        public ScreenSwitch(int schakelaar)
        {
            this.schakelaar = schakelaar;
        }
        public int setSchakelaar()
        {
            
            return schakelaar;
        }
        
    }
    Last edited by _-Blackhawk-_; 09-12-2011 at 01:41 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Multi-Jpanels in single JFrame

    Have a look at the available LayoutManagers; e.g. a CardLayout handles several Components (such as JPanels) but only shows a single one. A JTabbedPane is a JComponent itself but shows tabs for the several JComponents it handles (just like a CardLayout does).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    _-Blackhawk-_ is offline Member
    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0

    Default Re: Multi-Jpanels in single JFrame

    I have tryed to use cardlayout for a while but I get 1 big problem: the location where you place everything is rather limited because I could only use general ereas like North, WEST, CENTRAL.

    Did I just do something wrong and does is (or LayoutManager) support a precise placement of components or do I need to use it the way I mention before?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Multi-Jpanels in single JFrame

    Quote Originally Posted by _-Blackhawk-_ View Post
    I have tryed to use cardlayout for a while but I get 1 big problem: the location where you place everything is rather limited because I could only use general ereas like North, WEST, CENTRAL.

    Did I just do something wrong and does is (or LayoutManager) support a precise placement of components or do I need to use it the way I mention before?
    That's BorderLayout. A CardLayout displays one of a selected set of JPanels, and can switch between them fairly easily.

  5. #5
    _-Blackhawk-_ is offline Member
    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0

    Default Re: Multi-Jpanels in single JFrame

    So if I would use Cardlayout there is a fairly easy way to switch between Jpanels and I can just use for example "menuScreen.setBounds(455, 850, 100, 50);" to determine where a specific object is shown?
    If yes could you please post a link or tell me where i can find something like a tutorial. or an example.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Multi-Jpanels in single JFrame

    Quote Originally Posted by _-Blackhawk-_ View Post
    I have tryed to use cardlayout for a while but I get 1 big problem: the location where you place everything is rather limited because I could only use general ereas like North, WEST, CENTRAL.

    Did I just do something wrong and does is (or LayoutManager) support a precise placement of components or do I need to use it the way I mention before?
    A CardLayout doesn't allow any placement at all, i.e. it puts all components on top of each other (just like a deck of cards) and only shows one of them. Of course you can stick the CardLayout in another panel and position that panel again (possibly in another LayoutManager?)

    kind regards,

    Jos

    edit: here's a link to the relevant tutorials
    Last edited by JosAH; 09-12-2011 at 04:45 PM.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Multi-Jpanels in single JFrame

    The art of layout management.
    Picking which ones are useful for the different bits of your GUI and then bolting them all together.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Multi-Jpanels in single JFrame

    Moved from 'New to Java'

    db

  9. #9
    _-Blackhawk-_ is offline Member
    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0

    Default Re: Multi-Jpanels in single JFrame

    I looked in to the Layout managers a bit but I forsee a small problem in the near future:I have to get and move data to a MySQL database in a LAN-network, and the layout only seems to work in applets.

    If I can layoutmanager in an application or use an applet and mysql than for now it's oke if I just know its possible.

    Currently using Netbeans 7.0.1 so if there is a specific categorie and project type that I have to use to combine the 2 please tell me what it is.

    Or is there a way to create an application to connect to MySQL and use another java file type to do the visual part and just have to 2 of them communicate.
    Last edited by _-Blackhawk-_; 09-13-2011 at 09:49 AM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Multi-Jpanels in single JFrame

    Layouts work in Swing.
    Applets happen to use Swing, but that is only a subset of the things that use Swing.

    Assuming you're writing a desktop application using Swing (which is what you say above) then you will need to understand layout managers. See the tutorial linked to by Jos.

    As for talking to MySQL, that's simply another part of your application. You would generally write this as a separate layer that doesn't care what your front end gui looks like.
    Fubarable likes this.

  11. #11
    _-Blackhawk-_ is offline Member
    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0

    Default Re: Multi-Jpanels in single JFrame

    Thx everyone!! After some testing an creating a part of the app(way beonde 2 layers) I think its save to say that:
    This solved and answerd all the questions and problems I have for now.

Similar Threads

  1. Single to multi-dimension array
    By Migy in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 06:12 PM
  2. Multiple JPanels within a JFrame
    By horndinkle in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 07:51 PM
  3. repainting a jframe containing two jpanels
    By musasabi in forum New To Java
    Replies: 0
    Last Post: 05-12-2010, 04:36 AM
  4. Switch JPanels in a single JFrame
    By atom86 in forum AWT / Swing
    Replies: 8
    Last Post: 09-23-2009, 09:30 AM
  5. Single and Multi Selection Lists
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:03 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
  •