Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package clientServerApp;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import javax.swing.table.TableModel;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
- public class FirebirdTWJJ {
- /**
- * @param args
- */
- //global var
- static DBConnect dbConn;
- static JFrame frame;
- /// Depart controls
- static JTable depGrid;
- static JDialog addDepartDialog;
- static JDialog editDepartDialog;
- static JButton addDepButton;
- static JButton delDepButton;
- static JButton editDepButton;
- static JScrollPane departTableScroolPage;
- /// End Depart controls
- /// Position controls
- static JTable posGrid;
- static JDialog addPositDialog;
- static JDialog editPositDialog;
- static JButton addPosButton;
- static JButton delPosButton;
- static JButton editPosButton;
- static JScrollPane positTableScroolPage;
- /// End Position controls
- /// Employee controls
- static JTable empGrid;
- static JDialog addEmplDialog;
- static JDialog editEmplDialog;
- static JButton addEmpButton;
- static JButton delEmpButton;
- static JButton editEmpButton;
- static JScrollPane emplTableScroolPage;
- /// End Employee controls
- static JRadioButton empRB;
- static JRadioButton posRB;
- static JRadioButton depRB;
- //helpers
- static void InitDB(Properties p) throws Exception{
- dbConn = DBConnect.getInstance(p);
- }
- static DepartmentTable GetDepartData() throws SQLException {
- List<Department> dep = null;
- try {
- dep = dbConn.SelectDep();
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- DepartmentTable depTable = new DepartmentTable();
- String[] tmp = new String[depTable.getColumnCount()];
- for (int i = 0; i < dep.size(); i++) {
- Department d = dep.get(i);
- tmp[0] = "" + d.GetID();
- tmp[1] = d.GetNameDepartment();
- tmp[2] = d.GetMail();
- tmp[3] = d.GetPhone();
- depTable.AddRow(tmp);
- }
- return depTable;
- }
- static PositionTable GetPositionData() throws SQLException {
- List<Position> pos = null;
- try {
- pos = dbConn.SelectPos();
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- PositionTable posTable = new PositionTable();
- String[] tmp = new String[posTable.getColumnCount()];
- for (int i = 0; i < pos.size(); i++) {
- Position p = pos.get(i);
- tmp[0] = "" + p.GetID();
- tmp[1] = p.GetNamePosition();
- tmp[2] = ""+p.GetSalary();
- posTable.AddRow(tmp);
- }
- return posTable;
- }
- static EmployeeTable GetEmployeeData() throws SQLException {
- List<Employee> emp = null;
- try {
- emp = dbConn.SelectEmp();
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- EmployeeTable empTable = new EmployeeTable();
- String[] tmp = new String[empTable.getColumnCount()];
- for (int i = 0; i < emp.size(); i++) {
- Employee e = emp.get(i);
- tmp[0] = "" + e.getID();
- tmp[1] = e.GetFirstName();
- tmp[2] = e.GetLastName();
- tmp[3] = e.GetNameDep();
- tmp[4] = e.GetNamePos();
- tmp[5] =""+e.getSalary();
- empTable.AddRow(tmp);
- }
- return empTable;
- }
- static List<ComboItem> GetDepItems () {
- try {
- return dbConn.GetDepItems();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return new ArrayList<ComboItem> ();
- }
- static List<ComboItem> GetPosItems () {
- try {
- return dbConn.GetPosItems();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return new ArrayList<ComboItem> ();
- }
- //создаем новый Dialog при нажатие на кнопку Add для Departmena
- static void InitAddDepartDialog(){
- addDepartDialog = new JDialog(frame,"Добавить отдел",true);
- addDepartDialog.setLocationRelativeTo(frame);
- addDepartDialog.setSize(new Dimension(500,300));
- addDepartDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField phoneField=new JTextField(20);
- JTextField mailField=new JTextField(20);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String m=mailField.getText().trim();
- String p=phoneField.getText().trim();
- String n=nameField.getText().trim();
- if (m.length()>0 && p.length()>0 && n.length()>0) {
- try {
- dbConn.InsertDep(n, m, p);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- addDepartDialog.setVisible(false);
- depGrid.setModel(GetDepartData());
- //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
- //model.fireTableDataChanged();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- addDepartDialog.setVisible(false);
- }
- });
- addDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.pack();
- }
- //создаем новый Dialog при нажатие на кнопку Add для Positiona
- static void InitAddPositionDialog(){
- addPositDialog = new JDialog(frame,"Добавить должность",true);
- addPositDialog.setLocationRelativeTo(frame);
- addPositDialog.setSize(new Dimension(500,300));
- addPositDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField salaryField=new JTextField(20);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String posn=nameField.getText().trim();
- String ss=salaryField.getText().trim();
- if (ss.length()>0 && posn.length()>0) {
- try {
- int sal = Integer.parseInt(ss);
- dbConn.InsertPos(sal,posn);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- addPositDialog.setVisible(false);
- posGrid.setModel(GetPositionData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- addPositDialog.setVisible(false);
- }
- });
- addPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.pack();
- }
- //создаем новый Dialog при нажатие на кнопку Add для Employee
- static void InitAddEmployeeDialog(){
- addEmplDialog = new JDialog(frame,"Добавить сотрудника",true);
- addEmplDialog.setLocationRelativeTo(frame);
- addEmplDialog.setSize(new Dimension(500,300));
- addEmplDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField lnameField=new JTextField(20);
- JComboBox<ComboItem> pos= new JComboBox<ComboItem>();
- List <ComboItem> lp = GetPosItems();
- for (int i=0;i<lp.size();i++)
- {
- pos.addItem(lp.get(i));
- }
- JComboBox<ComboItem> dep= new JComboBox<ComboItem>();
- List <ComboItem> ld = GetDepItems();
- for (int i=0;i<ld.size();i++)
- {
- dep.addItem(ld.get(i));
- }
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String n=nameField.getText().trim();
- String ln=lnameField.getText().trim();
- int did= ((ComboItem)dep.getSelectedItem()).getValue();
- int pid= ((ComboItem)pos.getSelectedItem()).getValue();
- if ( ln.length()>0 && n.length()>0) {
- try {
- dbConn.InsertEmp(n,ln, did,pid);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- addEmplDialog.setVisible(false);
- empGrid.setModel(GetEmployeeData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- addEmplDialog.setVisible(false);
- }
- });
- addEmplDialog.add( new JLabel("Имя: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add( new JLabel("Фамилия: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(lnameField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add( new JLabel("Отдел: "), new GridBagConstraints(0,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(dep, new GridBagConstraints(1,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(pos, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(okButton, new GridBagConstraints(1,4,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.pack();
- }
- static void InitEditDepartDialog(){
- editDepartDialog = new JDialog(frame,"Изменить отдел",true);
- editDepartDialog.setLocationRelativeTo(frame);
- editDepartDialog.setSize(new Dimension(500,300));
- editDepartDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField phoneField=new JTextField(20);
- JTextField mailField=new JTextField(20);
- JTextField idField=new JTextField(20);
- idField.setVisible(false);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String m=mailField.getText().trim();
- String p=phoneField.getText().trim();
- String n=nameField.getText().trim();
- String sid=idField.getText().trim();
- int id = Integer.parseInt(sid);
- if (m.length()>0 && p.length()>0 && n.length()>0) {
- try {
- dbConn.UpdateDep(id, n, m, p);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- editDepartDialog.setVisible(false);
- depGrid.setModel(GetDepartData());
- //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
- //model.fireTableDataChanged();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- editDepartDialog.setVisible(false);
- }
- });
- editDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(idField, new GridBagConstraints(1,4,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.pack();
- }
- static void InitEditPositDialog(){
- editPositDialog = new JDialog(frame,"Изменить должность",true);
- editPositDialog.setLocationRelativeTo(frame);
- editPositDialog.setSize(new Dimension(500,300));
- editPositDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField salaryField=new JTextField(20);
- JTextField idField=new JTextField(20);
- idField.setVisible(false);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String n=nameField.getText().trim();
- String s=salaryField.getText().trim();
- String sid=idField.getText().trim();
- int id = Integer.parseInt(sid);
- if (s.length()>0 && n.length()>0) {
- try {
- dbConn.UpdatePos(Integer.parseInt(s),n,id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- editPositDialog.setVisible(false);
- posGrid.setModel(GetPositionData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- editPositDialog.setVisible(false);
- }
- });
- editPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editPositDialog.add(idField, new GridBagConstraints(1,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editPositDialog.pack();
- }
- static void InitEditEmployeeDialog(){
- addEmplDialog = new JDialog(frame,"Добавить сотрудника",true);
- addEmplDialog.setLocationRelativeTo(frame);
- addEmplDialog.setSize(new Dimension(500,300));
- addEmplDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField lnameField=new JTextField(20);
- JTextField idField=new JTextField(20);
- JComboBox<ComboItem> pos= new JComboBox<ComboItem>();
- List <ComboItem> lp = GetPosItems();
- for (int i=0;i<lp.size();i++)
- {
- pos.addItem(lp.get(i));
- }
- JComboBox<ComboItem> dep= new JComboBox<ComboItem>();
- List <ComboItem> ld = GetDepItems();
- for (int i=0;i<ld.size();i++)
- {
- dep.addItem(ld.get(i));
- }
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String n=nameField.getText().trim();
- String ln=lnameField.getText().trim();
- int did= ((ComboItem)dep.getSelectedItem()).getValue();
- int pid= ((ComboItem)pos.getSelectedItem()).getValue();
- String sid=idField.getText().trim();
- int id = Integer.parseInt(sid);
- if ( ln.length()>0 && n.length()>0) {
- try {
- dbConn.UpdateEmp(n,ln, did,pid,id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- addEmplDialog.setVisible(false);
- empGrid.setModel(GetEmployeeData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- addEmplDialog.setVisible(false);
- }
- });
- addEmplDialog.add( new JLabel("Имя: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add( new JLabel("Фамилия: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(lnameField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add( new JLabel("Отдел: "), new GridBagConstraints(0,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(dep, new GridBagConstraints(1,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(pos, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.add(okButton, new GridBagConstraints(1,4,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addEmplDialog.pack();
- }
- static void InitDialogs(){
- InitAddDepartDialog();
- InitEditDepartDialog();
- InitAddPositionDialog();
- InitEditPositDialog();
- InitAddEmployeeDialog();
- InitEditEmployeeDialog();
- }
- ///////////Работа со скрытием элементов
- //dep
- static void ShowDepartment() throws SQLException {
- HidePosition();
- HideEmployee();
- frame.add(departTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- depGrid.setModel(GetDepartData());
- depGrid.setVisible(true);
- addDepButton.setVisible(true);
- delDepButton.setVisible(true);
- editDepButton.setVisible(true);
- departTableScroolPage.setVisible(true);
- }
- static void HideDepartment() throws SQLException{
- departTableScroolPage.setVisible(false);
- frame.remove(departTableScroolPage);
- depGrid.setVisible(false);
- addDepButton.setVisible(false);
- delDepButton.setVisible(false);
- editDepButton.setVisible(false);
- }
- //pos
- static void ShowPosition() throws SQLException{
- HideDepartment();
- HideEmployee();
- frame.add(positTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- posGrid.setModel(GetPositionData());
- posGrid.setVisible(true);
- addPosButton.setVisible(true);
- delPosButton.setVisible(true);
- editPosButton.setVisible(true);
- positTableScroolPage.setVisible(true);
- }
- static void HidePosition() throws SQLException{
- positTableScroolPage.setVisible(false);
- frame.remove(positTableScroolPage);
- posGrid.setVisible(false);
- addPosButton.setVisible(false);
- delPosButton.setVisible(false);
- editPosButton.setVisible(false);
- }
- //emp
- static void ShowEmployee() throws SQLException{
- HidePosition();
- HideDepartment();
- frame.add(emplTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- empGrid.setModel(GetEmployeeData());
- empGrid.setVisible(true);
- addEmpButton.setVisible(true);
- delEmpButton.setVisible(true);
- editEmpButton.setVisible(true);
- emplTableScroolPage.setVisible(true);
- }
- static void HideEmployee() throws SQLException{
- emplTableScroolPage.setVisible(false);
- frame.remove(emplTableScroolPage);
- empGrid.setVisible(false);
- addEmpButton.setVisible(false);
- delEmpButton.setVisible(false);
- editEmpButton.setVisible(false);
- }
- ///////
- static void ShowFrame()
- {
- frame.pack();
- frame.setVisible(true);
- }
- static void InitFrame(){
- frame = new JFrame("Учет сотрудников предприятия");
- frame.setSize(new Dimension(1000, 1000));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setLayout(new GridBagLayout());
- ButtonGroup group = new ButtonGroup();
- //выполняем инизиализацию и отрисовку для
- //Department
- depRB = new JRadioButton("Отдел",true);
- depRB.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- ShowDepartment();
- }
- catch (SQLException ex)
- {
- JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- });
- group.add(depRB);
- //////
- //выполняем инизиализацию и отрисовку для
- //Position
- posRB = new JRadioButton("Должность",false);
- posRB.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- ShowPosition();
- }
- catch (SQLException ex) {
- JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- });
- group.add(posRB);
- //////
- //выполняем инизиализацию и отрисовку для
- //Employee
- empRB = new JRadioButton("Люди",false);
- //
- empRB.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- ShowEmployee();
- }
- catch (SQLException ex) {
- JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- });
- group.add(empRB);
- //////
- frame.add(depRB, new GridBagConstraints(0, 2, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 4, 1, 1), 0, 0));
- frame.add(posRB, new GridBagConstraints(1, 2, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 4, 1, 1), 0, 0));
- frame.add(empRB, new GridBagConstraints(2, 2, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 4, 1, 1), 0, 0));
- }
- static void InitDepartGrid() throws SQLException{
- //указываем модель таблицы
- try {
- depGrid = new JTable(GetDepartData());
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- depGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- departTableScroolPage = new JScrollPane(depGrid);
- departTableScroolPage.setPreferredSize(new Dimension(400, 400));
- //addbut
- addDepButton = new JButton("Добавить");
- addDepButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- addDepartDialog.setVisible(true);
- }
- });
- //delbut
- delDepButton = new JButton("Удалить");
- delDepButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=depGrid.getSelectedRow();
- if (n>=0){
- TableModel m=depGrid.getModel();
- String s=(String)m.getValueAt(n,0);
- int id= Integer.parseInt(s);
- try {
- dbConn.RemoveDep(id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- depGrid.setModel(GetDepartData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- //editbut
- editDepButton = new JButton("Изменить");
- editDepButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=depGrid.getSelectedRow();
- if (n>=0){
- TableModel m=depGrid.getModel();
- Component [] comp = editDepartDialog.getContentPane().getComponents();
- ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
- ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
- ((JTextField)comp[5]).setText((String)m.getValueAt(n,3));
- ((JTextField)comp[7]).setText((String)m.getValueAt(n,0));
- editDepartDialog.setVisible(true);
- }
- }
- });
- frame.add(addDepButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(delDepButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(editDepButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- }
- static void InitPositionGrid() throws SQLException{
- //указываем модель таблицы
- try {
- posGrid = new JTable(GetPositionData());
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- posGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- positTableScroolPage = new JScrollPane(posGrid);
- positTableScroolPage.setPreferredSize(new Dimension(400, 400));
- //addbut
- addPosButton = new JButton("Добавить");
- addPosButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- addPositDialog.setVisible(true);
- }
- });
- //delbut
- delPosButton = new JButton("Удалить");
- delPosButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=posGrid.getSelectedRow();
- if (n>=0){
- TableModel m=posGrid.getModel();
- String s=(String)m.getValueAt(n,0);
- int id= Integer.parseInt(s);
- try {
- dbConn.RemovePos(id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- posGrid.setModel(GetPositionData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- //editbut
- editPosButton = new JButton("Изменить");
- editPosButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=posGrid.getSelectedRow();
- if (n>=0){
- TableModel m=posGrid.getModel();
- Component [] comp = editPositDialog.getContentPane().getComponents();
- ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
- ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
- ((JTextField)comp[5]).setText((String)m.getValueAt(n,0));
- editPositDialog.setVisible(true);
- }
- }
- });
- frame.add(addPosButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(delPosButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(editPosButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- ShowFrame();
- }
- static void InitEmplnGrid() throws SQLException{
- //указываем модель таблицы
- try {
- empGrid = new JTable(GetEmployeeData());
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- empGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- emplTableScroolPage = new JScrollPane(empGrid);
- emplTableScroolPage.setPreferredSize(new Dimension(400, 400));
- //addbut
- addEmpButton = new JButton("Добавить");
- addEmpButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- addEmplDialog.setVisible(true);
- }
- });
- //delbut
- delEmpButton = new JButton("Удалить");
- delEmpButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=empGrid.getSelectedRow();
- if (n>=0){
- TableModel m=empGrid.getModel();
- String s=(String)m.getValueAt(n,0);
- int id= Integer.parseInt(s);
- try {
- dbConn.RemoveEmp(id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- empGrid.setModel(GetEmployeeData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- //editbut
- editEmpButton = new JButton("Изменить");
- editEmpButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=empGrid.getSelectedRow();
- if (n>=0){
- TableModel m=empGrid.getModel();
- Component [] comp = editEmplDialog.getContentPane().getComponents();
- ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
- ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
- ((JTextField)comp[5]).setText((String)m.getValueAt(n,0));
- editEmplDialog.setVisible(true);
- }
- }
- });
- frame.add(addEmpButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(delEmpButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(editEmpButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- ShowFrame();
- }
- public static void main(String[] args) {
- Properties props = new Properties();
- try {
- props.load(new FileInputStream(new File(".\\src\\config.ini")));
- } catch (IOException e) {
- JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- try {
- InitDB(props);
- } catch (Exception e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- InitFrame();
- try {
- InitDialogs();
- InitDepartGrid();
- InitPositionGrid();
- InitEmplnGrid();
- ShowDepartment();
- ShowFrame();
- } catch (SQLException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement