Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:59:56+00:00 2026-06-11T17:59:56+00:00

Okay so firstly it does connect to the server so there is no problem

  • 0

Okay so firstly it does connect to the server so there is no problem with the connection, the following code show the connection and sending a message to the server, when I click run it doesn’t open up the JFrame, the window where I can see my tool… Also I’m trying to send what I get from jMessage.getText into ows, but I get an error at this line:

String process = jMessage.getText();

Also I’d like to call osw.write(process); ONLY when I click the bSend button, so how can I use this inside the main method? Please take a look at the code, also remember the first question that my JFrame wont open in a new window showing me my tool, it just prints out the System.out.println at netbeans console:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcpservidor;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 *
 * @author Damage
 */
public class TeKServer extends javax.swing.JFrame {

/**
 * Creates new form TeKServer
 */
public TeKServer() {
    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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    textArea = new javax.swing.JTextArea();
    jMessage = new javax.swing.JTextField();
    bSendMessage = new javax.swing.JButton();
    jLabel2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    jLabel1.setText("Chat Client");

    textArea.setColumns(20);
    textArea.setRows(5);
    textArea.setEnabled(false);
    jScrollPane1.setViewportView(textArea);

    bSendMessage.setText("Send");
    bSendMessage.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            bSendMessageActionPerformed(evt);
        }
    });

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    jLabel2.setText("Message");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(294, 294, 294))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addGap(303, 303, 303))))
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1)
                        .addComponent(jMessage))
                    .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(0, 291, Short.MAX_VALUE)
                    .addComponent(bSendMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(286, 286, 286))))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jLabel2)
            .addGap(3, 3, 3)
            .addComponent(jMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(bSendMessage)
            .addContainerGap())
    );

    pack();
}// </editor-fold>

private void bSendMessageActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:


}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    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(TeKServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TeKServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TeKServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TeKServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    /** Define a host server */
        String host = "192.168.0.8";
        /** Define a port */
        int port = 7777;

        StringBuffer instr = new StringBuffer();
        String TimeStamp;
        System.out.println("SocketClient initialized");
        try {
        /** Obtain an address object of the server */
        InetAddress address = InetAddress.getByName(host);
        /** Establish a socket connetion */
        Socket connection = new Socket(address, port);
        System.out.println("Connection Established!");
        /** Instantiate a BufferedOutputStream object */
        BufferedOutputStream bos = new BufferedOutputStream(connection.
        getOutputStream());

        /** Instantiate an OutputStreamWriter object with the optional character
        * encoding.
        */
        OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
        TimeStamp = new java.util.Date().toString();

        String process = jMessage.getText();
        /** Write across the socket connection and flush the buffer */
        osw.write(process);
        osw.flush();
         /** Instantiate a BufferedInputStream object for reading
        /** Instantiate a BufferedInputStream object for reading
        * incoming socket streams.
        */

        BufferedInputStream bis = new BufferedInputStream(connection.
            getInputStream());
        /**Instantiate an InputStreamReader with the optional
        * character encoding.
        */

        InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

        /**Read the socket's InputStream and append to a StringBuffer */
        int c;
        while ( (c = isr.read()) != 13)
            instr.append( (char) c);

        /** Close the socket connection. */
        connection.close();
        System.out.println(instr);
        }
        catch (IOException f) {
        System.out.println("IOException: " + f);
        }
        catch (Exception g) {
        System.out.println("Exception: " + g);
        }

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new TeKServer().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton bSendMessage;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
public javax.swing.JTextField jMessage;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea textArea;
// End of variables declaration
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:59:58+00:00Added an answer on June 11, 2026 at 5:59 pm

    The line

    String process = jMessage.getText();
    

    causes a compilation error as it is declared in the static main method where jMessage is an instance variable. As you have a lot of code in the main method, I would suggest extracting it out into a launchApp method and call it like so:

    new TeKServer().launchApp();
    

    In the new launchApp() method, you would replace

    java.awt.EventQueue.invokeLater(new Runnable() {
    
        @Override
        public void run() {
            new TeKServer().setVisible(true);
        }
    });
    

    with

    setVisible(true);
    

    I’d like to call osw.write(process); ONLY when I click the bSend button

    If you wanted to do this you could maintain osw as a global member variable but holding sockets open for an indefinite amount of time is a bad approach generally. Would suggest to only open a socket when needed, write & close.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I have the weirdest problem right now. My code is fine! It
Okay, I'm reading about Linux kernel development and there are some code snippets using
Okay, I have the following create action #posts_controller, nested resource under discussions def create
Okay, so I'm working on a java server for an Apps backend, it must
Okay so my question is this. Say I have a simple C++ code: #include
Okay, I have setup up a handler for a div click but for some
Okay, so the answer to my question might not be the problem but here's
Okay,I've been following this tutorial http://coenraets.org/blog/android-samples/androidtutorial/ Basically it gives me exactly what i need
Okay firstly I just started C# so I'm not exactly the most skilled programmer
Okay, I've set up a bit of code which searching for all the pages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.