Results 1 to 14 of 14
  1. #1
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Post Repaint the entire JFrame (GroupLayout)

    Hi!

    I am implementing a program that simple enough to say, it will go through a sequence of windows (like when we are playing a game, before we can play the game, a sequence of windows will appear : introduction, setting, ...).

    Because the GUI in my program is a little complicated, so I choose GroupLayout with the help of NetBeans IDE.

    I've found some articles in the internet, I tried and the technique worked fine with simple GUIs. But when I used in my program, this technique did not work at all.

    The concept is to call validate() in the JFrame where the GUI is to displayed.

    The problem is when the second window is loaded, the components in the first window is not deleted and they are still there but they are overlapped by the components of the second window and they still react to the listener when they are pressed

    Here is the reduced segment of code that I wrote. I hope this is enough for you to understand my problem :

    Java Code:
    // other stuffs
    ....
    ....
    
    class MainFrame extends JFrame
    {
        JButton loginButton;
    
        public MainFrame()
        {
            // load the login window 
            createLoginPage();
    
            // the menu window will be displayed when the login button in the login window is pressed
            loginButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    createMenuPage();
                    validate(); // this won't work
                }
            });
        }
    
        // GUI for the login window
        private void createLoginPage()
        {
            // codes use [B]GroupLayout[/B] 
        }
    
        // GUI for the menu window
        private void createMenuPage()
        {
            // codes use [B]GroupLayout[/B]
        }
    }
    Please help me on this!

    Thanks in advance!
    Write a program to achieve anything you want in your life!

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

  3. #3
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Perhaps you want to swap "screens" via a CardLayout?
    Hi Fubarable!

    As I said, I am using GroupLayout in my program because the GUI is a little complicated.

    Yeah, the word swap is more suitable to descibe the problem that I am trying to solve.

    Great thanks!
    Write a program to achieve anything you want in your life!

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

    Default

    Quote Originally Posted by Willi View Post
    As I said, I am using GroupLayout in my program because the GUI is a little complicated.
    Yes, I understood this perfectly well.

    Yeah, the word swap is more suitable to descibe the problem that I am trying to solve.
    You've stated nothing to so far that seems to prevent you from using a CardLayout. The CardLayout doesn't care what layout is being used on the components that it swaps. The one thing important in this context though is that all components are expanded to the size of the largest component held in the Container that's using CardLayout.

  5. #5
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Post

    Quote Originally Posted by Fubarable View Post
    Yes, I understood this perfectly well.



    You've stated nothing to so far that seems to prevent you from using a CardLayout. The CardLayout doesn't care what layout is being used on the components that it swaps. The one thing important in this context though is that all components are expanded to the size of the largest component held in the Container that's using CardLayout.
    :o

    Actually, I don't come from a country that english is the first language, so sometimes I found it difficult to express my idea to others, especially when I want to find some useful stuffs by Google :o. I tried transition or repaint and some other words but no useful thing appeared and maybe swap is more suitable. It's funny, isn't it? :confused:

    Honestly, I didn't study CardLayout yet and for the first time I wanted to implement this program, I found it will be easier to implement using GroupLayout and I think it's the best solution for my program requirements.

    I appreciate your advice on using CardLayout but I think it's better for me to stick on GroupLayout because I've worked hard recently and I've implemented almost every window that is needed for my program and now I am trying to connect them together. Spending time to redo everything is a bit awful. T_T

    Do you have any idea? Thanks
    Last edited by Willi; 12-19-2009 at 07:09 PM.
    Write a program to achieve anything you want in your life!

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

    Default

    Honestly, I didn't study CardLayout yet and for the first time I wanted to implement this program, I found it will be easier to implement using GroupLayout and I think it's the best solution for my program requirements.
    You seem stuck on the idea that you can only use one layout at a time for your GUI. The sooner you purge yourself of this idea, the better off you will be.

    While I'm not a big fan of GroupLayout, I can see it has it's uses. If I were in your situation and were forced to use GroupLayout and had two GUI screens that I wanted displayed, I'd create two JPanels that had complex GUIs on them and then create a third JPanel that used CardLayout. This third panel would use the CardLayout display the first JPanel and then when required, the second JPanel.

    So my question to you is, how does this not work for you? Nowhere have you answered this question to my satisfaction.

    As I said, I am using GroupLayout in my program because the GUI is a little complicated.
    The next idea that I'd like to help expunge from your mind is that complex layouts require complex layout managers. It is usually easier to create and maintain complex layouts by nesting components such as JPanels each one using a relatively simple layout manager. This however has no bearing on your desire to learn how to swap screens, and so is a problem to be tackled later.

    If none of this helps you, I'd usually ask you to create an SSCCE (please see the link), but looking at code posted here that uses GroupLayout can be painful to the eyes. You may even consider posting images of your GUI's: what you desire, and what you are currently seeing.

    Much luck!

  7. #7
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    JTabbedPane offers the same multiple pages of CardLayout, and also offers a nice user interface.

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

    Default

    Quote Originally Posted by zweibieren View Post
    JTabbedPane offers the same multiple pages of CardLayout, and also offers a nice user interface.
    I agree that JTabbedPane is another excellent way to allow the user to view different GUIs in one application. I tend to use JTabbedPane when I want to give the user a random-access to different JPanels, and use CardLayout when I want to give the user a sequential access to different JPanels or if I want tighter control over what the user can access.

    Thoughts?

  9. #9
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    Thanks so much! =^_^=

    it's really a valuable reply to me. The idea that our program can use only one Layout Manager is actually not one of mine , but the second thing that you told me is definitely one of my obstacles.

    And one thing I want to make it clear (actually I've look through CardLayout) is I am implementing my program in Full-Screen Exclusive Mode and for example I have 2 screens S1 and S2. S1 is loaded first, and then S2. Each of them will be sequentially loaded. The loading task will be done by JButton that is on each screen.

    In your post above, I misunderstood. I thought that you told me to implement my GUI using CardLayout. But I think your idea is that I should implement a CardLayout that is used to swap 2 GUI screens implemented using GroupLayout. I hope I'm correct T_T.

    While I'm not a big fan of GroupLayout, I can see it has it's uses. If I were in your situation and were forced to use GroupLayout and had two GUI screens that I wanted displayed, I'd create two JPanels that had complex GUIs on them and then create a third JPanel that used CardLayout. This third panel would use the CardLayout display the first JPanel and then when required, the second JPanel.
    From this, I thought of a solution is : I will make a top-level JPanel for each screen, this JPanel will have same size with the screen and it's used to hold all GUI components for each screen in it. Each time I need to go to another screen, I just call validate(). In some previous examples I tried before, it worked fine maybe because the GUI has only one JPanel at the TOP using BorderLayout. I will try the same solution with a little trick. Hope this work!

    I will post a screenshot of my program to get more advices!

    P/S : God bless my English! :o
    Write a program to achieve anything you want in your life!

  10. #10
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    I attached 2 screens in my application.

    When the user enter username and password in the Login screen and press Login, the Main Menu will appear.

    If the user press Back in the Main Menu, the application will go back to Login screen.

    I am implementing this and the result will be updated.

    Thanks!
    Last edited by Willi; 12-19-2009 at 10:00 PM.
    Write a program to achieve anything you want in your life!

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

    Default

    Quote Originally Posted by Willi View Post
    In your post above, I misunderstood. I thought that you told me to implement my GUI using CardLayout. But I think your idea is that I should implement a CardLayout that is used to swap 2 GUI screens implemented using GroupLayout. I hope I'm correct T_T.
    Yes, this is correct.

    From this, I thought of a solution is : I will make a top-level JPanel for each screen, this JPanel will have same size with the screen and it's used to hold all GUI components for each screen in it. Each time I need to go to another screen, I just call validate(). In some previous examples I tried before, it worked fine maybe because the GUI has only one JPanel at the TOP using BorderLayout. I will try the same solution with a little trick. Hope this work!
    I don't see a need to call validate here.

    P/S : God bless my English! :o
    Your English is fine, certainly much better than my (distantly) second language, Spanish.

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

    Default

    Quote Originally Posted by Willi View Post
    I attached 2 screens in my application.
    First on seeing your images, I can safely say two things:

    1) that a CardLayout would work and likely work well in this situation, and

    2) that I would classify the layouts required for this application as simple, not complex.

    Best of luck!

  13. #13
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Post

    hi! Actually this is the login page, so it is not complicated like other screen. Take a look at the picture I attached below. This looks complicated but it can be implemented easily using your advice :

    It is usually easier to create and maintain complex layouts by nesting components such as JPanels each one using a relatively simple layout manager.
    Anyway, the solution that I found in the above post has just been implemented. Here is the sort of code. For each screen, I created a class that
    extends a JPanel and it works perfectly.

    Java Code:
    // Other Stuffs
    ....
    ....
    
    class MainFrame extends JFrame
    {
        public MainFrame()
        {
            // First load the Login window
            final CreateLoginPage panel = new CreateLoginPage();
    	add(panel);
    
    	panel.getLoginButton().addActionListener(new ActionListener()
    	{
    		public void actionPerformed(ActionEvent e)
    		{
                            // maybe this is not the best approach
    			remove(panel);
    			mainMenu = new CreateMainMenuPage();
    			add(mainMenu);
    			validate();
    		}
    	});
        }
    }
    
    class CreateLoginPage extends JPanel
    {
    private JButton loginButton;
        public CreateLoginPage()
        {
            loginButton = new JButton("Login");
            // Codes for creating the Login Window
        }
        public JButton getLoginButton()
        {
    	return loginButton;
        }
    }
    
    
    class CreateMainMenu extends JPanel
    {
        public CreateMainMenu()
        {
              // Codes for creating the Main Menu Window
        }
    }
    I don't see a need to call validate here.
    Maybe If we use CardLayout to manage the screen, validate() is not required, but my approach is a little different. I will try to make this work by using CardLayout :D

    Thanks so much for your enthusiasm!

    PS : Go to bed now, it's 2:50 in my local time T_T! Hope I will get a nice dream :mad:
    Attached Thumbnails Attached Thumbnails Repaint the entire JFrame (GroupLayout)-new-student.jpg  
    Last edited by Willi; 12-19-2009 at 10:04 PM.
    Write a program to achieve anything you want in your life!

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

    Default

    I'm glad that you're making progress.

    Your JPanel above can also be created using simpler nested layouts, of course, but another decent layout to look into is the MiGLayout which you can find out about and download here: MiGLayout

Similar Threads

  1. Anyone know how GroupLayout works?
    By ProgrammingPup in forum Advanced Java
    Replies: 5
    Last Post: 12-01-2009, 11:12 PM
  2. Doubt with GroupLayout
    By emylyano3 in forum AWT / Swing
    Replies: 1
    Last Post: 06-28-2009, 02:16 AM
  3. Download the entire web site?
    By makpandian in forum New To Java
    Replies: 0
    Last Post: 03-14-2009, 10:39 AM
  4. help about class GroupLayout
    By MaHa in forum NetBeans
    Replies: 2
    Last Post: 09-07-2008, 11:25 PM
  5. [SOLVED] Alignment in GroupLayout
    By nanou in forum AWT / Swing
    Replies: 8
    Last Post: 09-03-2008, 03:14 AM

Tags for this Thread

Posting Permissions

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