Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Using Netbeans IDE I need help getting started with the parts of the code b

ID: 3750935 • Letter: J

Question

Java

Using Netbeans IDE

I need help getting started with the parts of the code below that are in bold.

Declare 3 objects with Socket, PrintWriter and BufferedReader, so they are visible throughout the program.

import java.io.*;

import java.net.*;

public class kkClient extends javax.swing.JFrame {

/**
* Creates new form kkClient
*/
public kkClient() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")   
private void initComponents();

private void connectActionPerformed(java.awt.event.ActionEvent evt) {   
/* If client is disconnected button should say "connect" (which it does now) if client is connected the button should say "disconnect"(Having trouble with this part). getText() method is used to get host IP and port number. When discconect is clicked the socket and I/O objects should be closed and the button should revert back to "Connect."

try
{
Socket mySocket = new Socket(serverName.getText(),
Integer.parseInt(port.getText()));
//System.out.println(mySocket.getPort());
output.append("Connected to Server ");
}

private void sendActionPerformed(java.awt.event.ActionEvent evt) {
/* If for some reason you got disconnected, you won't be able to send messages to the server. This exception must be dealt with. Display messages you sent in the text area the follwoing format: "Client ........", write the message to the socket (printwriterObj.println() method), and wait for the server's response (bufferedreaderObj.readLine() method.) */

}
}   

private void messageActionPerformed(java.awt.event.ActionEvent evt) {   
// This is the user input message to be sent to the server from client.
}
catch (IOException e)
{
output.append("Connection error!! ");
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(kkClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(kkClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(kkClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(kkClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new kkClient().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton connect;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField message;
private javax.swing.JTextArea output;
private javax.swing.JTextField port;
private javax.swing.JButton send;
private javax.swing.JTextField serverName;
// End of variables declaration
}

  

Explanation / Answer

import java.net.Socket; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStreamReader; public class kkClient extends javax.swing.JFrame { public kkClient() { initComponents(); } private void initComponents() private void connectActionPerformed(java.awt.event.ActionEvent evt) { { try { Socket mySocket = new Socket(serverName.getText(), Integer.parseInt(port.getText())); //System.out.println(mySocket.getPort()); output.append("Connected to Server "); } setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); textArea.setEditable(false); textArea.setColumns(20); textArea.setRows(5); textArea.setText("click on connect button"); textArea.setWrapStyleWord(true); textArea.setCaretPosition(textArea.getDocument().getLength()); scrollPane.setViewportView(textArea); btnConnect.setText("Connect"); btnConnect.setActionCommand("btnConnect"); btnConnect.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { btnConnectMouseClicked(evt); } }); btnDisconnect.setText("Disconnect"); btnDisconnect.setActionCommand("btnDisconnect"); btnDisconnect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDisconnectActionPerformed(evt); } }); lblShowStatus.setText("Disconnected"); txtInput.setToolTipText(""); txtInput.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtInputActionPerformed(evt); } }); private void btnConnectMouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: lblShowStatus.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N lblShowStatus.setForeground(new java.awt.Color(0, 204, 51)); lblShowStatus.setText("Connected"); } private void btnDisconnectActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: lblShowStatus.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N lblShowStatus.setForeground(new java.awt.Color(255, 51, 51)); lblShowStatus.setText("Disconnected"); } private void txtInputActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } public static void main(String args[]) { try { client = new Socket(host, port); System.out.println("please Connected "); DataInputStream in = new DataInputStream(client.getInputStream()); DataOutputStream out = new DataOutputStream(client.getOutputStream()); System.out.println("Before setting text area"); updateTextArea("trying to update"); do { // HANDLE INPUT PART HERE serverInput = in.readUTF(); if(serverInput != null) { System.out.println("Reached here"); System.out.println(serverInput); updateTextArea(serverInput); } } while(!input.equals("/close")); System.out.println("Program closed"); } catch(Exception exc) { System.err.println(exc.getMessage()); } } private static void updateTextArea(String temp) { textArea.setText(textArea.getText() + " " + temp + " "); textArea.setCaretPosition(textArea.getDocument().getLength()); } private javax.swing.JButton btnConnect; private javax.swing.JButton btnDisconnect; private javax.swing.JLabel lblShowStatus; private javax.swing.JLabel lblStatus; private javax.swing.JScrollPane scrollPane; private static javax.swing.JTextArea textArea; private javax.swing.JTextField txtInput; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote