Debug: Accessing function variables from inner classes not possible [message #252212] |
Tue, 25 March 2008 19:39 |
Eclipse User |
|
|
|
Originally posted by: higherfalling.googlemail.com
Hello,
is it possible to access / inspect member variables of a function in an
anonymous inner class like below in following code example? When i put a
breakpoint to the actionPerformed method i cannot access the variables
from the constructor. Is this a bug or some configuration work?
Thanks for your advice
Frank
public class SwingTest {
SwingTest(){
JFrame frame = new JFrame("HelloWorldSwing");
final JLabel label = new JLabel("Some text");
JButton button = new JButton("Click");
final JTextField textField = new JTextField();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
label.setText("You entered: "+ textField.getText());
}
});
frame.getContentPane().setLayout( new GridLayout());
frame.getContentPane().add(textField);
frame.getContentPane().add(button);
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingTest swingTest = new SwingTest();
}
}
|
|
|
|
Re: Debug: Accessing function variables from inner classes not possible [message #252260 is a reply to message #252212] |
Wed, 26 March 2008 13:32 |
Eclipse User |
|
|
|
Originally posted by: eclipse-news.rizzoweb.com
Frank wrote:
> Hello,
>
> is it possible to access / inspect member variables of a function in an
> anonymous inner class like below in following code example? When i put a
> breakpoint to the actionPerformed method i cannot access the variables
> from the constructor. Is this a bug or some configuration work?
This can't work the way you want. The problem is that the c'tor
variables are out of scope at the time when actionPerformed() is called
(it is called by the Swing event thread in response to an event on the
JButton). In order to make them in scope in actionPerformed() you'd have
to refer to them in that method (which will only work for the ones that
are declared final, by the way).
Hope this helps,
Eric
> public class SwingTest {
>
> SwingTest(){
> JFrame frame = new JFrame("HelloWorldSwing");
> final JLabel label = new JLabel("Some text");
> JButton button = new JButton("Click");
> final JTextField textField = new JTextField();
> button.addActionListener(new ActionListener(){
> public void actionPerformed(ActionEvent e) {
> label.setText("You entered: "+
> textField.getText()); }
> }); frame.getContentPane().setLayout( new GridLayout());
> frame.getContentPane().add(textField);
> frame.getContentPane().add(button);
> frame.getContentPane().add(label);
> frame.pack();
> frame.setVisible(true);
> }
> public static void main(String[] args) {
> SwingTest swingTest = new SwingTest();
> }
> }
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.02975 seconds