elsemTim

main2

Nov 17th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.44 KB | None | 0 0
  1. //package clientServerApp;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.*;
  6. import javax.swing.table.DefaultTableModel;
  7. import javax.swing.table.TableModel;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.sql.SQLException;
  12. import java.util.List;
  13. import java.util.Properties;
  14.  
  15.  
  16. public class FirebirdTWJJ {
  17.  
  18.     /**
  19.      * @param args
  20.      */
  21.     //global var
  22.     static DBConnect dbConn;
  23.  
  24.     static JFrame frame;
  25.     /// Depart controls
  26.     static JTable depGrid;
  27.  
  28.     static JDialog addDepartDialog;
  29.     static JDialog editDepartDialog;
  30.  
  31.     static JButton addDepButton;
  32.     static JButton delDepButton;
  33.     static JButton editDepButton;
  34.     /// End Depart controls
  35.  
  36.     /// Position controls
  37.     static JTable posGrid;
  38.  
  39.     static JDialog addPositDialog;
  40.     static JDialog editPositDialog;
  41.  
  42.     static JButton addPosButton;
  43.     static JButton delPosButton;
  44.     static JButton editPosButton;
  45.     /// End Position controls
  46.  
  47.  
  48.     //helpers
  49.     static void InitDB(Properties p) throws Exception{
  50.         dbConn = DBConnect.getInstance(p);
  51.     }
  52.  
  53.     static DepartmentTable GetDepartData() throws SQLException {
  54.         List<Department> dep = null;
  55.         try {
  56.             dep = dbConn.SelectDep();
  57.         } catch (SQLException e) {
  58.             e.printStackTrace();
  59.             throw e;
  60.         }
  61.         DepartmentTable depTable = new DepartmentTable();
  62.         String[] tmp = new String[depTable.getColumnCount()];
  63.         for (int i = 0; i < dep.size(); i++) {
  64.             Department d = dep.get(i);
  65.             tmp[0] = "" + d.GetID();
  66.             tmp[1] = d.GetNameDepartment();
  67.             tmp[2] = d.GetMail();
  68.             tmp[3] = d.GetPhone();
  69.             depTable.AddRow(tmp);
  70.         }
  71.         return depTable;
  72.     }
  73.  
  74.     static PositionTable GetPositionData() throws SQLException {
  75.         List<Position> pos = null;
  76.         try {
  77.             pos = dbConn.SelectPos();
  78.         } catch (SQLException e) {
  79.             e.printStackTrace();
  80.             throw e;
  81.         }
  82.         PositionTable posTable = new PositionTable();
  83.         String[] tmp = new String[posTable.getColumnCount()];
  84.         for (int i = 0; i < pos.size(); i++) {
  85.             Position p = pos.get(i);
  86.             tmp[0] = "" + p.GetID();
  87.             tmp[1] = p.GetNamePosition();
  88.             tmp[2] = ""+p.GetSalary();
  89.             posTable.AddRow(tmp);
  90.         }
  91.         return posTable;
  92.     }
  93.  
  94.     //создаем новый Dialog при нажатие на кнопку Add для Departmena
  95.     static void InitAddDepartDialog(){
  96.         addDepartDialog = new JDialog(frame,"Добавить запись",true);
  97.         addDepartDialog.setLocationRelativeTo(frame);
  98.         addDepartDialog.setSize(new Dimension(500,300));
  99.         addDepartDialog.setLayout(new GridBagLayout());
  100.         JTextField nameField=new JTextField(20);
  101.         JTextField phoneField=new JTextField(20);
  102.         JTextField mailField=new JTextField(20);
  103.         JButton okButton = new JButton("OK");
  104.         okButton.addActionListener(new ActionListener() {
  105.             @Override
  106.             public void actionPerformed(ActionEvent e) {
  107.                 String m=mailField.getText().trim();
  108.                 String p=phoneField.getText().trim();
  109.                 String n=nameField.getText().trim();
  110.                 if (m.length()>0 && p.length()>0 && n.length()>0) {
  111.                     try {
  112.                         dbConn.InsertDep(n, m, p);
  113.  
  114.                     } catch (SQLException e1) {
  115.                         e1.printStackTrace();
  116.                     }
  117.  
  118.                     try {
  119.                         addDepartDialog.setVisible(false);
  120.                         depGrid.setModel(GetDepartData());
  121.                         //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
  122.                         //model.fireTableDataChanged();
  123.                     } catch (SQLException e1) {
  124.                         e1.printStackTrace();
  125.                     }
  126.                 }
  127.                 addDepartDialog.setVisible(false);
  128.             }
  129.         });
  130.         addDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
  131.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  132.                 new Insets(2,2,2,2),0,0));
  133.  
  134.         addDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  135.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  136.                 new Insets(2,2,2,2),0,0));
  137.  
  138.         addDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
  139.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  140.                 new Insets(2,2,2,2),0,0));
  141.  
  142.         addDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
  143.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  144.                 new Insets(2,2,2,2),0,0));
  145.  
  146.         addDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
  147.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  148.                 new Insets(2,2,2,2),0,0));
  149.  
  150.         addDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
  151.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  152.                 new Insets(2,2,2,2),0,0));
  153.  
  154.         addDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  155.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  156.                 new Insets(2,2,2,2),0,0));
  157.  
  158.         addDepartDialog.pack();
  159.     }
  160.  
  161.     //создаем новый Dialog при нажатие на кнопку Add для Positiona
  162.     static void InitAddPositionDialog(){
  163.         addPositDialog = new JDialog(frame,"Добавить запись",true);
  164.         addPositDialog.setLocationRelativeTo(frame);
  165.         addPositDialog.setSize(new Dimension(500,300));
  166.         addPositDialog.setLayout(new GridBagLayout());
  167.         JTextField nameField=new JTextField(20);
  168.         JTextField salaryField=new JTextField(20);
  169.         JButton okButton = new JButton("OK");
  170.         okButton.addActionListener(new ActionListener() {
  171.             @Override
  172.             public void actionPerformed(ActionEvent e) {
  173.  
  174.                 String s=nameField.getText().trim();
  175.                 String n=salaryField.getText().trim();
  176.                 if ( s.length()>0 && n.length()>0) {
  177.                     try {
  178.                         dbConn.InsertPos(n,s);
  179.  
  180.                     } catch (SQLException e1) {
  181.                         e1.printStackTrace();
  182.                     }
  183.  
  184.                     try {
  185.                         addPositDialog.setVisible(false);
  186.                         posGrid.setModel(GetPositionData());
  187.                     } catch (SQLException e1) {
  188.                         e1.printStackTrace();
  189.                     }
  190.                 }
  191.                 addPositDialog.setVisible(false);
  192.             }
  193.         });
  194.         addPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
  195.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  196.                 new Insets(2,2,2,2),0,0));
  197.  
  198.         addPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  199.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  200.                 new Insets(2,2,2,2),0,0));
  201.  
  202.         addPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
  203.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  204.                 new Insets(2,2,2,2),0,0));
  205.  
  206.         addPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
  207.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  208.                 new Insets(2,2,2,2),0,0));
  209.  
  210.         addPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  211.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  212.                 new Insets(2,2,2,2),0,0));
  213.  
  214.         addPositDialog.pack();
  215.     }
  216.  
  217.     static void InitEditDepartDialog(){
  218.         editDepartDialog = new JDialog(frame,"Добавить запись",true);
  219.         editDepartDialog.setLocationRelativeTo(frame);
  220.         editDepartDialog.setSize(new Dimension(500,300));
  221.         editDepartDialog.setLayout(new GridBagLayout());
  222.         JTextField nameField=new JTextField(20);
  223.         JTextField phoneField=new JTextField(20);
  224.         JTextField mailField=new JTextField(20);
  225.         JTextField idField=new JTextField(20);
  226.         idField.setVisible(false);
  227.         JButton okButton = new JButton("OK");
  228.         okButton.addActionListener(new ActionListener() {
  229.             @Override
  230.             public void actionPerformed(ActionEvent e) {
  231.                 String m=mailField.getText().trim();
  232.                 String p=phoneField.getText().trim();
  233.                 String n=nameField.getText().trim();
  234.                 String sid=idField.getText().trim();
  235.                 int id = Integer.parseInt(sid);
  236.                 if (m.length()>0 && p.length()>0 && n.length()>0) {
  237.                     try {
  238.                         dbConn.UpdateDep(id, n, m, p);
  239.  
  240.                     } catch (SQLException e1) {
  241.                         e1.printStackTrace();
  242.                     }
  243.  
  244.                     try {
  245.                         editDepartDialog.setVisible(false);
  246.                         depGrid.setModel(GetDepartData());
  247.                         //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
  248.                         //model.fireTableDataChanged();
  249.                     } catch (SQLException e1) {
  250.                         e1.printStackTrace();
  251.                     }
  252.                 }
  253.                 editDepartDialog.setVisible(false);
  254.             }
  255.         });
  256.         editDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
  257.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  258.                 new Insets(2,2,2,2),0,0));
  259.  
  260.         editDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  261.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  262.                 new Insets(2,2,2,2),0,0));
  263.  
  264.         editDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
  265.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  266.                 new Insets(2,2,2,2),0,0));
  267.  
  268.         editDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
  269.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  270.                 new Insets(2,2,2,2),0,0));
  271.  
  272.         editDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
  273.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  274.                 new Insets(2,2,2,2),0,0));
  275.  
  276.         editDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
  277.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  278.                 new Insets(2,2,2,2),0,0));
  279.  
  280.         editDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  281.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  282.                 new Insets(2,2,2,2),0,0));
  283.  
  284.         editDepartDialog.add(idField, new GridBagConstraints(1,4,1,1,1,1,
  285.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  286.                 new Insets(2,2,2,2),0,0));
  287.  
  288.  
  289.         editDepartDialog.pack();
  290.     }
  291.  
  292.     static void InitEditPositDialog(){
  293.         editPositDialog = new JDialog(frame,"Добавить запись",true);
  294.         editPositDialog.setLocationRelativeTo(frame);
  295.         editPositDialog.setSize(new Dimension(500,300));
  296.         editPositDialog.setLayout(new GridBagLayout());
  297.         JTextField nameField=new JTextField(20);
  298.         JTextField salaryField=new JTextField(20);
  299.         JTextField idField=new JTextField(20);
  300.         idField.setVisible(false);
  301.         JButton okButton = new JButton("OK");
  302.         okButton.addActionListener(new ActionListener() {
  303.             @Override
  304.             public void actionPerformed(ActionEvent e) {
  305.                 String n=nameField.getText().trim();
  306.                 String s=salaryField.getText().trim();
  307.                 String sid=idField.getText().trim();
  308.                 int id = Integer.parseInt(sid);
  309.                 if (s.length()>0 && n.length()>0) {
  310.                     try {
  311.                         dbConn.UpdatePos(Integer.parseInt(s),n,id);
  312.  
  313.                     } catch (SQLException e1) {
  314.                         e1.printStackTrace();
  315.                     }
  316.  
  317.                     try {
  318.                         editPositDialog.setVisible(false);
  319.                         posGrid.setModel(GetPositionData());
  320.                     } catch (SQLException e1) {
  321.                         e1.printStackTrace();
  322.                     }
  323.                 }
  324.                 editPositDialog.setVisible(false);
  325.             }
  326.         });
  327.  
  328.         addPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
  329.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  330.                 new Insets(2,2,2,2),0,0));
  331.  
  332.         addPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  333.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  334.                 new Insets(2,2,2,2),0,0));
  335.  
  336.         addPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
  337.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  338.                 new Insets(2,2,2,2),0,0));
  339.  
  340.         addPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
  341.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  342.                 new Insets(2,2,2,2),0,0));
  343.  
  344.         addPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  345.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  346.                 new Insets(2,2,2,2),0,0));
  347.  
  348.         editPositDialog.pack();
  349.     }
  350.  
  351.     static void InitDialogs(){
  352.         InitAddDepartDialog();
  353.         InitEditDepartDialog();
  354.  
  355.     }
  356. ///////////Работа со скрытием элементов
  357. //dep
  358.     static void ShowDepartment() throws SQLException {
  359.  
  360.         depGrid.setModel(GetDepartData());
  361.         depGrid.setVisible(true);
  362.         addDepButton.setVisible(true);
  363.         delDepButton.setVisible(true);
  364.         editDepButton.setVisible(true);
  365.     }
  366.  
  367.     static void HideDepartment() throws SQLException{
  368.         depGrid.setVisible(false);
  369.         addDepButton.setVisible(false);
  370.         delDepButton.setVisible(false);
  371.         editDepartDialog.setVisible(false);
  372.     }
  373. //pos
  374.     static void ShowPosition() throws SQLException{
  375.         posGrid.setModel(GetPositionData());
  376.         posGrid.setVisible(true);
  377.         addPosButton.setVisible(true);
  378.         delPosButton.setVisible(true);
  379.         editPosButton.setVisible(true);
  380.         //HideDepartment();
  381.     }
  382.  
  383.     static void HidePosition() throws SQLException{
  384.  
  385.         addPosButton.setVisible(false);
  386.         delPosButton.setVisible(false);
  387.         editPosButton.setVisible(false);
  388.         posGrid.setVisible(false);
  389.     }
  390. //emp
  391.     static void ShowEmployee() throws SQLException{
  392.        // HidePosition();
  393.         //HideDepartment();
  394.         //включаем видимость кнопок
  395.         //
  396.     }
  397.  
  398.     //static void HideEmployee() throws SQLException{
  399.         //выключаем видимость кнопок
  400.     //}
  401.  
  402. ///////
  403.     static void ShowFrame()
  404.     {
  405.         frame.setVisible(true);
  406.         frame.pack();
  407.     }
  408.     static void InitFrame(){
  409.         frame = new JFrame("Учет сотрудников предприятия");
  410.         frame.setSize(new Dimension(1000, 1000));
  411.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  412.         frame.setLocationRelativeTo(null);
  413.         frame.setLayout(new GridBagLayout());
  414.  
  415.         ButtonGroup group = new ButtonGroup();
  416.         //выполняем инизиализацию и отрисовку для
  417.         //Department
  418.         JRadioButton dep = new JRadioButton("Отдел",true);
  419.  
  420.         dep.addActionListener(new ActionListener() {
  421.             @Override
  422.             public void actionPerformed(ActionEvent e) {
  423.                 try {
  424.                     HidePosition();
  425.                     ShowDepartment();
  426.  
  427.                 }
  428.                 catch (SQLException ex)
  429.                 {
  430.                     JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
  431.                             JOptionPane.ERROR_MESSAGE);
  432.                     return;
  433.                 }
  434.             }
  435.         });
  436.         group.add(dep);
  437.         //////
  438.  
  439.         //выполняем инизиализацию и отрисовку для
  440.         //Position
  441.         JRadioButton pos = new JRadioButton("Должность",false);
  442.  
  443.         pos.addActionListener(new ActionListener() {
  444.             @Override
  445.             public void actionPerformed(ActionEvent e) {
  446.                 try {
  447.  
  448.                     InitAddPositionDialog();
  449.                     InitEditPositDialog();
  450.                     InitPositionGrid();
  451.                     HideDepartment();
  452.                     ShowPosition();
  453.  
  454.  
  455.                 }
  456.                 catch (SQLException ex) {
  457.  
  458.                 JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
  459.                         JOptionPane.ERROR_MESSAGE);
  460.                 return;
  461.                 }
  462.  
  463.  
  464.             }
  465.         });
  466.         group.add(pos);
  467.         //////
  468.  
  469.         //выполняем инизиализацию и отрисовку для
  470.         //Employee
  471.         JRadioButton emp = new JRadioButton("Люди",false);
  472.         //
  473.         emp.addActionListener(new ActionListener() {
  474.             @Override
  475.             public void actionPerformed(ActionEvent e) {
  476.                 try {
  477.                     ShowEmployee();
  478.                 }
  479.                 catch (SQLException ex) {
  480.  
  481.                     JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
  482.                             JOptionPane.ERROR_MESSAGE);
  483.                     return;
  484.                 }
  485.             }
  486.         });
  487.         group.add(emp);
  488.         //////
  489.         frame.add(dep, new GridBagConstraints(0, 2, 1, 1, 1, 1,
  490.                 GridBagConstraints.NORTH,
  491.                 GridBagConstraints.BOTH,
  492.                 new Insets(1, 4, 1, 1), 0, 0));
  493.         frame.add(pos, new GridBagConstraints(1, 2, 1, 1, 1, 1,
  494.                 GridBagConstraints.NORTH,
  495.                 GridBagConstraints.BOTH,
  496.                 new Insets(1, 4, 1, 1), 0, 0));
  497.  
  498.         frame.add(emp, new GridBagConstraints(2, 2, 1, 1, 1, 1,
  499.                 GridBagConstraints.NORTH,
  500.                 GridBagConstraints.BOTH,
  501.                 new Insets(1, 4, 1, 1), 0, 0));
  502.  
  503.     }
  504.  
  505.     static void InitDepartGrid() throws SQLException{
  506.         //указываем модель таблицы
  507.  
  508.         try {
  509.             depGrid = new JTable(GetDepartData());
  510.         } catch (SQLException e) {
  511.             e.printStackTrace();
  512.             throw e;
  513.         }
  514.         depGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  515.         JScrollPane emplTableScroolPage = new JScrollPane(depGrid);
  516.         emplTableScroolPage.setPreferredSize(new Dimension(400, 400));
  517.         //addbut
  518.         addDepButton = new JButton("Добавить");
  519.         addDepButton.addActionListener(new ActionListener() {
  520.             @Override
  521.             public void actionPerformed(ActionEvent e) {
  522.                 addDepartDialog.setVisible(true);
  523.             }
  524.         });
  525.  
  526.         //delbut
  527.         delDepButton = new JButton("Удалить");
  528.         delDepButton.addActionListener(new ActionListener() {
  529.             @Override
  530.             public void actionPerformed(ActionEvent e) {
  531.                 int n=depGrid.getSelectedRow();
  532.                 if (n>=0){
  533.                     TableModel m=depGrid.getModel();
  534.                     String s=(String)m.getValueAt(n,0);
  535.                     int id= Integer.parseInt(s);
  536.                     try {
  537.                         dbConn.RemoveDep(id);
  538.                     } catch (SQLException e1) {
  539.                         e1.printStackTrace();
  540.                     }
  541.  
  542.                     try {
  543.                         depGrid.setModel(GetDepartData());
  544.                         } catch (SQLException e1) {
  545.                         e1.printStackTrace();
  546.                     }
  547.                 }
  548.  
  549.             }
  550.         });
  551.  
  552.         //editbut
  553.         editDepButton = new JButton("Изменить");
  554.         editDepButton.addActionListener(new ActionListener() {
  555.             @Override
  556.             public void actionPerformed(ActionEvent e) {
  557.                 int n=depGrid.getSelectedRow();
  558.                 if (n>=0){
  559.                     TableModel m=depGrid.getModel();
  560.                     Component [] comp = editDepartDialog.getContentPane().getComponents();
  561.                     ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
  562.                     ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
  563.                     ((JTextField)comp[5]).setText((String)m.getValueAt(n,3));
  564.                     ((JTextField)comp[7]).setText((String)m.getValueAt(n,0));
  565.  
  566.                     editDepartDialog.setVisible(true);
  567.                 }
  568.  
  569.             }
  570.         });
  571.  
  572.         frame.add(emplTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
  573.                 GridBagConstraints.NORTH,
  574.                 GridBagConstraints.BOTH,
  575.                 new Insets(1, 1, 1, 1), 0, 0));
  576.  
  577.         frame.add(addDepButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
  578.                 GridBagConstraints.NORTH,
  579.                 GridBagConstraints.BOTH,
  580.                 new Insets(1, 1, 1, 1), 0, 0));
  581.  
  582.         frame.add(delDepButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
  583.                 GridBagConstraints.NORTH,
  584.                 GridBagConstraints.BOTH,
  585.                 new Insets(1, 1, 1, 1), 0, 0));
  586.  
  587.         frame.add(editDepButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
  588.                 GridBagConstraints.NORTH,
  589.                 GridBagConstraints.BOTH,
  590.                 new Insets(1, 1, 1, 1), 0, 0));
  591.     }
  592.  
  593.     static void InitPositionGrid() throws SQLException{
  594.         //указываем модель таблицы
  595.  
  596.         try {
  597.             posGrid = new JTable(GetPositionData());
  598.         } catch (SQLException e) {
  599.             e.printStackTrace();
  600.             throw e;
  601.         }
  602.         posGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  603.         JScrollPane emplTableScroolPage = new JScrollPane(posGrid);
  604.         emplTableScroolPage.setPreferredSize(new Dimension(400, 400));
  605.         //addbut
  606.         addPosButton = new JButton("Добавить");
  607.         addPosButton.addActionListener(new ActionListener() {
  608.             @Override
  609.             public void actionPerformed(ActionEvent e) {
  610.                 addPositDialog.setVisible(true);
  611.             }
  612.         });
  613.  
  614.         //delbut
  615.         delPosButton = new JButton("Удалить");
  616.         delPosButton.addActionListener(new ActionListener() {
  617.             @Override
  618.             public void actionPerformed(ActionEvent e) {
  619.                 int n=posGrid.getSelectedRow();
  620.                 if (n>=0){
  621.                     TableModel m=posGrid.getModel();
  622.                     String s=(String)m.getValueAt(n,0);
  623.                     int id= Integer.parseInt(s);
  624.                     try {
  625.                         dbConn.RemovePos(id);
  626.                     } catch (SQLException e1) {
  627.                         e1.printStackTrace();
  628.                     }
  629.  
  630.                     try {
  631.                         posGrid.setModel(GetPositionData());
  632.                     } catch (SQLException e1) {
  633.                         e1.printStackTrace();
  634.                     }
  635.                 }
  636.  
  637.             }
  638.         });
  639.  
  640.         //editbut
  641.         editPosButton = new JButton("Изменить");
  642.         editPosButton.addActionListener(new ActionListener() {
  643.             @Override
  644.             public void actionPerformed(ActionEvent e) {
  645.                 int n=posGrid.getSelectedRow();
  646.                 if (n>=0){
  647.                     TableModel m=posGrid.getModel();
  648.                     Component [] comp = editPositDialog.getContentPane().getComponents();
  649.                     ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
  650.                     ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
  651.                     ((JTextField)comp[5]).setText((String)m.getValueAt(n,3));
  652.                     editPositDialog.setVisible(true);
  653.                 }
  654.  
  655.             }
  656.         });
  657.  
  658.         frame.add(emplTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
  659.                 GridBagConstraints.NORTH,
  660.                 GridBagConstraints.BOTH,
  661.                 new Insets(1, 1, 1, 1), 0, 0));
  662.  
  663.         frame.add(addPosButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
  664.                 GridBagConstraints.NORTH,
  665.                 GridBagConstraints.BOTH,
  666.                 new Insets(1, 1, 1, 1), 0, 0));
  667.  
  668.         frame.add(delPosButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
  669.                 GridBagConstraints.NORTH,
  670.                 GridBagConstraints.BOTH,
  671.                 new Insets(1, 1, 1, 1), 0, 0));
  672.  
  673.         frame.add(editPosButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
  674.                 GridBagConstraints.NORTH,
  675.                 GridBagConstraints.BOTH,
  676.                 new Insets(1, 1, 1, 1), 0, 0));
  677.         ShowFrame();
  678.     }
  679.  
  680.     public static void main(String[] args) {
  681.         Properties props = new Properties();
  682.         try {
  683.             props.load(new FileInputStream(new File(".\\src\\config.ini")));
  684.         } catch (IOException e) {
  685.             JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
  686.                     JOptionPane.ERROR_MESSAGE);
  687.             return;
  688.         }
  689.         try {
  690.             InitDB(props);
  691.         } catch (Exception e) {
  692.             e.printStackTrace();
  693.             JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
  694.                     JOptionPane.ERROR_MESSAGE);
  695.             return;
  696.         }
  697.         InitFrame();
  698.        InitDialogs();
  699.         try {
  700.             InitDepartGrid();
  701.             ShowFrame();
  702.             //ShowDepartment();
  703.         } catch (SQLException e) {
  704.             e.printStackTrace();
  705.             JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
  706.                     JOptionPane.ERROR_MESSAGE);
  707.             return;
  708.         };
  709.     }
  710. }
Advertisement
Add Comment
Please, Sign In to add comment