0
votes

I am creating a quiz and have created a class with a JFrame which sort of acts like the main menu. On this menu, I have created a JButton which I want to open the seperate JDialog (which will contain the questions etc).

The JDialog is a seperate class called questionDialog.java

I believe you have to implement an action listener calling setVisible(true) however when I do that, I get a cannot make static reference to non-static method setvisible error.

Any help would be greatly appreciated, I am using eclipse and Jigloo for the GUI

here is my code in my main menu JFrame class, specifically the code for the button I want to open the new JDialog public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { NewJFrame inst = new NewJFrame(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); }

 startButton = new JButton();
        getContentPane().add(startButton);
        startButton.setText("Start Quiz");
        startButton.setBounds(454, 239, 65, 23);

And here is the code which gives me the error

startButton = new JButton();
        getContentPane().add(startButton);
        startButton.setText("Start Quiz");
        startButton.setBounds(454, 239, 65, 23);
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent)
            {
                questionDialog.setVisible(true);
            }
        });

Here is the code from the seperate JDialog class

package ZillionaireGUI;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class questionDialog extends javax.swing.JDialog {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                questionDialog inst = new questionDialog(frame);
                inst.setVisible(true);
            }
        });
    }

    public questionDialog(JFrame frame) {
        super(frame);
        initGUI();
    }

    private void initGUI() {
        try {
            setSize(400, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    
}
3
The code you posted has no errors. Where exactly are you seeing the error?Paul Samsotha
@peeskillet I don't see an error either. I think @user3455584 is trying to open the questionDialog by calling questionDialog.setVisible(true). Is that the case?SharpKnight
@SharpKnight yes that is correct, however when I try doing this I get the aforementioned erroruser3455584
POst your code with the actionListener()StanislavL
I have edited my post with the action listeneruser3455584

3 Answers

3
votes

Here's what you should do.

  1. Get rid of the main method in the JDialog class. Your application should only have one main method, and that should be in your JFrame class.

  2. Don't create a new JFrame to pass it to your dialog.

  3. To open it on a button click just create a new questionDialog() passing the current frame to it. Something like this

    public class MyFrame extends JFrame {
       public MyFrame() {
           JButton but = new JButton("but");
           but.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e) {
                   QuestionDialog dialog = new QuestionDialog(MyFrame.this);
                   dialog.setVisible(true);
               }
           });
       }
    
       public static void main(String[] args) {
           SwingUtilities.invokeLater(new Runnable(){
               public void run() {
                   new MyFrame();
               } 
           });
       }
    }
    
    public class QuestionDialog extends JDialog {
        public QuestionDialog(Frame parent) {
            super(parent);
        }
    }
    

Bonus

  • You are getting the error doing questionDialog.setVisible(true) because setVisible is an instance method and you are trying to call it in a static way. You need to create a new instance of your dialog class to call it.

  • Use Java naming convention. Class names begin with capital letters.
    questionDialogQuestionDialog

0
votes

In your main menu write the following

 startButton.addActionListener(new ActionListener()
 {  
    public void actionPerformed(ActionEvent e)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               JFrame frame = new JFrame();
               questionDialog inst = new questionDialog(frame);
               inst.setVisible(true);
            }
        });
    }
 });
0
votes

I think it is likely that you are trying to do something to a non-static member while you are in main (which is a static method). You should just use main to create an instance and then call some method of that instance. I've put some working code below:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Zillionaire extends JFrame implements ActionListener {

    JButton startButton;

    public static void main(String[] args) {
        Zillionaire zillionaire = new Zillionaire();
        zillionaire.init();
    }

    private void init() {
        startButton = new JButton();
        // Removed: we just use add now, and bets to do this last.
//        getContentPane().add(startButton); 
        startButton.setText("Start Quiz");
        startButton.setBounds(454, 239, 65, 23);
        startButton.addActionListener(this);
        // Add after the button is configured, not before
        add(startButton);

        // This just makes our JFrame pretty and visible
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    // Anything that implements the ActionListener interface can listen
    // for the button being pressed and activate the JDialog
    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Button pressed, but Dialog doesn't do much");
        QuestionDialog questionDialog = new QuestionDialog(this);
    }

}

class QuestionDialog extends javax.swing.JDialog {

    public QuestionDialog(JFrame frame) {
        super(frame);
        initGUI();
    }

    // Set it visible when we make our GUI
    private void initGUI() {
        try {
            setSize(400, 300);
            // Added so we can see something
            add(new JLabel("I should add something!"));
            setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}