Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pw_05_swingpierwszenieocieniane;
- import java.awt.List;
- import java.util.Vector;
- import javax.swing.JOptionPane;
- import javax.swing.table.DefaultTableModel;
- /**
- *
- * @author Tomek
- */
- public class NewJFrame extends javax.swing.JFrame {
- public static Vector<Pracownik> pracownicy = new Vector<>();
- DefaultTableModel dtm = new DefaultTableModel(new String[]{"ID", "Imię", "Nazwisko", "Płaca podstawowa"}, 0);
- int row;
- /**
- * Creates new form NewJFrame
- */
- public NewJFrame() {
- initComponents();
- }
- /**
- * 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">//GEN-BEGIN:initComponents
- private void initComponents() {
- jDialog1 = new javax.swing.JDialog();
- Panel_Imie = new javax.swing.JTextField();
- Panel_Nazwisko = new javax.swing.JTextField();
- Panel_Placa = new javax.swing.JTextField();
- PanelB_Dodaj = new javax.swing.JButton();
- PanelB_Anuluj = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTable1 = new javax.swing.JTable();
- jMenuBar1 = new javax.swing.JMenuBar();
- jMenu1 = new javax.swing.JMenu();
- jMenuItem1 = new javax.swing.JMenuItem();
- jMenuItem2 = new javax.swing.JMenuItem();
- jMenuItem3 = new javax.swing.JMenuItem();
- jMenuItem4 = new javax.swing.JMenuItem();
- jDialog1.setLocation(new java.awt.Point(500, 200));
- jDialog1.setMinimumSize(new java.awt.Dimension(500, 500));
- Panel_Imie.setText("Imie");
- Panel_Nazwisko.setText("Nazwisko");
- Panel_Placa.setText("Placa");
- PanelB_Dodaj.setText("Dodaj");
- PanelB_Dodaj.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ADD_Accept(evt);
- }
- });
- PanelB_Anuluj.setText("Anuluj");
- PanelB_Anuluj.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ADD_Cancel(evt);
- }
- });
- javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
- jDialog1.getContentPane().setLayout(jDialog1Layout);
- jDialog1Layout.setHorizontalGroup(
- jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jDialog1Layout.createSequentialGroup()
- .addGap(39, 39, 39)
- .addComponent(Panel_Imie, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(Panel_Nazwisko, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(Panel_Placa, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(42, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog1Layout.createSequentialGroup()
- .addGap(0, 0, Short.MAX_VALUE)
- .addComponent(PanelB_Dodaj, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(83, 83, 83)
- .addComponent(PanelB_Anuluj)
- .addGap(138, 138, 138))
- );
- jDialog1Layout.setVerticalGroup(
- jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jDialog1Layout.createSequentialGroup()
- .addGap(57, 57, 57)
- .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(Panel_Imie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Panel_Nazwisko, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Panel_Placa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
- .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(PanelB_Anuluj)
- .addComponent(PanelB_Dodaj))
- .addGap(68, 68, 68))
- );
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jTable1.setModel(dtm
- );
- jScrollPane1.setViewportView(jTable1);
- jMenu1.setText("Pracownik");
- jMenuItem1.setLabel("Dodaj");
- jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Add(evt);
- }
- });
- jMenu1.add(jMenuItem1);
- jMenuItem2.setLabel("Modyfikuj");
- jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Modify(evt);
- }
- });
- jMenu1.add(jMenuItem2);
- jMenuItem3.setLabel("Usuń");
- jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Delete(evt);
- }
- });
- jMenu1.add(jMenuItem3);
- jMenuItem4.setLabel("Koniec");
- jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Exit(evt);
- }
- });
- jMenu1.add(jMenuItem4);
- jMenuBar1.add(jMenu1);
- setJMenuBar(jMenuBar1);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(31, 31, 31)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 857, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(38, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 32, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- private void Wyswietl() {
- for (int i = 0; i < pracownicy.size(); i++) {
- jTable1.setValueAt(i + 1, i, 0);
- jTable1.setValueAt(pracownicy.get(i).getImie(), i, 1);
- jTable1.setValueAt(pracownicy.get(i).getNazwisko(), i, 2);
- jTable1.setValueAt(pracownicy.get(i).getPlaca(), i, 3);
- }
- }
- int modyfikacja = 0;
- private void Add(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Add
- jDialog1.setVisible(true);
- }//GEN-LAST:event_Add
- private void Modify(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Modify
- modyfikacja = 1;
- jDialog1.setVisible(true);
- }//GEN-LAST:event_Modify
- private void Delete(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Delete
- try {
- row = jTable1.getSelectedRow();
- dtm.removeRow(row);
- pracownicy.remove(row);
- Wyswietl();
- } catch (Exception e) {
- }
- }//GEN-LAST:event_Delete
- private void Exit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Exit
- System.exit(0);
- }//GEN-LAST:event_Exit
- private void ADD_Accept(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ADD_Accept
- if (modyfikacja != 1) {
- try {
- Pracownik pracownik = new Pracownik(Panel_Imie.getText(), Panel_Imie.getText(), Panel_Placa.getText());
- pracownicy.add(pracownik);
- dtm.addRow(new Object[]{"data", "data", "data", "data"});
- Wyswietl();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Nie utworzono pracownika");
- }
- }
- if (modyfikacja == 1) {
- try {
- row = jTable1.getSelectedRow();
- pracownicy.get(row).setImie(Panel_Imie.getText());
- pracownicy.get(row).setNazwisko(Panel_Nazwisko.getText());
- pracownicy.get(row).setPlaca(Panel_Placa.getText());
- Wyswietl();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Nie Zmieniono pracownika");
- }
- }
- }//GEN-LAST:event_ADD_Accept
- private void ADD_Cancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ADD_Cancel
- Panel_Imie.setText(null);
- Panel_Nazwisko.setText(null);
- Panel_Placa.setText(null);
- jDialog1.setVisible(false);
- modyfikacja = 0;
- }//GEN-LAST:event_ADD_Cancel
- /**
- * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JButton PanelB_Anuluj;
- private javax.swing.JButton PanelB_Dodaj;
- private javax.swing.JTextField Panel_Imie;
- private javax.swing.JTextField Panel_Nazwisko;
- private javax.swing.JTextField Panel_Placa;
- private javax.swing.JDialog jDialog1;
- private javax.swing.JMenu jMenu1;
- private javax.swing.JMenuBar jMenuBar1;
- private javax.swing.JMenuItem jMenuItem1;
- private javax.swing.JMenuItem jMenuItem2;
- private javax.swing.JMenuItem jMenuItem3;
- private javax.swing.JMenuItem jMenuItem4;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTable jTable1;
- // End of variables declaration//GEN-END:variables
- }
Advertisement
Add Comment
Please, Sign In to add comment