JRadioButton

Es un componente que nos permite estar seleccionado o deseleccionado y utiliza un buttonGroup como objecto para ajuntar los jradioButton para que hayan solo uno seleccionado esto se va trabajando con eventos

isselected(); verifica si el radioButtton ha sido seleccionado retorna true, false
setSelected(); selecionado el radioButton y deselecciona el otro que haya sido seleccionado esto es porque esta trabajando con buttonGroupo que esta agrupado

PARA MAS INFORMACION VISITAR LA PAGINA DE LA API

Un ejemplo de la utilizacion de radioButton y otros componentes , el programa consiste en un espejo que si se selecciona en una parte el otro tambien sera seleccionado

package desarrolloejemplos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

/**
 *
 * @author USUARIO
 */
public class espejoEvento extends javax.swing.JFrame {

    /**
     * Creates new form espejoEvento
     */
    public espejoEvento() {
        initComponents();
        setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        ButtonGroup grupo1 = new ButtonGroup();
        panel.setEnabled(false);
        grupo1.add(opcion1);
        grupo1.add(opcion2);
        grupo1.add(opcion3);

        opcion1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                opcion7.setSelected(true);
            }
        });
        opcion2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                opcion8.setSelected(true);
            }
        });
        opcion3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                opcion9.setSelected(true);
            }
        });

        opcion4.addActionListener(new ActionListener() {
            boolean verifica = false;

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (opcion4.isSelected()) {
                    opcion10.setSelected(true);
                } else if (!opcion4.isSelected()) {
                    opcion10.setSelected(false);
                }
            }
        });

        texto1.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent ke) {
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                texto2.setText(texto1.getText());
            }

            @Override
            public void keyReleased(KeyEvent ke) {
            }
        }
        );

    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                       
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        opcion1 = new javax.swing.JRadioButton();
        opcion2 = new javax.swing.JRadioButton();
        opcion3 = new javax.swing.JRadioButton();
        opcion4 = new javax.swing.JCheckBox();
        opcion5 = new javax.swing.JCheckBox();
        opcion6 = new javax.swing.JCheckBox();
        texto1 = new javax.swing.JTextField();
        combobox = new javax.swing.JComboBox<>();
        spiner = new javax.swing.JSpinner();
        panel = new javax.swing.JPanel();
        opcion7 = new javax.swing.JRadioButton();
        opcion8 = new javax.swing.JRadioButton();
        opcion9 = new javax.swing.JRadioButton();
        opcion10 = new javax.swing.JCheckBox();
        opcion11 = new javax.swing.JCheckBox();
        opcion12 = new javax.swing.JCheckBox();
        texto2 = new javax.swing.JTextField();
        combobox1 = new javax.swing.JComboBox<>();
        spiner1 = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Original"));

        opcion1.setText("opcion 1");

        opcion2.setText("opcion 2");

        opcion3.setText("opcion 3");

        opcion4.setText("opcion 4");

        opcion5.setText("opcion 5");

        opcion6.setText("opcion 6");

        combobox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        combobox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                comboboxMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                comboboxMouseEntered(evt);
            }
        });

        spiner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinerStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(opcion1)
                    .addComponent(opcion2)
                    .addComponent(opcion3))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(opcion6)
                        .addGap(18, 18, 18)
                        .addComponent(spiner))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(opcion5)
                        .addGap(18, 18, 18)
                        .addComponent(combobox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(opcion4)
                        .addGap(18, 18, 18)
                        .addComponent(texto1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opcion1)
                    .addComponent(opcion4)
                    .addComponent(texto1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opcion2)
                    .addComponent(opcion5)
                    .addComponent(combobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opcion3)
                    .addComponent(opcion6)
                    .addComponent(spiner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        panel.setBorder(javax.swing.BorderFactory.createTitledBorder("Espejo"));

        buttonGroup1.add(opcion7);
        opcion7.setText("opcion 1");

        buttonGroup1.add(opcion8);
        opcion8.setText("opcion 2");

        buttonGroup1.add(opcion9);
        opcion9.setText("opcion 3");

        opcion10.setText("opcion 4");

        opcion11.setText("opcion 5");

        opcion12.setText("opcion 6");

        combobox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(opcion7)
                    .addComponent(opcion8)
                    .addComponent(opcion9))
                .addGap(10, 10, 10)
                .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panelLayout.createSequentialGroup()
                        .addComponent(opcion12)
                        .addGap(18, 18, 18)
                        .addComponent(spiner1))
                    .addGroup(panelLayout.createSequentialGroup()
                        .addComponent(opcion11)
                        .addGap(18, 18, 18)
                        .addComponent(combobox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(panelLayout.createSequentialGroup()
                        .addComponent(opcion10)
                        .addGap(18, 18, 18)
                        .addComponent(texto2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opcion7)
                    .addComponent(opcion10)
                    .addComponent(texto2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opcion8)
                    .addComponent(opcion11)
                    .addComponent(combobox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opcion9)
                    .addComponent(opcion12)
                    .addComponent(spiner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(150, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                     

    private void comboboxMouseClicked(java.awt.event.MouseEvent evt) {                                   


    }                                 

    private void comboboxMouseEntered(java.awt.event.MouseEvent evt) {                                   
        combobox1.setSelectedIndex(combobox.getSelectedIndex());
    }                                 

    private void spinerStateChanged(javax.swing.event.ChangeEvent evt) {                                 
        spiner1.setValue((Integer) spiner.getValue());
    }                               

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(espejoEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(espejoEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(espejoEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(espejoEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new espejoEvento().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                 
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox<String> combobox;
    private javax.swing.JComboBox<String> combobox1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton opcion1;
    private javax.swing.JCheckBox opcion10;
    private javax.swing.JCheckBox opcion11;
    private javax.swing.JCheckBox opcion12;
    private javax.swing.JRadioButton opcion2;
    private javax.swing.JRadioButton opcion3;
    private javax.swing.JCheckBox opcion4;
    private javax.swing.JCheckBox opcion5;
    private javax.swing.JCheckBox opcion6;
    private javax.swing.JRadioButton opcion7;
    private javax.swing.JRadioButton opcion8;
    private javax.swing.JRadioButton opcion9;
    private javax.swing.JPanel panel;
    private javax.swing.JSpinner spiner;
    private javax.swing.JSpinner spiner1;
    private javax.swing.JTextField texto1;
    private javax.swing.JTextField texto2;
    // End of variables declaration               


}

0 comentarios:

Publicar un comentario

BTemplates.com

Buscar este blog

Archivo del Blog

Con tecnología de Blogger.