Component access from event methods (Anonymous class) [message #707019] |
Mon, 01 August 2011 06:07 |
someone Messages: 19 Registered: July 2009 |
Junior Member |
|
|
What is the best way to access a component from an anonymous class method when the component is declared as a local variable?
It seems like I have to convert it to a field but would it be better to click convery to field in the properties pane and access it directly by it's name or by doing expose component (which also converts to field) and using the get*** method?
Swing example:
private JButton btnTest;
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
btnTest = new JButton("Test");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
<btnTest or getBtnTest()>?.setText("OK");
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnTest)
.addContainerGap(335, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnTest)
.addContainerGap(229, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
public JButton getBtnTest() {
return btnTest;
}
[Updated on: Mon, 01 August 2011 06:08] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03601 seconds