Scope of problems with the 'this' keyword

I am trying to create my own window class that extends JFrame . However, I have a problem with the action listener for fullScreenBtn . When writing the ActionListener.actionPerformed function ActionListener.actionPerformed I cannot use the this keyword because it refers to the new ActionListener . How can I refer to an instance of MyWindow ?

 public class MyWindow extends JFrame { private static GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment(); private static GraphicsDevice gDev = gEnv.getDefaultScreenDevice(); private static JPanel toolbar = new JPanel(); private static JButton fullScreenBtn = new JButton("Show Full Screen"); private static boolean isFullScreen = false; public MyWindow() { toolbar.setLayout(new FlowLayout()); this.getContentPane().add(toolbar, BorderLayout.PAGE_START); fullScreenBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Toggle full screen window this.setUndecorated(!isFullScreen); this.setResizable(isFullScreen); gDev.setFullScreenWindow(this); isFullScreen = !isFullScreen; if (isFullScreen) { fullScreenBtn.setText("Show Windowed"); } else { fullScreenBtn.setText("Show Full Screen"); } } }); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { this.dispose(); System.exit(0); } }); } } 
+4
source share
4 answers

In inner classes, you will need to add using this with the class name of the outer class if you need to get a reference to the outer class: for example, use

 MyWindow.this.setUndecorated(...)` // etc... 

As an aside, you really don't want to extend the JFrame here and in most situations.

In addition, the ancestor window that contains the JButton can be obtained in other ways, for example, through SwingUtilities.getWindowAncestor(theButton) . i.e.,

  public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source instanceof JButton) { JButton button = (button) source; Window ancestorWin = SwingUtilities.getAncestorWindow(button); ancestorWin.setUndecorated(!isFullScreen); ancestorWin.setResizable(isFullScreen); // etc... 

Or, if you know for sure that the ancestor window is a JFrame:

  public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source instanceof JButton) { JButton button = (button) source; JFrame ancestorWin = (JFrame) SwingUtilities.getAncestorWindow(button); ancestorWin.setUndecorated(!isFullScreen); ancestorWin.setResizable(isFullScreen); // etc... 
+7
source

This is the syntax for accessing an instance of the surrounding class from an inner class or anonymous class:

 OuterClass.this.foo(); 
+3
source

Since you are using an anonymous class , this will refer to this class, in this case an ActionListener . Since your ActionListener does not have methods like setUndecorated , this will give you a compilation error.

What you want to do is use MyWindow.this , followed by any MyWindow method.

+1
source

You will need to access this by specifying an external class too, in your case it should be something like the following:

 MyWindow.this 
+1
source

All Articles