Q:

Java: I can't put my actionlistener outside the constructor

public Server(){
 start.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            try{    
                port = Integer.parseInt(portInput.getText());
            }
            catch(NumberFormatException e){
                text.append("");
            }

        }

 });

}

Having the actionlistener inside the constructor, I can't use the append method becacuse it tells me to add a Server cast to text.append("");
When I do this it tells me I "Cannot cast from JTextArea to Server"
When I move the action listener outside of the constructor it gives me an error and basically forces me to put the action listener inside the constructor. So what I want is to be able to have the action listener outside the constructor so I can call the append method inside the actionlistener.
At this point I'm not sure what to do. I'm sure its something minor, but I just can't figure it out. Any help please?

A:

I'm going to first present working code based for the addActionListener in constructor case, I took the liberty of introducing missing fields.
public class ServerGUI {

    private final JButton startServer = new JButton("Start server");
    int port;
    private JTextField portInput = new JTextField();
    private JTextArea eventsLog = new JTextArea();

    public ServerGUI(){
        startServer.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    port = Integer.parseInt(portInput.getText());
                }
                catch(NumberFormatException nfe){
                    appendEventsLog("");
                }
            }
        });
    }

    private void appendEventsLog(String msg) {
        String text = eventsLog.getText();
        eventsLog.setText(text + "\n" + msg);
    }
}

The problem here was that appendEventsLog is not a member of JTextArea but a member of ServerGUI.
For the second case assigning ActionListener to JButton outside the constructor you have to use static code block or what I prefer initialise method
public class ServerGUI {

    private final JButton startServer = new JButton("Start server");
    int port;
    private JTextField portInput = new JTextField();
    private JTextArea eventsLog = new JTextArea();

    public ServerGUI(){
        initalise();
    }

    private void initalise() {
        startServer.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    port = Integer.parseInt(portInput.getText());
                }
                catch(NumberFormatException nfe){
                    appendEventsLog("");
                }
            }
        });
    }

    private void appendEventsLog(String msg) {
        String text = eventsLog.getText();
        eventsLog.setText(text + "\n" + msg);
    }
}

