Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Debug: Accessing function variables from inner classes not possible
Debug: Accessing function variables from inner classes not possible [message #252212] Tue, 25 March 2008 19:39 Go to next message
Eclipse UserFriend
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 #252216 is a reply to message #252212] Tue, 25 March 2008 19:41 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: higherfalling.googlemail.com

I forgot: I use eclipse 3.3.2
Re: Debug: Accessing function variables from inner classes not possible [message #252260 is a reply to message #252212] Wed, 26 March 2008 13:32 Go to previous message
Eclipse UserFriend
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();
> }
> }
>
>
Previous Topic:External Tool Builder isn't executed during manual build and auto build
Next Topic:How to make Source->Generate getters and setters use PropertyChange
Goto Forum:
  


Current Time: Wed Jan 15 10:52:54 GMT 2025

Powered by FUDForum. Page generated in 0.03448 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top