0

I'm trying make a simple program, which can showing what user selected on radio button and display the result with .setText, But they only can choose 1 choice (like male or female choice). And another program with CheckBox, same result. user check their choice and the result showed on text area.

Here's the code:

public class Pilihan extends javax.swing.JFrame {

/**
 * Creates new form Pilihan
 */

public Pilihan() {
    initComponents();
}

private void tampilkanWarna(){
    StringBuffer warna =  new StringBuffer();
        if(chkBiru.isSelected()){
            warna.append(chkBiru.getText() + " ");
        }
        if(chkHijau.isSelected()){
            warna.append(chkHijau.getText() + " ");
        }
        if(chkKuning.isSelected()){
            warna.append(chkKuning.getText() + " ");
        }
        if(chkMerah.isSelected()){
            warna.append(chkMerah.getText() + " ");
        }
        txtWarna.setText(warna.toString());

private void radioBerwarnaActionPerformed(java.awt.event.ActionEvent evt) {                                              
    if(radioBerwarna.isSelected()){
        lblTipeWarna.setText("Tipe Warna : "+radioBerwarna.getText());
    }
}                                             

private void radioTransparanActionPerformed(java.awt.event.ActionEvent evt) {                                                
    if(radioTransparan.isSelected()){
        lblTipeWarna.setText("Tipe Warna : "+radioTransparan.getText());
    }
}                                               

private void chkHijauActionPerformed(java.awt.event.ActionEvent evt) {                                         
    tampilkanWarna();
}                                        

private void chkBiruActionPerformed(java.awt.event.ActionEvent evt) {                                        
    tampilkanWarna();
}                                       

private void chkMerahActionPerformed(java.awt.event.ActionEvent evt) {                                         
    tampilkanWarna();
}                                        

private void chkKuningActionPerformed(java.awt.event.ActionEvent evt) {                                          
    tampilkanWarna();
}

What I got is, When I clicked radioBerwarna (radio button), the result is true, it's shows me "Tipe Warna : Berwarna". And then I click radioTransparan, the result is also true, "Tipe Warna : Transparan". But the radioBerwarna Button still selected, which means GUI shows me that I selected 2 option, radioBerwarna and radioTransparan.

I want to set the radio Button only can be selected 1 choice. Can you show me what I miss? Thank you.

bnrfly
  • 155
  • 1
  • 2
  • 11
  • 1
    I can't tell from your example, but are your radio buttons in the same button group? – John Giotta Feb 14 '16 at 17:17
  • 3
    I think you are looking for a [ButtonGroup](https://docs.oracle.com/javase/7/docs/api/javax/swing/ButtonGroup.html). – Gliderman Feb 14 '16 at 17:18
  • to @JohnGiotta : yes, my radio buttons in the same ButtonGroup, I set the post-creation code in each radio button this line : `groupTipeWarna.add(radioBerwarna);` but it's still like that. – bnrfly Feb 14 '16 at 17:24

0 Answers0