javaperformanceswingjradiobuttonitemlistener

Java coding efficiency. JRadioButton objects and ItemListener interaction


I started coding in Java recently. I wanted to code a Window which contains:

Its purpose were like a one-man chat. Write in the TextField, submit button and print it in the TextArea appending to any previous message. Next step, I named 3 Radio Buttons "User 1", "User 2" and "User 3" On their selection they will print: user_x.GetName+(String)message;

My first try was an ActionListener. (This is a prototype):

    ActionListener updateUserTalking = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        JTextField tf = textField; //New message
        JTextArea ta = textArea; //Previous message to track an history
        String str = ta.getText()+"Radio button changed"; //This should print "User_x"
        str = str+tf.setText(str+System.lineSeparator());
    }
};

My second try was an ItemListener. (This is a prototype)

    public void itemStateChanged(ItemEvent e) {
        updateSystemMessage();  

This updateSystemMessage() call this method:

    ItemListener updateSystemMessage = new ItemListener(){
    public void itemStateChanged(ItemEvent e) {
        JTextField tf = textField; //New message
        JTextArea ta = textArea; //Previous message to track an history
        String str = ta.getText()+"RadioButton changed"; //This should print "User_x"
        str = str+tf.setText(str+System.lineSeparator());
    }
};

The latest print a double message. Because the same method is shared. So when selection is changed there are two commutation so this method will be called twice. Here comes my question:

I know I can make one method for each JRadioButton istantiated. I was guessing if there was a way to make an unique method viable. Where the JRadioButton selected give its name as parameter to the ActionListener or ItemListener.

I already tried something like this:

   private void updateSystemMessage() {
    JRadioButton jrb = (JRadioButton)this.bGroup.getSelection();
    this.system =jrb.getText();
}

But it doesn't work because bGroup.getSelection() return a ButtonModel that can't be casted as (JRadioButton). Therefore is there a way like this? Or I have to code one method for each JRadioButton (Who basically do the same thing)?


Solution

  • ActionEvent.getSource() will return the JRadioButton that generated in, so you could

    ActionListener updateUserTalking = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        JTextField tf = textField; //New message
        JTextArea ta = textArea; //Previous message to track an history
        JRadioButton jrb = (JRadioButton) arg0.getSource();
        String str = ta.getText()+"Button "+jrb.getLabel();
        str = str+tf.setText(str+System.lineSeparator());
    }
    

    Or, you get call JRadioButton.setActionCommand("button1") and then use ActionEvent.getActionCommand() to pass a unique string for each button.

    JRadioButton user1 = new JRadioButton("User_1");
    user1.setActionCommand("User_1");
    
    ActionListener updateUserTalking = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        JTextField tf = textField; //New message
        JTextArea ta = textArea; //Previous message to track an history
        String cmd = arg0.getActionCommand();
        String str = ta.getText()+"Button "+cmd;
        str = str+tf.setText(str+System.lineSeparator());
    }
    

    Or, you can use the actionCommand with ButtonModel as well.

    private void updateSystemMessage() {
        String actionCommand = this.bGroup.getSelection().getActionCommand();
        this.system = actionCommand;
    }