Radiobutton - Selektionsabfrage

AlexD1979

Erfahrenes Mitglied
Hallo
Ich habe in einer Applikation ein Auswahlfeld mit Radiobuttons, ja oder nein anklickbar. Nun will ich, je nachdem welche Option gewählt ist die selektierte Option auslesen um sie in einer Datenbank speichern zu können. Ich habe die beiden Buttons zu einer ButtonGroup zusammengefasst. z.B. wenn nein gewählt ist soll ein N in die Datenbank wenn Ja gewählt, dann ein Y.. aber ich muss erst mal wissen was selektiert ist

Kurzer Quelltext:
ButtonGroup bg1 = new ButtonGroup();
Box b1 = Box.createHorizontalBox();
JRadioButton rb1 = new JRadioButton("Ja");
JRadioButton rb2 = new JRadioButton("Nein", true);

bg1.add(rb1);
bg1.add(rb2);
b1.add(rb1);
b1.add(rb2);
pan1.add(b1);
 
Servus!

Versuchs mal so:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * JtestFrm1.java
 *
 * Created on 29. April 2003, 18:56
 */

/**
 *
 * @author  Administrator
 */
public class JtestFrm1 extends javax.swing.JFrame implements ActionListener{
    
    /** Creates new form JtestFrm1 */
    public JtestFrm1() {
        initComponents();
        this.setSize(320,240);
        
        JPanel pan = new JPanel();
        pan.setLayout(new FlowLayout());
        ButtonGroup bg = new ButtonGroup();
        for (int i = 0; i<4;i++){
         JRadioButton jrb = new JRadioButton("RadioButton:" +i);
         jrb.addActionListener(this);
         jrb.setActionCommand(jrb.getName());
         bg.add(jrb);
         pan.add(jrb);
        }
        
        this.getContentPane().add(pan,BorderLayout.CENTER);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        pack();
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new JtestFrm1().show();
    }
    
    public void actionPerformed(ActionEvent ae){
        String command = ae.getActionCommand();
        
        if(command.equals("RadioButton:0")){
            System.out.println("RB 0");
        } else if (command.equals("RadioButton:1")){
            System.out.println("RB 1");
        } else if (command.equals("RadioButton:2")){
            System.out.println("RB 2");
        } else if (command.equals("RadioButton:3")){
            System.out.println("RB 3");
        }
        
            
        
        
    }
    
    // Variables declaration - do not modify
    // End of variables declaration
    
}

Gruss Tom
 
Ja danke das funktioniert inzwischen dank deiner Hilfe aber ein ganz andres Problem..
wie kann ich durch einen Methodenaufruf die Selektion ändern?
z.B. Stanard ist "Nein" als True gewählt
nun will ich durch buttonklick den Status meinetwegen auf "Ja" ändern.. wenn ich rb1.setSelected(true) mache dann ändert sich nichts, warum?
 
Servus!

Hier ist wieder das Beispiel von oben mit der gewünschten Selektionsfähigkeit ...

Gruss Tom

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


/*
 * JtestFrm1.java
 *
 * Created on 29. April 2003, 18:56
 */

/**
 *
 * @author  Administrator
 */
public class JtestFrm1 extends javax.swing.JFrame implements ActionListener{
    
    ButtonGroup bg;

    
    /** Creates new form JtestFrm1 */
    public JtestFrm1() {
        initComponents();
        this.setSize(320,240);
        
        JPanel pan = new JPanel();
        pan.setLayout(new FlowLayout());
        bg = new ButtonGroup();
        JButton jb1 = new JButton("set_RB3_true");
        jb1.addActionListener(this);
        
        for (int i = 0; i<4;i++){
         JRadioButton jrb = new JRadioButton("RadioButton:" +i);
         jrb.addActionListener(this);
         jrb.setActionCommand(jrb.getName());
         bg.add(jrb);
         pan.add(jrb);
         pan.add(jb1);
        }
        
        this.getContentPane().add(pan,BorderLayout.CENTER);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        pack();
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new JtestFrm1().show();
    }
    
    public void actionPerformed(ActionEvent ae){
        String command = ae.getActionCommand();
        
        if(command.equals("RadioButton:0")){
            System.out.println("RB 0");
        } else if (command.equals("RadioButton:1")){
            System.out.println("RB 1");
        } else if (command.equals("RadioButton:2")){
            System.out.println("RB 2");
        } else if (command.equals("RadioButton:3")){
            System.out.println("RB 3");
        }else if(command.equals("set_RB3_true")){
            System.out.println("Setze RB3 auf true!");

            //NEU          
            //referenzen aller Buttons besorgen;
            
            JRadioButton jrb_temp;
            
            Enumeration e = bg.getElements();
            
            while(e.hasMoreElements()){
                if( (jrb_temp = ((JRadioButton)e.nextElement())).getActionCommand().equals("RadioButton:3") ){
                              jrb_temp.setSelected(true);
                }
                
            }
            
            
            
        }
        
            
        
        
    }
    
    // Variables declaration - do not modify
    // End of variables declaration
    
}
 
Zurück