Scope problems with 'this' keyword

jurre

New Member
I am trying to create my own window class which extends \[code\]JFrame\[/code\]. However, I am having a problem with the action listener for \[code\]fullScreenBtn\[/code\]. When writing the \[code\]ActionListener.actionPerformed\[/code\] funcion, I am unable to use the \[code\]this\[/code\] keyword as it refers to \[code\]new ActionListener\[/code\]. How do I refer to the instance of \[code\]MyWindow\[/code\] instead?\[code\]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); } }); }}\[/code\]
 
Back
Top