zdcobran
zdcobran

Reputation: 311

Access a class object from its inner class

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();
    } 

Upvotes: 3

Views: 494

Answers (3)

Riduidel
Riduidel

Reputation: 22308

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

 JOptionPane.showMessageDialog(JFrame.this, "HelloEnd", "End of World", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("explode.jpg"));

Upvotes: 4

Kel
Kel

Reputation: 7790

You can use OuterClassName.this.

Upvotes: 1

shybovycha
shybovycha

Reputation: 12275

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"));

Upvotes: 0

Related Questions