ljukk

urna/pouzitie combo boxu a TextArea

May 23rd, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.85 KB | None | 0 0
  1. import javax.swing.DefaultComboBoxModel;
  2. import javax.swing.JComboBox;
  3.  
  4. /*
  5.  * To change this license header, choose License Headers in Project Properties.
  6.  * To change this template file, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9. /**
  10.  *
  11.  * @author sused
  12.  */
  13. public class HlasovaciFormular extends javax.swing.JFrame {
  14.  
  15.     /**
  16.      * Creates new form HlasovaciFormular
  17.      */
  18.     private Urna u;
  19.     private DefaultComboBoxModel moznosti;
  20.  
  21.     public HlasovaciFormular() {
  22.         initComponents();
  23.         u = new Urna();
  24.         moznosti = new DefaultComboBoxModel();
  25.         for (int i = 0; i < u.getPocet(); i++) {
  26.             moznosti.addElement(u.getNazov(i));
  27.         }
  28.         jComboBox1.setModel(moznosti);
  29.     }
  30.  
  31.     /**
  32.      * This method is called from within the constructor to initialize the form.
  33.      * WARNING: Do NOT modify this code. The content of this method is always
  34.      * regenerated by the Form Editor.
  35.      */
  36.     @SuppressWarnings("unchecked")
  37.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  38.     private void initComponents() {
  39.  
  40.         jButton1 = new javax.swing.JButton();
  41.         jLabel1 = new javax.swing.JLabel();
  42.         jButton2 = new javax.swing.JButton();
  43.         jScrollPane1 = new javax.swing.JScrollPane();
  44.         jTextArea1 = new javax.swing.JTextArea();
  45.         jComboBox1 = new javax.swing.JComboBox<>();
  46.  
  47.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  48.  
  49.         jButton1.setText("Hlasuj");
  50.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  51.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  52.                 jButton1ActionPerformed(evt);
  53.             }
  54.         });
  55.  
  56.         jLabel1.setText("Cislo moznosti");
  57.  
  58.         jButton2.setText("Vysledok");
  59.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  60.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  61.                 jButton2ActionPerformed(evt);
  62.             }
  63.         });
  64.  
  65.         jTextArea1.setColumns(20);
  66.         jTextArea1.setRows(5);
  67.         jScrollPane1.setViewportView(jTextArea1);
  68.  
  69.         jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
  70.  
  71.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  72.         getContentPane().setLayout(layout);
  73.         layout.setHorizontalGroup(
  74.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75.             .addGroup(layout.createSequentialGroup()
  76.                 .addContainerGap()
  77.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  78.                     .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
  79.                     .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  80.                 .addGap(21, 21, 21)
  81.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.                     .addGroup(layout.createSequentialGroup()
  83.                         .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  84.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  85.                         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
  86.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
  87.                 .addContainerGap(128, Short.MAX_VALUE))
  88.         );
  89.         layout.setVerticalGroup(
  90.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  91.             .addGroup(layout.createSequentialGroup()
  92.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  93.                     .addGroup(layout.createSequentialGroup()
  94.                         .addComponent(jLabel1)
  95.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  96.                         .addComponent(jButton1))
  97.                     .addGroup(layout.createSequentialGroup()
  98.                         .addContainerGap()
  99.                         .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  100.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  101.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  102.                     .addGroup(layout.createSequentialGroup()
  103.                         .addComponent(jButton2)
  104.                         .addGap(0, 0, Short.MAX_VALUE))
  105.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE))
  106.                 .addContainerGap())
  107.         );
  108.  
  109.         pack();
  110.     }// </editor-fold>                        
  111.  
  112.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  113.         // TODO add your handling code here:
  114.         int cisloHlasu = jComboBox1.getSelectedIndex();
  115.         if (cisloHlasu > -1) {
  116.             u.zahlasuj(cisloHlasu);
  117.     }                                        
  118.     }
  119.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  120.         // TODO add your handling code here:
  121.         jTextArea1.setText(u.getVysledok());
  122.     }                                        
  123.  
  124.     /**
  125.      * @param args the command line arguments
  126.      */
  127.     public static void main(String args[]) {
  128.         /* Set the Nimbus look and feel */
  129.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  130.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  131.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  132.          */
  133.         try {
  134.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  135.                 if ("Nimbus".equals(info.getName())) {
  136.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  137.                     break;
  138.                 }
  139.             }
  140.         } catch (ClassNotFoundException ex) {
  141.             java.util.logging.Logger.getLogger(HlasovaciFormular.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  142.         } catch (InstantiationException ex) {
  143.             java.util.logging.Logger.getLogger(HlasovaciFormular.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  144.         } catch (IllegalAccessException ex) {
  145.             java.util.logging.Logger.getLogger(HlasovaciFormular.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  146.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  147.             java.util.logging.Logger.getLogger(HlasovaciFormular.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  148.         }
  149.         //</editor-fold>
  150.  
  151.         /* Create and display the form */
  152.         java.awt.EventQueue.invokeLater(new Runnable() {
  153.             public void run() {
  154.                 new HlasovaciFormular().setVisible(true);
  155.             }
  156.         });
  157.     }
  158.  
  159.     // Variables declaration - do not modify                    
  160.     private javax.swing.JButton jButton1;
  161.     private javax.swing.JButton jButton2;
  162.     private javax.swing.JComboBox<String> jComboBox1;
  163.     private javax.swing.JLabel jLabel1;
  164.     private javax.swing.JScrollPane jScrollPane1;
  165.     private javax.swing.JTextArea jTextArea1;
  166.     // End of variables declaration                  
  167. }
Add Comment
Please, Sign In to add comment