Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:25:03+00:00 2026-05-26T00:25:03+00:00

I create a new frame in EventDispatch thread and want to add new Panels

  • 0

I create a new frame in EventDispatch thread and want to add new Panels to that later on. But all i get is a blank frame, with 0 height. But panels added from inside the inner class are displayed. How to add using showFirstFrame()?
I had to follow such an approach after getting this issue: All the Swing frames get "frozen" when wait() is called in Java

I’ve been referring to this tutorial: http://leepoint.net/JavaBasics/gui/gui-commentary/guicom-main-thread.html

Thanks in advance.

public class GUIController {
    JFrame bf;
    JFrame tempFrame;

    public JFrame showFrame(){
        SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                            try {
                                Class c;
                                Constructor ctr;
                                c = Class.forName("SomeJFrame");
                                ctr = c.getConstructor();
                                GUIController.this.bf.removeAll();
                                GUIController.this.bf = (BaseFrame) ctr.newInstance();
                                GUIController.this.bf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                                GUIController.this.bf.pack();
                                GUIController.this.bf.setVisible(true);

                        } catch (InstantiationException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalArgumentException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InvocationTargetException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NoSuchMethodException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (SecurityException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                        }

                }
            });

  return  GUIController.this.bf;
}


public void showFirstFrame(){
         tempFrame = showFrame();
        tempFrame .getContentPane().add(headerPanel, BorderLayout.PAGE_START);
           tempFrame .getContentPane().add(new EnterSomePanel(), BorderLayout.CENTER);
           tempFrame .getContentPane().add(footerPanel, BorderLayout.PAGE_END);
            tempFrame .setVisible(true);

    }
}

EDIT:

...

class GUIController {


    HeaderPanel headerPanel = new HeaderPanel(); // extends JPanel
    FooterPanel footerPanel = new FooterPanel();
    BaseFrame bf = new BaseFrame(); // extends JFrame

    public BaseFrame showFrame(String frameName){


         try {
                        Class c;
                        Constructor ctr;
                        c = Class.forName("some.dynamically.loaded.JFrame" + frameName);
                        ctr = c.getConstructor();
                        bf = (BaseFrame) ctr.newInstance();
                        bf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                        bf.pack();
                        bf.setVisible(true);

                    } catch (InstantiationException ex) {
                        Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalArgumentException ex) {
                        Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InvocationTargetException ex) {
                        Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NoSuchMethodException ex) {
                        Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SecurityException ex) {
                        Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
                    }

        return bf;

    }


    public void showFirstFrame(final String frame){ //some controller will pass a frame name to this

        bf =  showFrame(frame);

        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    bf.getContentPane().add(headerPanel, BorderLayout.PAGE_START);
                    bf.invalidate();
                    bf.validate();
                    System.out.println("test");
                }
         });


    }
}

class Main{
    public static void main(String args[]){
        GUIController c = new GUIController();
        c.showFirstFrame("FirstFrame");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:25:04+00:00Added an answer on May 26, 2026 at 12:25 am

    don’t recreate GUI this way, if you want ot only switch betweens two JPanels inside JFrame then you have two very simple choises

    1) JFrame has by defalut BorderLayout, and if you put there JPanel (add.myPanel;) then this JPanel is placed to the CENTER area and occupated whole JFrame, and in BorderLayout only one JComponent could be placed to the concrete area, then you will call only (no remove, any reason for that)

    myFatherPanel.add(myPanel, BorderLayout.PAGE_START);
    revalidate();
    repaint();
    

    2) and best of all would be lay your GUI by using CardLayout, then you can pretty to forgot about all problems with your GUI

    3) more safer would be to place (to the JFrame) FatherPanel (that never has been removed) and remove JComponents from this FatherPanel, because if you calling for JFrame#removeAll(), then you removed RootPane, and from your JFrame stays there only Borders same as you decribed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create new desktop with CreateDesktop and want to get it's DC & RC.
I am trying create a new frame in wxPython that is a child of
I actually want to create a new local. I know it sounds dubious, but
I create new ASP.NET web application that use SMTP to send message. The problem
Eclipse and MyEclipse create new Java files with an extra blank line after the
I can create new Silverlight App with .NET RIA Services enabled. But when I
Hai All... I want to change the uipagecontroller indicator(dot) color...For that i'm doing the
I am trying to create a new column (variable) according to the values that
I'm not understanding how to create a new 'lagged' column in a data.frame. My
I've got a simple Swing GUI and I want to add a new line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.