Add a listener inside or outside get method
- by James P.
I'm learning Swing and have composed an interface using a series of get methods to add components. Is it a good practise to add a Listener inside a get method as follows? I'd like to make things as decoupled as possible.
private JButton getConnectButton() {
if (connectButton == null) {
connectButton = new JButton();
connectButton.setText("Connect");
connectButton.setSize(new Dimension(81, 16));
connectButton.setLocation(new Point(410, 5));
connectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// actionPerformed code goes here
}
});
}
return connectButton;
}