J.Olufsen
J.Olufsen

Reputation: 13915

Pass static class reference to a constructor of non-static class

I need to have a reference to the Client because I need to invoke a setWinTitle to change the title of current window. How to fix it?

    public class Client { 
        public static void main(String[] args){
            JPanel gui= startGUI();
            ...
        }

        private static JPanel startGUI(){
            f = new JFrame();
            JPanel gui = new JPanel(this); // error
        }

        public void setWinTitle(String tite){
            f.setTitle(tite);
        }
    }

public class JPanel extends javax.swing.JPanel {
    Client client;

    public JPanel(Client cl) {
        client= cl; 
        initComponents();
    }
...
}

Upvotes: 0

Views: 627

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292415

You need to create an instance of Client:

JPanel gui = new JPanel(new Client());

Upvotes: 3

Related Questions