import java.awt.EventQueue; //原來的 Based on Youtube tutorial by ProgrammingKnowledge import java.awt.BorderLayout; //原來的 import java.awt.Color; //原來的 import java.awt.Component; //原來的 import java.awt.Container; //原來的 import java.awt.EventQueue; //原來的 import java.awt.FocusTraversalPolicy; //原來的 import javax.swing.JFrame; //原來的 import javax.swing.JPanel; //原來的 import javax.swing.border.EmptyBorder; //原來的 import javax.swing.JLabel; //原來的 import javax.swing.JTextField; //原來的 import javax.swing.JTextArea; //原來的 import net.proteanit.sql.DbUtils; //原來的 import javax.swing.JLabel; //原來的 import javax.swing.JOptionPane; //原來的 import java.awt.Font; //原來的 import javax.swing.ImageIcon; import javax.swing.JButton; //原來的 import javax.swing.JTable; //原來的 import javax.swing.JScrollPane; //原來的 import java.awt.event.ActionListener; //原來的 import java.sql.*; //原來的 import java.util.Vector; //原來的 import java.sql.PreparedStatement; //原來的 import java.awt.event.ActionEvent; //原來的 import javax.swing.JTextField; //原來的 import javax.swing.JComboBox; //原來的 import java.sql.PreparedStatement; //原來的 public class EmployeeInfo extends JFrame { //原來的 //6 May revisions. Fixed a couple of dumb mistakes. Still can't be opened in design // mode, and the save, update, and delete functions do not work properly. private JPanel contentPane; //原來的 private JTable table; //原來的 private JComboBox comboBoxName; //原來的 /*=============================================================*/ /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { EmployeeInfo frame = new EmployeeInfo(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } Connection connection = null; private JTextField textFieldEID; private JTextField textFieldEID2; private JTextField textFieldName; private JTextField textFieldName2; private JTextField textFieldSurname; private JTextField textFieldPassword; private JTextField textFieldAge; private JTextField textFieldComment; private JFrame frame; private JTextArea txtrExplanation; private JTextArea txtrquestion; private JTextArea txtrQuestion; private JTextArea txtrComment; private JTextField textField2; private JTextField textField_1; private JTextField txtSurname2; private JTextField txtSurname3; private JTextField txtAge3; private JTextField txtCid; private JTextField txtCid2; private JTextField txtSid; /* ================================================================ */ public void refreshTable() { try { String query = "select EID, Name, Surname, Password, Age from EmployeeInfo"; PreparedStatement pst = connection.prepareStatement(query); ResultSet rs = pst.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(rs)); rs.close(); pst.close(); } catch (Exception e1) { e1.printStackTrace(); } } /* =================================================================== */ public void fillComboBox() { try { String query = "select * from EmployeeInfo"; PreparedStatement pst = connection.prepareStatement(query); ResultSet rs = pst.executeQuery(); while(rs.next()) { comboBoxName.addItem(rs.getString("EID")); // combobox shows ID numbe.r Using EID works. //JOptionPane.showMessageDialog(null, comboBoxName); /////21 April } table.setModel(DbUtils.resultSetToTableModel(rs)); //restored at 16:36 rs.close(); //restored at 16:36 pst.close(); //restored at 16:36 } catch (Exception e1) { e1.printStackTrace(); } } /** * Create the frame. */ /* =========================================================================*/ public EmployeeInfo() { // connect to data base connection = sqliteConnection.dbConnector(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 1266, 661); //Screen size. contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); /*--------------------------------------------------------------------*/ JButton btnLoadTable = new JButton("Load Database"); btnLoadTable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String query = "select * from EmployeeInfo"; PreparedStatement pst = connection.prepareStatement(query); ResultSet rs = pst.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(rs)); rs.close(); pst.close(); } catch (Exception e1) { e1.printStackTrace(); } } }); btnLoadTable.setBounds(392, 48, 206, 29); contentPane.add(btnLoadTable); /*-------------------------------------------------------------*/ JButton btnTrick = new JButton("Load Database"); btnLoadTable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String query = "select * from EmployeeInfo"; PreparedStatement pst = connection.prepareStatement(query); ResultSet rs = pst.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(rs)); rs.close(); pst.close(); } catch (Exception e1) { e1.printStackTrace(); } } }); btnTrick.setBounds(858, 564, 402, -561); contentPane.add(btnTrick); /*----------------------------------------------------------------*/ JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(54, 20, 45, 25); //-250 EACH EXCEPT 100 contentPane.add(scrollPane); table = new JTable(); // 4 May restored The combobox came back scrollPane.setViewportView(table);// 4 May restored // added for grid lines, default is white // 4 May table.setGridColor(Color.black); comboBoxName = new JComboBox(); comboBoxName.addActionListener(new ActionListener() { //1 public void actionPerformed(ActionEvent arg0) { //2 try { //3 String query = "select * from EmployeeInfo where EID = ? "; PreparedStatement pst = connection.prepareStatement (query); pst.setString(1, (String) comboBoxName.getSelectedItem()); // was 1 ResultSet rs = pst.executeQuery(); //JOptionPane.showMessageDialog(null, textFieldEID); ////////////////////////////////////21 April ////shows number but lagging one step while(rs.next()) { textFieldEID.setText(rs.getString("EID")); textFieldName.setText(rs.getString("Name")); textFieldSurname.setText(rs.getString("Surname")); textFieldPassword.setText(rs.getString("Password")); textFieldAge.setText(rs.getString("Age")); } String str = ""; str= textFieldEID.getText();////////////////////////////////////////// JLabel lblSteps = new JLabel(str); lblSteps.setBounds(383, 134,61, 16);//////// . frame.getContentPane().add(lblSteps); pst.close();///////////////////////////////////////////////////////// } //3 catch(Exception j) { //4 j.printStackTrace (); } //4 } //2 }); //1 //) comboBoxName.setBounds(31, 35, 216, 55);//added these four 18:43 contentPane.add(comboBoxName);//this got me the bes back, but nothing filled out left side refreshTable();// fillComboBox();//// 4 May restored The program has retiurned to normal function. /* ========================================= JComboBox =====================================*/ JLabel lblNewLabel = new JLabel("EID"); lblNewLabel.setBounds(44, 180, 55, 16); contentPane.add(lblNewLabel); // String text = textFieldEID.getText(); // JOptionPane.showMessageDialog(null,text); textFieldEID = textFieldEID2 = new JTextField(); textFieldEID2.setBackground(Color.YELLOW); textFieldEID2.setForeground(Color.MAGENTA); textFieldEID2.setText("EID2"); textFieldEID.setBounds(112, 175, 160, 28); contentPane.add(textFieldEID); textFieldEID.setColumns(10); //////////////////////////////////////////////////////////////////////////////// NAME /////////////////////////////////////////// textFieldName = textFieldName = new JTextField(); textFieldName.setBackground(Color.CYAN); textFieldName.setText("NAME"); textFieldName.setBounds(50, 220, 720, 38); contentPane.add(textFieldName); textFieldName.setColumns(10); String shuzi = textFieldName.getText().toString(); JOptionPane.showMessageDialog(null,"Name:" + shuzi); /////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// SURNAME textFieldSurname = new JTextField(); textFieldSurname.setBackground(Color.PINK); textFieldSurname.setText("Surname"); textFieldSurname.setBounds(50, 320, 720, 38); contentPane.add(textFieldSurname); textFieldSurname.setColumns(10); //// //////////////////////////////////////////////////////////////// AGE textFieldAge = new JTextField(); textFieldAge.setBackground(Color.YELLOW); textFieldAge.setText("age"); textFieldAge.setBounds(50, 420, 720, 38); contentPane.add(textFieldAge); textFieldAge.setColumns(10); /////////////////////////////////////////////////////////////////// //JTextArea txtrComment = new JTextArea(); //txtrComment.setText("AREA_Comment"); //txtrComment.setBounds(50, 340, 720, 74);ß //contentPane.add(txtrComment); textField2 = new JTextField(); textField2.setBackground(Color.CYAN); textField2.setBounds(50, 250, 720, 38); contentPane.add(textField2); textField2.setColumns(10); textField_1 = new JTextField(); textField_1.setBackground(Color.CYAN); textField_1.setBounds(50, 280, 720, 38); contentPane.add(textField_1); textField_1.setColumns(10); JLabel lblName = new JLabel(" Name"); lblName.setBounds(31, 3, 61, 16); contentPane.add(lblName); txtSurname2 = new JTextField(); txtSurname2.setBackground(Color.PINK); txtSurname2.setText("surname2"); txtSurname2.setBounds(50, 350, 720, 38); contentPane.add(txtSurname2); txtSurname2.setColumns(10); txtSurname3 = new JTextField(); txtSurname3.setBackground(Color.PINK); txtSurname3.setText("surname3"); txtSurname3.setBounds(50, 380, 720, 38); contentPane.add(txtSurname3); txtSurname3.setColumns(10); JTextField txtAge2 = new JTextField(); txtAge2.setBackground(Color.YELLOW); txtAge2.setText("age2"); txtAge2.setBounds(50, 450, 720, 38); contentPane.add(txtAge2); txtAge2.setColumns(10); txtAge3 = new JTextField(); txtAge3.setBackground(Color.YELLOW); txtAge3.setText("age3"); txtAge3.setBounds(50, 480, 720, 38); contentPane.add(txtAge3); txtAge3.setColumns(10); txtCid = new JTextField(); txtCid.setBackground(new Color(102, 255, 0)); txtCid.setText("cid3"); txtCid.setBounds(50, 520, 720, 38); contentPane.add(txtCid); txtCid.setColumns(10); txtCid2 = new JTextField(); txtCid2.setBackground(new Color(153, 102, 51)); txtCid2.setText("cid2"); txtCid2.setBounds(50, 550, 720, 38); contentPane.add(txtCid2); txtCid2.setColumns(10); textFieldComment = new JTextField(); textFieldComment.setBackground(new Color(112, 153, 255)); textFieldComment.setText("Comment"); textFieldComment.setBounds(50, 580, 720, 38); contentPane.add(textFieldComment); textFieldComment.setColumns(10); txtSid = new JTextField(); txtSid.setBackground(new Color(204, 102, 51)); txtSid.setText("sid"); txtSid.setBounds(50, 610, 720, 38); contentPane.add(txtSid); txtSid.setColumns(10); /////////////////////////////////////////////////////////////////////////////// JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String query = "select * from EmployeeInfo"; query = "insert into EmployeeInfo (EID, Name, Surname, Password. Age) values(?, ?, ?, ?, ?)"; PreparedStatement pst = connection.prepareStatement(query); pst.setString(1, textFieldEID.getText()); pst.setString(2, textFieldName.getText()); pst.setString(3, textFieldSurname.getText()); pst.setString(4, textFieldPassword.getText()); pst.setString(5, textFieldAge.getText()); pst.execute(); JOptionPane.showMessageDialog(null, "Data saved"); pst.close(); } catch (Exception e1) { JOptionPane.showMessageDialog(null, "Data save failed"); } refreshTable(); } }); btnSave.setBounds(64, 80, 117, 29); contentPane.add(btnSave); JButton btnUpdate = new JButton("Update"); btnUpdate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String query = "update EmployeeInfo set EID='"+textFieldEID.getText()+"' ,name='"+textFieldName.getText()+"',Surname='"+textFieldSurname.getText()+"'.'"+textFieldPassword.getText()+"' , ,Age='"+textFieldAge.getText()+"' where EID='"+textFieldEID.getText()+"'"; PreparedStatement pst = connection.prepareStatement(query); pst.execute(); // JOptionPane.showMessageDialog(null, "Data updateded)"; pst.close(); } catch (Exception e1) { JOptionPane.showMessageDialog(null, "Data update failed."); } refreshTable(); } }); btnUpdate.setBounds(64, 110, 117, 29); contentPane.add(btnUpdate); /*------------------------------------------------------------------*/ JButton btnDelete = new JButton("Delete"); btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String query = "delete from EmployeeInfo where EID='"+textFieldEID.getText()+"' "; PreparedStatement pst = connection.prepareStatement(query); pst.execute(); JOptionPane.showMessageDialog(null, "Data deleted"); // 4 May pst.close(); } catch (Exception e1) { JOptionPane.showMessageDialog(null, "Data not deleted"); } refreshTable(); } }); btnDelete.setBounds(64, 140, 117, 29);//64, 261, 117, 29 contentPane.add(btnDelete); // if(textFieldName != null) == "⎈"{ // ImageIcon icon = new ImageIcon (textFieldPassword); // } String string = textFieldSurname.getText(); lblNewLabel = new JLabel(""); ImageIcon icon = new ImageIcon ("Images/flowers.png"); JOptionPane.showMessageDialog(null, string); lblNewLabel.setIcon(icon); lblNewLabel.setBounds(770, 31,400, 500); contentPane.add(lblNewLabel); /* JLabel lblFoto = new JLabel(""); ImageIcon icon = new ImageIcon ("Images/ET.png"); lblNewLabel.setIcon(icon); lblFoto.setBounds(914, 15, 332, 429); contentPane.add(lblFoto); frame.getContentPane().add(lblFoto); */ }// end of employee info } //*End of :extends frame