1

My code is this:

public class clientsocket extends JFrame implements Runnable {

    public JTextArea chatbox;

    public clientsocket() {
        getContentPane().setLayout(null);
        chatbox = new JTextArea();
        chatbox.setBounds(20, 36, 404, 187);
        getContentPane().add(chatbox);
    }

    void checkconnection() {

        clientsocket obj1 = new clientsocket();
        Thread t1 = new Thread(obj1);
        t1.start();
    }

    public void run() {
        System.out.println("Step4");
        String responseLine;
        try {
            while ((responseLine = br.readLine()) != null) {
                System.out.println(responseLine);
                ipaddr.setText(responseLine);
                if (responseLine.indexOf("*** Bye") != -1) {
                    break;
                }
            }
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }

    }

}

All the System.out.println() work properly in run() method. But I am unable to change the contents of the chatbox from within the run() method.

Why am I not able to access the chatbox in run() method?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. .. – Andrew Thompson Jul 03 '14 at 23:37
  • .. 3) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). .. – Andrew Thompson Jul 03 '14 at 23:39
  • .. 4) Use a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Jul 03 '14 at 23:39
  • In you code `ipaddr` is undefiend – MadProgrammer Jul 04 '14 at 00:03

1 Answers1

2

This is how you might set up the GUI.

enter image description here

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class clientsocket {

    public JTextArea chatbox;
    JPanel ui;

    public clientsocket() {
        ui = new JPanel(new BorderLayout(3, 3));
        // pad around the GUI
        ui.setBorder(new EmptyBorder(30, 20, 30, 20));

        // suggest a size in rows x cols
        chatbox = new JTextArea(12, 36);
        //chatbox.setBounds(20, 36, 404, 187); // set a border/cols instead
        ui.add(new JScrollPane(chatbox)); // default is CENTER
        getContentPane().add(ui);
    }

    public JComponent getUi() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            public void run() {
                clientsocket cs = new clientsocket();
                JFrame f = new JFrame("Chat Client");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                f.add(cs.getUi());

                f.pack(); // sets the GUI the smallest it can be to display the content
                f.setMinimumSize(f.getSize()); // enforce a mimimum size
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);
                f.setVisible(true); // Should be last.
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433