Including JCheckBox in JFrame as and when a client connects

92 views Asked by At

I am trying to write a server application in java and creating a vector v1 to store all the sockets that hit the server.

Next i made an arraylist where i stored all my Client sockets as objects and then created a JCheckBox array where I loop it to add checkbox to the JPanel controlpanel which was initiated in the class contructor but something seems to be going wrong and i can't see any checkbox.

import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

class server extends JFrame implements ActionListener {

    JTextArea t1;
    PrintWriter pw;
    JButton b1, b2;
    static Vector v1 = new Vector();
    static ArrayList checks = new ArrayList();
    static JPanel controlpanel;

    server() {
        setLayout(new FlowLayout());
        t1 = new JTextArea();
        add(t1);
        b1 = new JButton("Send to All");
        b2 = new JButton("Send");
    //b1.addActionListener(this);
        //b2.addActionListener(this);
        add(b2, BorderLayout.SOUTH);

        add(b1, BorderLayout.NORTH);
        b1.addActionListener(this);
        b2.addActionListener(this);
        controlpanel = new JPanel();
        controlpanel.setVisible(true);
        add(controlpanel);
    }

    public void actionPerformed(ActionEvent ae) {

        String m = t1.getText();
        if (ae.getSource() == b1) {
            try {
                Iterator t2 = v1.iterator();
                while (t2.hasNext()) {
                    Socket s = (Socket) t2.next();
                    pw = new PrintWriter(s.getOutputStream(), true);
                    pw.println(m);
                    pw.flush();
                    System.out.println("send" + m);
                }
            } catch (Exception e) {
            }
        }
        if (ae.getSource() == b2) {
            try {
                Iterator itr = checks.iterator();
                while (itr.hasNext()) {
                    Socket schecked = (Socket) itr.next();
                    pw = new PrintWriter(schecked.getOutputStream(), true);
                    pw.println(m);
                    pw.flush();
                    System.out.println("sent " + m);

                }
            } catch (Exception e) {
            }
        }
        t1.setText("");
    }

    public static void main(String z[]) {

        try {
            new server().setVisible(true);
            ServerSocket s = new ServerSocket(2000);
            while (true) {
                System.out.println("waiting...");
                Socket c = s.accept();
                //          System.out.print(c);
                v1.add(c);
                for (int i = 0; i < v1.size(); i++) {
                    checks.add(v1.get(i));
                    //              checks[i].setText()=v1[i];
                }
                JCheckBox checkbox[] = new JCheckBox[checks.size()];

                for (int i = 0; i < checks.size(); i++) {
                    //  Object o=checks.get(i);
                    checkbox[i] = new JCheckBox();
                    //              checkbox[i].setText()=checks.get(i).name;
                    controlpanel.add(checkbox[i]);
                    checkbox[i].setSelected(true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
1

There are 1 answers

2
Roberto Attias On

As you are adding UI elements after the JFrame has been made visible, you need to call revalidate() on the panel. Also you should never add directly from a thread which is not the Event Dispatch Thread. Finally, it appears every time a new client connects, you're going to re-add all the checkbox you already have added, plus one (i.e. when the first client connects you will have 1 checkbox, when the second connects you'll have 1 + 2 = 3, when the third connects you'll have 1 + 2 + 3 = 6, etc.).

I strongly suggests you mentally run through your code, instruction by instruction, to see what happens. Alternatively, if you're using a decent IDE, you can step through the code in the debugger and inspect the content of controlPanel. If you're not using an IDE... you should.

I don't quite understand what you need check[] and v1[] for but assuming that you do need them, instead of maintaining three arrays you can define a class containing a variable for a check, one for a v1, and one for a checkbox, and maintain a single array of instances of such objects.