Question

In my project I want to select a file in server and send to clients in java swing. In this I must send to the client which I like in the radio button for clients. But I don't know how to check the radio button is clicked in the send button. As I must check the radio button is clicked in send button method.

My radio button code

jRadioButton1.setText("One");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
  public void actionPerformed(java.awt.event.ActionEvent evt) {
    jRadioButton1ActionPerformed(evt);
  }
});
}


private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  JOptionPane.showMessageDialog(null, "You Selected Button 1");
}

For send button......

    jButton1.setText("SEND");
    jButton1.addActionListener(new java.awt.event.ActionListener() 
    {
        public void actionPerformed(java.awt.event.ActionEvent evt) 
        {
            jButton1ActionPerformed(evt);
        }
    });

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {

    jRadioButton1ActionPerformed(evt);
    Object k=evt.getSource();
    System.out.println(k);
}

How to check the radiobutton is clicked in send action method?

Was it helpful?

Solution

Assuming your different UI components are in the same class and declared as instance members, you can simply reference them anywhere within your class. So you could write:

jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        if (jRadioButton1.isSelected()) {
            System.out.println("radio button selected");
        } else {
            System.out.println("radio button NOT selected");
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top