0

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();


    }

}
Masoud
  • 25
  • 6
  • it doesn't add the name to the JList, an error occurs in this line: btnConnect.addActionListener(new handler(Name)); it says actual and argument lists differ in lenghts – Masoud Dec 16 '14 at 09:10
  • When listModel changes, call clientList.setModel(listModel) again. – Héctor Dec 16 '14 at 09:19
  • what about the previous error I mentioned? I mean this error: actual and argument lists differ in lenghts I think this error must be solved first! – Masoud Dec 16 '14 at 09:29

1 Answers1

1

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.

kiheru
  • 6,588
  • 25
  • 31
  • thanks alot for your help, now the error for that line is solved but the same error for the following line appears: btnConnect.addActionListener(new handler()); I have implemented the MouseListener already, but I didn't display them in the code shown here in order to be easier to understand! – Masoud Dec 16 '14 at 09:44
  • @Masoud See the line about needing two constructors (if you actually intend to have one listener where the `Name` is `null`). The other would be obviously `handler() ...`. You did not get an error there before because classes having no constructors defined in the code get given the no-parameter default constructor. – kiheru Dec 16 '14 at 09:47
  • @Masoud Though it would be probably cleaner to have just one constructor anyway, and pass it the `null` explicitly where needed. That would make it clear that you set the name to `null`. – kiheru Dec 16 '14 at 10:01
  • yes it solved this error too, thanks again, but when I run the program this error is displayed in the console: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. sorry if my questions are simple! – Masoud Dec 16 '14 at 10:04
  • @Masoud That can be caused by a lot of things, and *the code you posted*, with the above fixes does not do that, so it's in some other part of your code. But see [here](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) how to debug NullPointerExceptions. – kiheru Dec 16 '14 at 10:10
  • I got where that error came from, and it is all fixed now. i shifted listModel.addElement(Name); to actionPerformed method, then it got fixed thanks alot for sharing your knowledge – Masoud Dec 16 '14 at 10:25