peterzig

[JAVA] Tabelki Tomka

Apr 26th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.34 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 pw_05_swingpierwszenieocieniane;
  7.  
  8. import java.awt.List;
  9. import java.util.Vector;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.table.DefaultTableModel;
  12.  
  13. /**
  14.  *
  15.  * @author Tomek
  16.  */
  17. public class NewJFrame extends javax.swing.JFrame {
  18.  
  19.     public static Vector<Pracownik> pracownicy = new Vector<>();
  20.     DefaultTableModel dtm = new DefaultTableModel(new String[]{"ID", "Imię", "Nazwisko", "Płaca podstawowa"}, 0);
  21.     int row;
  22.  
  23.     /**
  24.      * Creates new form NewJFrame
  25.      */
  26.     public NewJFrame() {
  27.         initComponents();
  28.     }
  29.  
  30.     /**
  31.      * This method is called from within the constructor to initialize the form.
  32.      * WARNING: Do NOT modify this code. The content of this method is always
  33.      * regenerated by the Form Editor.
  34.      */
  35.     @SuppressWarnings("unchecked")
  36.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  37.     private void initComponents() {
  38.  
  39.         jDialog1 = new javax.swing.JDialog();
  40.         Panel_Imie = new javax.swing.JTextField();
  41.         Panel_Nazwisko = new javax.swing.JTextField();
  42.         Panel_Placa = new javax.swing.JTextField();
  43.         PanelB_Dodaj = new javax.swing.JButton();
  44.         PanelB_Anuluj = new javax.swing.JButton();
  45.         jScrollPane1 = new javax.swing.JScrollPane();
  46.         jTable1 = new javax.swing.JTable();
  47.         jMenuBar1 = new javax.swing.JMenuBar();
  48.         jMenu1 = new javax.swing.JMenu();
  49.         jMenuItem1 = new javax.swing.JMenuItem();
  50.         jMenuItem2 = new javax.swing.JMenuItem();
  51.         jMenuItem3 = new javax.swing.JMenuItem();
  52.         jMenuItem4 = new javax.swing.JMenuItem();
  53.  
  54.         jDialog1.setLocation(new java.awt.Point(500, 200));
  55.         jDialog1.setMinimumSize(new java.awt.Dimension(500, 500));
  56.  
  57.         Panel_Imie.setText("Imie");
  58.  
  59.         Panel_Nazwisko.setText("Nazwisko");
  60.  
  61.         Panel_Placa.setText("Placa");
  62.  
  63.         PanelB_Dodaj.setText("Dodaj");
  64.         PanelB_Dodaj.addActionListener(new java.awt.event.ActionListener() {
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  66.                 ADD_Accept(evt);
  67.             }
  68.         });
  69.  
  70.         PanelB_Anuluj.setText("Anuluj");
  71.         PanelB_Anuluj.addActionListener(new java.awt.event.ActionListener() {
  72.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  73.                 ADD_Cancel(evt);
  74.             }
  75.         });
  76.  
  77.         javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
  78.         jDialog1.getContentPane().setLayout(jDialog1Layout);
  79.         jDialog1Layout.setHorizontalGroup(
  80.             jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  81.             .addGroup(jDialog1Layout.createSequentialGroup()
  82.                 .addGap(39, 39, 39)
  83.                 .addComponent(Panel_Imie, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
  84.                 .addGap(18, 18, 18)
  85.                 .addComponent(Panel_Nazwisko, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
  86.                 .addGap(18, 18, 18)
  87.                 .addComponent(Panel_Placa, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
  88.                 .addContainerGap(42, Short.MAX_VALUE))
  89.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog1Layout.createSequentialGroup()
  90.                 .addGap(0, 0, Short.MAX_VALUE)
  91.                 .addComponent(PanelB_Dodaj, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
  92.                 .addGap(83, 83, 83)
  93.                 .addComponent(PanelB_Anuluj)
  94.                 .addGap(138, 138, 138))
  95.         );
  96.         jDialog1Layout.setVerticalGroup(
  97.             jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  98.             .addGroup(jDialog1Layout.createSequentialGroup()
  99.                 .addGap(57, 57, 57)
  100.                 .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  101.                     .addComponent(Panel_Imie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  102.                     .addComponent(Panel_Nazwisko, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  103.                     .addComponent(Panel_Placa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  104.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
  105.                 .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  106.                     .addComponent(PanelB_Anuluj)
  107.                     .addComponent(PanelB_Dodaj))
  108.                 .addGap(68, 68, 68))
  109.         );
  110.  
  111.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  112.  
  113.         jTable1.setModel(dtm
  114.         );
  115.         jScrollPane1.setViewportView(jTable1);
  116.  
  117.         jMenu1.setText("Pracownik");
  118.  
  119.         jMenuItem1.setLabel("Dodaj");
  120.         jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
  121.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  122.                 Add(evt);
  123.             }
  124.         });
  125.         jMenu1.add(jMenuItem1);
  126.  
  127.         jMenuItem2.setLabel("Modyfikuj");
  128.         jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
  129.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  130.                 Modify(evt);
  131.             }
  132.         });
  133.         jMenu1.add(jMenuItem2);
  134.  
  135.         jMenuItem3.setLabel("Usuń");
  136.         jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
  137.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  138.                 Delete(evt);
  139.             }
  140.         });
  141.         jMenu1.add(jMenuItem3);
  142.  
  143.         jMenuItem4.setLabel("Koniec");
  144.         jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
  145.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  146.                 Exit(evt);
  147.             }
  148.         });
  149.         jMenu1.add(jMenuItem4);
  150.  
  151.         jMenuBar1.add(jMenu1);
  152.  
  153.         setJMenuBar(jMenuBar1);
  154.  
  155.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  156.         getContentPane().setLayout(layout);
  157.         layout.setHorizontalGroup(
  158.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  159.             .addGroup(layout.createSequentialGroup()
  160.                 .addGap(31, 31, 31)
  161.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 857, javax.swing.GroupLayout.PREFERRED_SIZE)
  162.                 .addContainerGap(38, Short.MAX_VALUE))
  163.         );
  164.         layout.setVerticalGroup(
  165.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  166.             .addGroup(layout.createSequentialGroup()
  167.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE)
  168.                 .addGap(0, 32, Short.MAX_VALUE))
  169.         );
  170.  
  171.         pack();
  172.     }// </editor-fold>//GEN-END:initComponents
  173.  
  174.     private void Wyswietl() {
  175.         for (int i = 0; i < pracownicy.size(); i++) {
  176.             jTable1.setValueAt(i + 1, i, 0);
  177.             jTable1.setValueAt(pracownicy.get(i).getImie(), i, 1);
  178.             jTable1.setValueAt(pracownicy.get(i).getNazwisko(), i, 2);
  179.             jTable1.setValueAt(pracownicy.get(i).getPlaca(), i, 3);
  180.         }
  181.  
  182.     }
  183.     int modyfikacja = 0;
  184.     private void Add(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Add
  185.         jDialog1.setVisible(true);
  186.     }//GEN-LAST:event_Add
  187.  
  188.     private void Modify(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Modify
  189.         modyfikacja = 1;
  190.         jDialog1.setVisible(true);
  191.  
  192.     }//GEN-LAST:event_Modify
  193.  
  194.     private void Delete(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Delete
  195.         try {
  196.             row = jTable1.getSelectedRow();
  197.             dtm.removeRow(row);
  198.             pracownicy.remove(row);
  199.             Wyswietl();
  200.         } catch (Exception e) {
  201.         }
  202.     }//GEN-LAST:event_Delete
  203.  
  204.     private void Exit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Exit
  205.         System.exit(0);
  206.     }//GEN-LAST:event_Exit
  207.  
  208.     private void ADD_Accept(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ADD_Accept
  209.         if (modyfikacja != 1) {
  210.             try {
  211.                 Pracownik pracownik = new Pracownik(Panel_Imie.getText(), Panel_Imie.getText(), Panel_Placa.getText());
  212.                 pracownicy.add(pracownik);
  213.                 dtm.addRow(new Object[]{"data", "data", "data", "data"});
  214.                 Wyswietl();
  215.             } catch (Exception e) {
  216.                 JOptionPane.showMessageDialog(null, "Nie utworzono pracownika");
  217.             }
  218.         }
  219.         if (modyfikacja == 1) {
  220.             try {
  221.                 row = jTable1.getSelectedRow();
  222.                 pracownicy.get(row).setImie(Panel_Imie.getText());
  223.                 pracownicy.get(row).setNazwisko(Panel_Nazwisko.getText());
  224.                 pracownicy.get(row).setPlaca(Panel_Placa.getText());
  225.                 Wyswietl();
  226.             } catch (Exception e) {
  227.                 JOptionPane.showMessageDialog(null, "Nie Zmieniono pracownika");
  228.             }
  229.         }
  230.  
  231.     }//GEN-LAST:event_ADD_Accept
  232.  
  233.     private void ADD_Cancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ADD_Cancel
  234.         Panel_Imie.setText(null);
  235.         Panel_Nazwisko.setText(null);
  236.         Panel_Placa.setText(null);
  237.         jDialog1.setVisible(false);
  238.         modyfikacja = 0;
  239.     }//GEN-LAST:event_ADD_Cancel
  240.  
  241.     /**
  242.      * @param args the command line arguments
  243.      */
  244.     public static void main(String args[]) {
  245.         /* Set the Nimbus look and feel */
  246.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  247.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  248.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  249.          */
  250.         try {
  251.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  252.                 if ("Nimbus".equals(info.getName())) {
  253.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  254.                     break;
  255.                 }
  256.             }
  257.         } catch (ClassNotFoundException ex) {
  258.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  259.         } catch (InstantiationException ex) {
  260.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  261.         } catch (IllegalAccessException ex) {
  262.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  263.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  264.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  265.         }
  266.         //</editor-fold>
  267.  
  268.         /* Create and display the form */
  269.         java.awt.EventQueue.invokeLater(new Runnable() {
  270.             public void run() {
  271.                 new NewJFrame().setVisible(true);
  272.             }
  273.         });
  274.     }
  275.  
  276.     // Variables declaration - do not modify//GEN-BEGIN:variables
  277.     private javax.swing.JButton PanelB_Anuluj;
  278.     private javax.swing.JButton PanelB_Dodaj;
  279.     private javax.swing.JTextField Panel_Imie;
  280.     private javax.swing.JTextField Panel_Nazwisko;
  281.     private javax.swing.JTextField Panel_Placa;
  282.     private javax.swing.JDialog jDialog1;
  283.     private javax.swing.JMenu jMenu1;
  284.     private javax.swing.JMenuBar jMenuBar1;
  285.     private javax.swing.JMenuItem jMenuItem1;
  286.     private javax.swing.JMenuItem jMenuItem2;
  287.     private javax.swing.JMenuItem jMenuItem3;
  288.     private javax.swing.JMenuItem jMenuItem4;
  289.     private javax.swing.JScrollPane jScrollPane1;
  290.     private javax.swing.JTable jTable1;
  291.     // End of variables declaration//GEN-END:variables
  292. }
Advertisement
Add Comment
Please, Sign In to add comment