import java.awt.EventQueue; 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.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 { // needs accompanying sqliteConnection.java and rs2xml.jar 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 textFieldName; private JTextField textFieldSurname; private JTextField textFieldAge; private JTextField textFieldComment; private JFrame frame; /* ========================================================================================================= */ public void refreshTable() { try { String query = "select EID, Name, Surname, 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. } 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, 624); //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); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(54, 20, 45, 25); //-250 EACH EXCEPT 100 contentPane.add(scrollPane); table = new JTable(); scrollPane.setViewportView(table); // added for grid lines, default is white 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")); textFieldAge.setText(rs.getString("Age")); } /*EID, Name, Surname, and Age can go into JTextField, but not into JTextArea. Strings to through o.k., but not the textFieldxxx items. String string = "453"; // JOptionPane.showMessageDialog(null, textFieldEID); ////////Early Monday JLabel lblNewLabel = new JLabel(string); lblNewLabel.setBounds(38, 134, 61, 16); frame.getContentPane().add(lblNewLabel); JTextField txtThisIsA = new JTextField(); txtThisIsA.setText(string); txtThisIsA.setBackground(new Color(255, 255, 204)); txtThisIsA.setBounds(383, 171, 130, 26); frame.getContentPane().add(txtThisIsA); txtThisIsA.setColumns(10); */ //I thought of one more way, but it doesn't work either. JTextArea txtrTheNumberShould = new JTextArea(); txtrTheNumberShould.setText((rs.getString("EID"))); txtrTheNumberShould.setBackground(new Color(255, 255, 255)); txtrTheNumberShould.setBounds(383, 219, 42, 26); frame.getContentPane().add(txtrTheNumberShould); JOptionPane.showMessageDialog(null, txtrTheNumberShould); 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();// /* ========================================= JComboBox =====================================*/ JLabel lblNewLabel = new JLabel("EID"); lblNewLabel.setBounds(60, 185, 150, 16); //24, 168, 61, 16 contentPane.add(lblNewLabel); textFieldEID = new JTextField(); textFieldEID.setBounds(113, 180, 160, 28); //113, 157, 134, 28 contentPane.add(textFieldEID); textFieldEID.setColumns(10); contentPane.add(textFieldEID); JLabel lblSeqNum = new JLabel("Sequence number"); lblSeqNum.setBounds(400, 185, 150, 16); contentPane.add(lblSeqNum); //String string22 = textFieldEID.getText().toString();///////So we've now got it in string form //JTextArea txtrStepNumber = new JTextArea(); //txtrStepNumber.setText(string22); //txtrStepNumber.setBounds(600, 185, 150, 16); //contentPane.add(txtrStepNumber); JTextArea txtrEmpID = new JTextArea(); contentPane.add(txtrEmpID); //contentPane.add(textFieldEID); txtrEmpID.setBounds(600, 185, 150, 16); textFieldEID.setColumns(10); //////////////////////////////////////////////////////////////////////////////// NAME /////////////////////////////////////////// textFieldName = new JTextField(); textFieldName.setBounds(173, 590, 900, 38); contentPane.add(textFieldName); textFieldName.setColumns(10); String string23 = textFieldEID.getText().toString();///////So we've now got it in string form /* txtrStepNumber.setText(string22); txtrStepNumber.setBounds(600, 185, 150, 16); contentPane.add(txtrStepNumber); Same failure String str23 = ""; str23 = textFieldName.getText().toString(); String string23 = textFieldName.getText().toString(); JTextArea txtrTxtrexplanation = new JTextArea(); txtrTxtrexplanation.setText(string23); txtrTxtrexplanation.setBounds(173, 220, 490, 74); contentPane.add(txtrTxtrexplanation); */ ////////////////////////////////////////////////////////////////////// SURNAME textFieldSurname = new JTextField(); textFieldSurname.setBounds(173, 640, 900, 38); contentPane.add(textFieldSurname); textFieldSurname.setColumns(10); JTextArea txtrquestion = new JTextArea(); txtrquestion.setText("AREA_Question"); txtrquestion.setBounds(173, 310, 490, 74); contentPane.add(txtrquestion); //// //////////////////////////////////////////////////////////////// AGE textFieldAge = new JTextField(); textFieldAge.setBounds(173, 690, 900, 38);// contentPane.add(textFieldAge); textFieldAge.setColumns(10); JTextArea txtrQuestion = new JTextArea(); txtrQuestion.setText("AREA_Response"); txtrQuestion.setBounds(173, 400, 490, 74); contentPane.add(txtrQuestion); /////////////////////////////////////////////////////////////////// COMMENT textFieldComment = new JTextField(); textFieldComment.setBounds(173, 740, 900, 38); contentPane.add(textFieldComment); textFieldComment.setColumns(10); JTextArea txtrComment = new JTextArea(); txtrComment.setText("AREA_Comment"); txtrComment.setBounds(173, 490, 490, 74); contentPane.add(txtrComment); JLabel lblInfo = new JLabel("Info"); lblInfo.setBounds(40, 600, 150,16); contentPane.add(lblInfo); JLabel lblAsk = new JLabel("Ask"); lblAsk.setBounds(40, 650, 150, 16); contentPane.add(lblAsk); JLabel lblResp = new JLabel("Respond"); lblResp.setBounds(40, 700, 150, 16); contentPane.add(lblResp); JLabel lblFeedback = new JLabel("Feedback"); lblFeedback.setBounds(40, 710, 150, 16); contentPane.add(lblFeedback); JLabel lblRemarks = new JLabel("Remarks"); lblFeedback.setBounds(40, 770, 150, 16); contentPane.add(lblRemarks); /////////////////////////////////////////////////////// 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, 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, textFieldAge.getText()); pst.execute(); JOptionPane.showMessageDialog(null, "Data saved"); pst.close(); } catch (Exception e1) { e1.printStackTrace(); } 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()+"' ,Age='"+textFieldAge.getText()+"' where EID='"+textFieldEID.getText()+"'"; PreparedStatement pst = connection.prepareStatement(query); pst.execute(); pst.close(); } catch (Exception e1) { e1.printStackTrace(); } 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"); pst.close(); } catch (Exception e1) { e1.printStackTrace(); } refreshTable(); } }); btnDelete.setBounds(64, 140, 117, 29);//64, 261, 117, 29 contentPane.add(btnDelete); }// end of employee info /*=========================================================================================================*/ } //*End of :extends frame