javaeclipseclasswindowbuilder

How to call a class that was created using windowBuilder (Eclipse)


I am used to being able to create an instance of another class by running

Config con = new Config();
con.setVisible(true);

However, this appears not to work with the way the WindowBuilder plugin has set up the gui in Config. When the previous command is run, it creates an empty, tiny JFrame. The main method of Config contains just the following:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Config window = new Config();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

the constructor just calls an initialize method which contains the content creation:

public Config(){
initialize();
}
private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);

//other configuration here
}

How can I call Config from another class to run and be visible?


Solution

  • Alright, found the answer. First, to stop the tiny useless frame from being displayed, do not set visible true from the initial class. Instead just run:

    Config con = new Config();
    

    Then, in the constructor of Config, after initializing everything, add

    frame.setVisible(true);