I have one project named P2PTicTac. I have one file named layout.java. and one with CLient.java. I have made simple GridView in layout file with 9 buttons in 3*3 Grid.

I have run two threads in client.java. One for reading the data from server and one for writing data to server. I have actionListeners in Layout file. When I click on any button in layout ,
then I want to send one number to server using the thread which I run in client.java. And then server will send me a number which I will use to update my GUI in layout.java.

I have made both the files completely but I don't know how to give data to the thread so that I could send to server and how to update GUI when I get response from server in write thread. I hope I am clear. I know thread is a separate entity. But How can I do my task? I don't want code. I want logic how to do this thing?

P.S I have used AWT for making the GUI and I used simple THREAD API of Java to run two threads in the CLient.java class.

CLient.Java

sReadThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    Scanner scanner = new Scanner(System.in);
                    System.out.println(describeUserCommands());
                    while (true) {
//                      System.out.println("User: ");
                        String userInput = scanner.nextLine();
                        if (userInput.equalsIgnoreCase("help"))
                            System.out.println(describeUserCommands());
                        else if (userInput.startsWith("bye")) {
                            break;
                        } else {
                            cOut.println(userInput);
                            //System.out.println("Command sent: " + userInput);
                        }
                    }
                    try {
                        scanner.close();
                        System.out.println("User closed connection!!");
                        cOut.close();
                        sWriteThread.interrupt();
                        cIn.close();
                        mSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

            sWriteThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    String fromServer = null;
                    try {
                        while ((fromServer = cIn.readLine()) != null) {
                            System.out.println(fromServer);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            sReadThread.start();
            sWriteThread.start();

Layout.java

public First() {
        mainFrame = new Frame();
        mainFrame.setLayout(new GridLayout(0, 3));
        mainFrame.setSize(400, 400);
        Button button = new Button();
        button.setActionCommand("button 1");
        button.addActionListener(new ButtonClassListener());
        mainFrame.add(button);
        mainFrame.add(new Button());
        mainFrame.add(new Button());
        mainFrame.add(new Button());
        mainFrame.add(new Button());
        mainFrame.add(new Button());
        mainFrame.add(new Button());
        mainFrame.add(new Button());
        mainFrame.add(new Button());
        Button button2 = new Button("Exit");
        button.setActionCommand("Exit");
        button2.addActionListener(new ButtonClassListener());
        mainFrame.add(button2);
        mainFrame.setVisible(true);

    }

    public class ButtonClassListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if(command.equals("button 1")) {
                Button b = (Button)e.getSource();
                b.setLabel("Cross");
                b.setName("Cross");
                b.setEnabled(false);
            }
            else if(command.equals("Exit")) {
                System.exit(0);
            }
        }

    }
}

Thanks in advance.

Looks like you're trying to adapt a command-line interface, which doesn't fit so well with Swing.
In your action listeners for the GUI you can simply send the appropriate commands to the server - all you need is a reference to the socket output stream. If you're not doing command line input you can discard that whole scanner thread/loop.
When you receive input from the server, update the GUI. You;ll probably be OK doing that in the existing "from server" thread/loop, but ideally you'll store the data and use a SwingUtilities invokeLater to do the actual update.

But, I want to take input from the ActionListeners like when I click on the button I want to send one number to the server. But in CLient, I have SYstem.in as the stream to get input. But I am not using command line to take input now. I want some way to give input to CLient which is waiting for System.in right now. I want the code to make the thread waiting for my input from ActionListeners directly.

Currently, I made the code to take input from Command-line. But , I want to modify it so that when I click on any button, I will send one number to the thread waiting. How to do this?

Like I said:

No command line input -> get rid of that whole thread. You don't need it any more.

In your action listeners just send the data to the server. Simple.

If I will remove that thread, then at the same time if server sends data, will it work? I mean at the time when actionlistner is sending data to server , then server sends some data. then will it wait for the action listener to complete or will it go side-by-side?

Yes it will work. The listener/send is executed on the Swing thread, so it could run concurrently with your receive thread receiving data from the server. That's why it would be best that any GUI update after server input is done via invokeLater.

All you are doing is replacing your thread that waits for console input with the Swing thread that waits for user GUI inoput. It's the same really.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.