Add a listener inside or outside get method

Posted by James P. on Stack Overflow See other posts from Stack Overflow or by James P.
Published on 2010-05-24T09:33:47Z Indexed on 2010/05/24 9:51 UTC
Read the original article Hit count: 277

Filed under:
|
|
|

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;
 }

© Stack Overflow or respective owner

Related posts about java

Related posts about swing