Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » Component access from event methods (Anonymous class)
Component access from event methods (Anonymous class) [message #707019] Mon, 01 August 2011 06:07 Go to next message
someone  is currently offline 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

Re: Component access from event methods (Anonymous class) [message #707282 is a reply to message #707019] Mon, 01 August 2011 12:39 Go to previous messageGo to next message
Eric Clayberg is currently offline Eric Clayberg
Messages: 860
Registered: July 2009
Location: Boston, MA
Senior Member
Do you want to reference the widget outside of that class for some reason?
Re: Component access from event methods (Anonymous class) [message #721981 is a reply to message #707282] Sat, 03 September 2011 22:02 Go to previous messageGo to next message
someone  is currently offline someone
Messages: 19
Registered: July 2009
Junior Member
No, I only want to reference it from the anonymous class method (Event).
Re: Component access from event methods (Anonymous class) [message #722142 is a reply to message #721981] Sun, 04 September 2011 16:21 Go to previous message
Eric Clayberg is currently offline Eric Clayberg
Messages: 860
Registered: July 2009
Location: Boston, MA
Senior Member
In that case, I don't see any reason to create an accessor for the widget. Just making it a field should be sufficient for what you want to do.
Previous Topic:Setting orientation
Next Topic:NPE in org.eclipse.wb.internal.swt.support.AbstractSupport$1 after adding a getter
Goto Forum:
  


Current Time: Fri Sep 19 20:11:39 GMT 2014

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

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