0

suppose i have a code in java like this

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class GUIExercise {
    private static void createAndShowGUI () {
        JFrame frame = new JFrame("My Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel center = new JPanel();
        center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));

        JLabel label = new JLabel("Migz");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));

        center.add(label);
        JButton btn = new JButton("Click me");
        btn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed (ActionEvent e) {
                JOptionPane.showMessageDialog(GUIExercise.this, "Font.ITALIC is " + Font.ITALIC + " and Font.BOLD is " + Font.BOLD + " finally Font.ITALIC | Font.BOLD is " + (Font.ITALIC | Font.BOLD), "Ni Hao", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        center.add(btn);

        frame.getContentPane().add(center, BorderLayout.CENTER);
        frame.pack();
        frame.setSize(frame.getWidth() + 100, frame.getHeight() + 50);
        frame.setVisible(true);
    }

    public static void main (String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run () {
                createAndShowGUI();
            }
        });
    }
}

putting GUIExercise.this in the first parameter of showmessagedialog will result in error: non-static variable this cannot be referenced from a static context. what must be done? or how can i access the EnclosingClass?

2
  • I still don't see. That code is fine if you assume that EnclosingClass should extends Component. I can't trust this code. Also the error message is sketch because this cannot be the name of a variable. Commented Mar 24, 2014 at 1:33
  • oh i forgot that the first parameter of the JOptionPane.showMessageDialog must be a component or container. my class is neither, so i should put the JFrame. well, thank you for explaining :) Commented Mar 24, 2014 at 1:36

1 Answer 1

2

It seems like you're trying to use that code in a static method. You cannot access an enclosing instance from a static context, since there is no instance.


That isn't the problem. The issue is that you are trying to execute the method directly in the body of the class. You can't do that. You'll have to put it in a method, probably the method you are meant to override as part of the ActionListener interface.

btn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(EnclosingClass.this, "Hello");
    }
});

(Assuming EnclosingClass is a Component.)

Sign up to request clarification or add additional context in comments.

2 Comments

oh, i forgot to put my overriden actionPerformed method, im sorry for that but it still doesn't work :(
@MiguelRemulta See my edit. You cannot do that from a static method. There is no enclosing instance in that context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.