Advertisement
ljukk

jLIst

May 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. public class Lekvar extends javax.swing.JFrame {
  2.  DefaultListModel zoznam = new DefaultListModel();
  3.  
  4.     /** Creates new form Lekvar */
  5.     public Lekvar() {
  6.         initComponents();
  7.         jList1.setModel(zoznam);
  8.     }
  9.  
  10.     /** This method is called from within the constructor to
  11.      * initialize the form.
  12.      * WARNING: Do NOT modify this code. The content of this method is
  13.      * always regenerated by the Form Editor.
  14.      */
  15.     @SuppressWarnings("unchecked")
  16.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  17.     private void initComponents() {
  18.  
  19.         jScrollPane1 = new javax.swing.JScrollPane();
  20.         jList1 = new javax.swing.JList();
  21.         jButton1 = new javax.swing.JButton();
  22.         jTextField1 = new javax.swing.JTextField();
  23.         jButton2 = new javax.swing.JButton();
  24.  
  25.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  26.         setTitle("Čakáreň");
  27.  
  28.         jList1.setModel(new javax.swing.AbstractListModel() {
  29.             String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
  30.             public int getSize() { return strings.length; }
  31.             public Object getElementAt(int i) { return strings[i]; }
  32.         });
  33.         jScrollPane1.setViewportView(jList1);
  34.  
  35.         jButton1.setText("Pridaj");
  36.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  37.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  38.                 jButton1ActionPerformed(evt);
  39.             }
  40.         });
  41.  
  42.         jButton2.setText("Vymaž aktuálny");
  43.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  44.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  45.                 jButton2ActionPerformed(evt);
  46.             }
  47.         });
  48.  
  49.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  50.         getContentPane().setLayout(layout);
  51.         layout.setHorizontalGroup(
  52.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  53.             .addGroup(layout.createSequentialGroup()
  54.                 .addGap(25, 25, 25)
  55.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  56.                     .addGroup(layout.createSequentialGroup()
  57.                         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
  58.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  59.                         .addComponent(jButton1))
  60.                     .addGroup(layout.createSequentialGroup()
  61.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
  62.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  63.                         .addComponent(jButton2)))
  64.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  65.         );
  66.         layout.setVerticalGroup(
  67.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  68.             .addGroup(layout.createSequentialGroup()
  69.                 .addGap(22, 22, 22)
  70.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  71.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
  72.                     .addComponent(jButton2))
  73.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  74.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  75.                     .addComponent(jButton1)
  76.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  77.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  78.         );
  79.  
  80.         pack();
  81.     }// </editor-fold>                        
  82.  
  83. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  84.   // zistim pocet prvkov v zozname
  85.   int pocet = zoznam.size();
  86.   // pridam hodnotu z textoveho pola na koniec zoznamu
  87.   zoznam.insertElementAt(jTextField1.getText(),pocet);
  88.   // textove pole vyprazdnim
  89.   jTextField1.setText("");
  90. }                                        
  91.  
  92. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  93.  // zistim index prvku, na ktorom som nastaveny
  94.  int index = jList1.getSelectedIndex();
  95.  // ak nie som na prazdnom tak mam hodnotu 0 a viac
  96.  if (index>-1)
  97.   zoznam.remove(index); // riadok vymazem
  98.  
  99. }                                        
  100.  
  101.     /**
  102.     * @param args the command line arguments
  103.     */
  104.     public static void main(String args[]) {
  105.         java.awt.EventQueue.invokeLater(new Runnable() {
  106.             public void run() {
  107.                 new Lekvar().setVisible(true);
  108.             }
  109.         });
  110.     }
  111.  
  112.     // Variables declaration - do not modify                    
  113.     private javax.swing.JButton jButton1;
  114.     private javax.swing.JButton jButton2;
  115.     private javax.swing.JList jList1;
  116.     private javax.swing.JScrollPane jScrollPane1;
  117.     private javax.swing.JTextField jTextField1;
  118.     // End of variables declaration                  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement