3

This method is inside JFrame object, how can I pass that JFrame object as an argument to the method in its inner class?? My code is: The comment explains what I am interested to do:

public void runTime(){        
        ActionListener action = new ActionListener(){
            public void actionPerformed(ActionEvent e){
                count++;                
                text.setText(new Integer(count).toString());
                while (count==2012){
                    //I want to pass the frame that holds this rather than null, how it is possible?
                    JOptionPane.showMessageDialog(null, "HelloEnd", "End of World", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("explode.jpg"));
                    break;
                }
            }
        };
        tr = new Timer(1000,action);
        tr.start();
    } 
shybovycha
  • 11,556
  • 6
  • 52
  • 82
zdcobran
  • 301
  • 2
  • 3
  • 10
  • 2
    possible duplicate of [How do you get a reference to the enclosing class from an anonymous inner class in Java?](http://stackoverflow.com/questions/31201/how-do-you-get-a-reference-to-the-enclosing-class-from-an-anonymous-inner-class-i) – Riduidel Jan 26 '11 at 09:52

3 Answers3

4

Hopefully, this question wasn't answered before. Anyway, try

 JOptionPane.showMessageDialog(JFrame.this, "HelloEnd", "End of World", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("explode.jpg"));
Community
  • 1
  • 1
Riduidel
  • 22,052
  • 14
  • 85
  • 185
1

You can use OuterClassName.this.

Kel
  • 7,680
  • 3
  • 29
  • 39
  • Actually, I have fell in this problem before. The context of the problem was same, I had to access the reference to enclosing class from the inner class. Thank you for you answer. It's great, solved my problem. – zdcobran Jan 26 '11 at 10:06
0

AFAIK, if your method is not static, you can use this and super keywords. Here's some tutorial. E.g.:

JOptionPane.showMessageDialog(this, "HelloEnd", "End of World", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("explode.jpg"));
shybovycha
  • 11,556
  • 6
  • 52
  • 82
  • If I use the this keyword, then this represents the action object in my code (from ActionListener class), but I want to make the parent component from outerclass. – zdcobran Jan 26 '11 at 10:01