Advertisement
stevennathaniel

Pemilihan jRadiButton Menggunakan If Else Berbasis String

Jan 29th, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.81 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package latihan13;
  7.  
  8.  
  9. import javax.swing.ButtonGroup;
  10.  
  11. import javax.swing.ButtonModel;
  12.  
  13. import java.util.Enumeration;
  14. import javax.swing.JOptionPane;
  15.  
  16. /**
  17.  *
  18.  * @author steven
  19.  */
  20. public class RadioButton4 extends javax.swing.JFrame {
  21.  
  22.     /**
  23.      * Creates new form RadioButton4
  24.      */
  25.     public RadioButton4() {
  26.         initComponents();
  27.     }
  28.  
  29.     /**
  30.      * This method is called from within the constructor to initialize the form.
  31.      * WARNING: Do NOT modify this code. The content of this method is always
  32.      * regenerated by the Form Editor.
  33.      */
  34.     @SuppressWarnings("unchecked")
  35.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  36.     private void initComponents() {
  37.  
  38.         buttonGroup1 = new javax.swing.ButtonGroup();
  39.         jRadioButton1 = new javax.swing.JRadioButton();
  40.         jRadioButton2 = new javax.swing.JRadioButton();
  41.         jRadioButton3 = new javax.swing.JRadioButton();
  42.         jRadioButton4 = new javax.swing.JRadioButton();
  43.         jButton1 = new javax.swing.JButton();
  44.         jTextField1 = new javax.swing.JTextField();
  45.  
  46.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  47.  
  48.         jRadioButton1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  49.         jRadioButton1.setText("Steven Nathaniel");
  50.  
  51.         jRadioButton2.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  52.         jRadioButton2.setText("Yulia Mulyawati");
  53.  
  54.         jRadioButton3.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  55.         jRadioButton3.setText("Noviah D Lestyorini");
  56.  
  57.         jRadioButton4.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  58.         jRadioButton4.setText("Lilis Hendayani");
  59.  
  60.         jButton1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  61.         jButton1.setText("Pilihanku");
  62.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  63.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  64.                 jButton1ActionPerformed(evt);
  65.             }
  66.         });
  67.  
  68.         jTextField1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  69.  
  70.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  71.         getContentPane().setLayout(layout);
  72.         layout.setHorizontalGroup(
  73.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.             .addGroup(layout.createSequentialGroup()
  75.                 .addContainerGap()
  76.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  77.                     .addComponent(jRadioButton1)
  78.                     .addComponent(jRadioButton2)
  79.                     .addComponent(jRadioButton3)
  80.                     .addComponent(jRadioButton4))
  81.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
  82.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  83.                     .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
  84.                     .addComponent(jTextField1))
  85.                 .addContainerGap())
  86.         );
  87.         layout.setVerticalGroup(
  88.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89.             .addGroup(layout.createSequentialGroup()
  90.                 .addContainerGap()
  91.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92.                     .addGroup(layout.createSequentialGroup()
  93.                         .addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
  94.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  95.                         .addComponent(jRadioButton2))
  96.                     .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
  97.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  98.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  99.                     .addComponent(jRadioButton3)
  100.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
  101.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  102.                 .addComponent(jRadioButton4)
  103.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  104.         );
  105.  
  106.         pack();
  107.     }// </editor-fold>                        
  108.  
  109.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  110.         // TODO add your handling code here:
  111.        
  112.        
  113.         buttonGroup1.add(jRadioButton1);
  114.        
  115.         buttonGroup1.add(jRadioButton2);
  116.        
  117.         buttonGroup1.add(jRadioButton3);
  118.        
  119.         buttonGroup1.add(jRadioButton4);
  120.        
  121.        
  122.         String pilihanku = jTextField1.getText();
  123.        
  124.        
  125.         if(pilihanku.equals("Steven Nathaniel")){
  126.    
  127.        
  128.             buttonGroup1.setSelected(jRadioButton1.getModel(), true);
  129.        
  130.        
  131.            
  132.                
  133.                
  134.         }else if (pilihanku.equals("Yulia Mulyawati")){
  135.                        
  136.                        
  137.             buttonGroup1.setSelected(jRadioButton2.getModel(), true);
  138.                        
  139.                        
  140.                        
  141.         }else if (pilihanku.equals("Noviah D Lestyorini")){
  142.            
  143.            
  144.             buttonGroup1.setSelected(jRadioButton3.getModel(), true);
  145.            
  146.            
  147.            
  148.         }else if (pilihanku.equals("Lilis Hendayani")){
  149.            
  150.            
  151.             buttonGroup1.setSelected(jRadioButton4.getModel(), true);
  152.            
  153.            
  154.            
  155.         }else{
  156.            
  157.            
  158.             JOptionPane.showMessageDialog(null, "Maaf Pilihan Tidak tersedia");
  159.            
  160.            
  161.         }
  162.        
  163.        
  164.        
  165.                        
  166.                        
  167.                        
  168.                        
  169.                        
  170.                        
  171.                        
  172.                
  173.                
  174.            
  175.        
  176.        
  177.        
  178.    
  179.        
  180.  
  181.        
  182.        
  183.     }                                        
  184.  
  185.     /**
  186.      * @param args the command line arguments
  187.      */
  188.     public static void main(String args[]) {
  189.         /* Set the Nimbus look and feel */
  190.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  191.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  192.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  193.          */
  194.         try {
  195.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  196.                 if ("Nimbus".equals(info.getName())) {
  197.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  198.                     break;
  199.                 }
  200.             }
  201.         } catch (ClassNotFoundException ex) {
  202.             java.util.logging.Logger.getLogger(RadioButton4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  203.         } catch (InstantiationException ex) {
  204.             java.util.logging.Logger.getLogger(RadioButton4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  205.         } catch (IllegalAccessException ex) {
  206.             java.util.logging.Logger.getLogger(RadioButton4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  207.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  208.             java.util.logging.Logger.getLogger(RadioButton4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  209.         }
  210.         //</editor-fold>
  211.  
  212.         /* Create and display the form */
  213.         java.awt.EventQueue.invokeLater(new Runnable() {
  214.             public void run() {
  215.                 new RadioButton4().setVisible(true);
  216.             }
  217.         });
  218.     }
  219.  
  220.     // Variables declaration - do not modify                    
  221.     private javax.swing.ButtonGroup buttonGroup1;
  222.     private javax.swing.JButton jButton1;
  223.     private javax.swing.JRadioButton jRadioButton1;
  224.     private javax.swing.JRadioButton jRadioButton2;
  225.     private javax.swing.JRadioButton jRadioButton3;
  226.     private javax.swing.JRadioButton jRadioButton4;
  227.     private javax.swing.JTextField jTextField1;
  228.     // End of variables declaration                  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement