Search code examples
javaswingcompiler-errorsscopejbutton

JButton variable cannot be resolved


I am trying to code a JButton where when I click it, the text will change from Connect to Disconnect, however Eclipse gives me an error that says "Connect cannot be resolved." I have tried putting it outside of the try catch loop as well.

btnConnect = new JButton("Connect");
btnConnect.addMouseListener(new MouseAdapter() {

    public void mouseClicked(MouseEvent arg0) {
        try {
            Connect.setText("Disconnect");
            int portNum = 5520;
            String hostAddress = Actual_IP_Address.getText();
            sock = new Socket(hostAddress, portNum);
            writeSock = new PrintWriter ( sock.getOutputStream(), true);
            readSock = new BufferedReader (new InputStreamReader(sock.getInputStream()));
        }
        catch(Exception ex) {
            System.out.println("Error: " + ex);
            sock = null;
        }
    }}
);

Any pointers would be greatly appreciated.


Solution

  • Change Connect.setText("Disconnect"); to btnConnect.setText("Disconnect");.

    Connect is a class that does not exists, that's why Eclipse is giving you a warning.