Hi, I am trying to make simple gui which switches jpanel inside jframe but my IDE says "Cannot make static reference to non static method setPage(int) from the type Main". Why is that? There is no static modifier on actionPerformed or at setPage or actually anywhere else than public static void main(String[] args) {}

Main.java

package net.viped;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class Main extends JFrame{
    PwGUI pwgui = new PwGUI();
    FirstPageGUI fpgui = new FirstPageGUI();

    public Main() {
        setContentPane(pwgui);
        setSize(800, 600);
        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void setPage(int i) {
        switch(i) {
        case 1:
            setContentPane(pwgui);
            break;
        case 2:
            setContentPane(fpgui);
            break;
        }
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main();
    }

}

PwGUI.java

package net.viped;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class PwGUI extends JPanel implements ActionListener{

    JButton ok = new JButton();

    public PwGUI(){

        ok.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getActionCommand().equals(ok)) {
            Main.setPage(2);
        }
    }
}

FirstPageGUI.java

package net.viped;

import javax.swing.JButton;
import javax.swing.JPanel;

public class FirstPageGUI extends JPanel{
    JButton newProject = new JButton();
    public FirstPageGUI() {
        add(newProject);
    }
}

Recommended Answers

All 8 Replies

Main.setPage(2) Main is the name of a class, so whatever follows Main. is in a "static context". setPage is an instance mtheod, not static, so it needs an intance when you call it.

You can not call a non-static method like that
Try changing Main.setPage(2); in PwGUI.java to,

Main m = new Main();
m.setPage(2);

Ok, do you have any suggestions how to do that, I tried to do instance of Main but it didnt work as the way I thought it would. Is this even the good way to make many jpanels in jframe?

When you have a main object that the other objects need to refer to, it's normal to pass the main object as a parameter to the other object's constructors, eg

... in Main...
PwGUI pwgui = new PwGUI(this);
FirstPageGUI fpgui = new FirstPageGUI(this);

... in PwGUI ...
Main mainObject;
public PwGUI(Main m){
    mainObject = m;
    ok.addActionListener(this);
    ...

mainObject.setPage(2);

... etc

Thanks James that make it work. Now I have to just find out what that really does so I can understand and use it better.

If you don't understand the solution then I've not done a good job! If it's still not clear after a bit of thought please come back here so we can discuss it further.

We make new instance of Main and with that we use setPage method. It is kind of clear to me. But something there is unclear. I really can't say what. I just havent slept well and my brains aren't functioning correctly. I thinks it's just that I haven't ever send object as a parameter. If you could little clear out that part.

It's no different than sending a String as a parameter. "this" just means the current Main object, so the other classes get a reference to the Main object, just like they would get a reference to a String or any other kind of Object. Maybe after a good night's sleep... ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.