Question: How can I use the same jButton to connect and disconnect to a server using Java? I currently have: jButton1.setText(connect); jButton1.addActionListener(new java.awt.event.ActionListener() { public void

How can I use the same jButton to connect and disconnect to a server using Java? I currently have:

jButton1.setText("connect"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });

try { Socket mySocket = new Socket(serverName.getText(), Integer.parseInt(port.getText()));

//System.out.println(mysocket.getPort()); output.append("Connected to Server "); jButton1.setText("Disconnect"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); }}); } catch (IOException e) { output.append("Connection Error!!! "); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!