adding users to JList in chatroom GUI

369 views Asked by At

I'm trying to write a simple chatroom GUI using java, including a Jlist to show the online users. first a user chooses a display name so that the name will be displayed in the JList. here is the code:

(the problem is only inside the createServer() method which sends Name as an argument to the handler constructor, in order to display it in the JList!)

public class GUI{
    private JFrame frame;
    private JButton btnSend, btnConnect;
    private JTextArea txtChat;
    private JTextField fldText, fldName;
    private JList clientList;
    private DefaultListModel listModel;
    private JScrollPane sc, scClients;
    private JPanel jpS2All, jpS2Client, jpS2Text;
    private String Name;

    class handler implements ActionListener, MouseListener{
    private String Name = null;

        void handler(String Name) {
            this.Name = Name;

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            chatroom();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            fldName.setText("");
            listModel.addElement(Name);
        }
    }

    public void creatServer() {
        frame = new JFrame("login");
        frame.setBounds(50, 50, 300, 200);
        btnConnect = new JButton("connect");
        frame.add(btnConnect, BorderLayout.EAST);
        fldName = new JTextField("enter your name");
        fldName.addMouseListener(new handler());
        Name = fldName.getText();
        btnConnect.addActionListener(new handler(Name));
        frame.add(fldName, BorderLayout.CENTER);

        frame.setVisible(true);
        }


    public void chatroom() {
        frame = new JFrame("online friends");
        frame.setBounds(100, 100, 400, 400);
        jpS2All = new JPanel();
        txtChat = new JTextArea();
        txtChat.setRows(25);
        txtChat.setColumns(25);
        txtChat.setEditable(false);
        sc = new JScrollPane(txtChat);
        jpS2All.add(sc);
        frame.add(jpS2All, BorderLayout.WEST);


        ////////////////////////
        jpS2Text = new JPanel();
        fldText = new JTextField();
        fldText.setColumns(34);
        fldText.setHorizontalAlignment(JTextField.RIGHT);

        jpS2Text.add(fldText);
        frame.add(jpS2Text, BorderLayout.SOUTH);

        /////////
        jpS2Client = new JPanel();

        listModel = new DefaultListModel();
        clientList = new JList(listModel);
        clientList.setFixedCellHeight(14);
        clientList.setFixedCellWidth(100);
        scClients = new JScrollPane(clientList);

        frame.add(jpS2Client.add(scClients), BorderLayout.EAST);
        /////////
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.pack();

    }

}

and finally in the main method:

public class Chat {

    public static void main(String[] args) {
        GUI gui = new GUI();
        gui.creatServer();


    }

}
1

There are 1 answers

6
kiheru On BEST ANSWER

This:

void handler(String Name) {
    ...

Should be

handler(String Name)  {
    ...

To be a constructor, instead of a method. Also, you create a handler with two different parameter lists: empty, and the string. You need a constructor for both.

By the way, the code would be a lot easier to follow if it used the usual java naming conventions. Now they are inverted in a couple of places. Also, MouseListener has more methods that need to be implemented - consider extending MouseAdapter. Finally, you should create and access swing components only in the event dispatch thread.