Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI表将某些行添加到监视列表_Java_Mysql_Swing_Jdbc - Fatal编程技术网

Java GUI表将某些行添加到监视列表

Java GUI表将某些行添加到监视列表,java,mysql,swing,jdbc,Java,Mysql,Swing,Jdbc,好的,我正在设计一个应用程序,它将被许多使用JTable的用户使用,其中表的数据是使用JDBC拉入的。我正在考虑添加一个监视列表,以便用户可以在GUI中监视表中的某些行。这里是我的问题,因为我正在从JDBC中提取表的数据,所以我不需要为使用该应用程序的每个用户创建一个数据库MySql表,这样一个用户就可以向监视列表中添加行,而其他用户可以向监视列表中添加其他行,并排除其他用户可能已经包含在监视列表中的行。因为我的解决方案是在数据库中为监视列表的值添加一个额外的列,如果该值为true,则只显示这些

好的,我正在设计一个应用程序,它将被许多使用JTable的用户使用,其中表的数据是使用JDBC拉入的。我正在考虑添加一个监视列表,以便用户可以在GUI中监视表中的某些行。这里是我的问题,因为我正在从JDBC中提取表的数据,所以我不需要为使用该应用程序的每个用户创建一个数据库MySql表,这样一个用户就可以向监视列表中添加行,而其他用户可以向监视列表中添加其他行,并排除其他用户可能已经包含在监视列表中的行。因为我的解决方案是在数据库中为监视列表的值添加一个额外的列,如果该值为true,则只显示这些行,如果该值为true,则不显示。但这个解决方案涉及为每个用户创建一个数据库表,这不是我想要的,而是将这些行添加到监视列表的另一种方法

public class EmployeeSearchApp extends JFrame {
    private static  Connection connect;
    private JLabel title = new JLabel();
    private JPanel contentPane;
    private JTextField lastNameTextField;
    private JButton btnSearch;
    private JScrollPane scrollPane;
    private JTable table;
    private JButton appartmentformbutton = new JButton();
    private JButton selectbutton = new JButton();
    private JButton logoutexitbutton = new JButton();
    private JComboBox  addresscb = new JComboBox();
    private JComboBox  sexcb = new JComboBox();
    private JComboBox  agecb = new JComboBox();
    private JComboBox coursecb = new JComboBox();
    private JComboBox jobcb = new JComboBox();  
    static String loginusername;
    private EmployeeDAO employeeDAO;
    List<Employee> employees = null;
    static EmployeeSearchApp searchouse = new EmployeeSearchApp(loginusername);
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    searchouse.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public EmployeeSearchApp(String loginusername) {    
         this.loginusername=loginusername;
            if(loginusername!=null){
            System.out.print(loginusername);
            }
        // create the DAO
        try {
            employeeDAO = new EmployeeDAO();
        } catch (Exception exc) {
            JOptionPane.showMessageDialog(this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE); 
        }
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Search for house");
        setAutoRequestFocus(false);
        setBackground(new java.awt.Color(204, 204, 255));
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBackground(new java.awt.Color(204, 204, 255));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        title.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        title.setText("               Looking for Appartment");
        JPanel panel = new JPanel();
        panel.setBackground(new java.awt.Color(204, 204, 255));
        FlowLayout flowLayout = (FlowLayout) panel.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        contentPane.add(panel, BorderLayout.NORTH);
        JLabel lblEnterLastName = new JLabel("Enter last name");
        panel.add(title);
        panel.add(lblEnterLastName);
        lastNameTextField = new JTextField();
        panel.add(lastNameTextField);
        lastNameTextField.setColumns(10);   
        logoutexitbutton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                Component frame = null;
                int n = JOptionPane.showConfirmDialog(
                        frame,
                        "Exit to Main Screen",
                        "Exit to Main Screen",
                        JOptionPane.WARNING_MESSAGE);
                        searchouse.dispose();
                        intro  cancellogin = new intro();
                        cancellogin.setVisible(true);
            }
        });
        addresscb.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Location", 
                "Willow View", "Carton Drive", "Ballykeeran", "Moydrum",
                "Valley Court", "Auburn Heights", "Cypress Gardens", "Old Willow Park", "Old Willow Place",
                "Willow Park", "Wellmount Apartments", "Erris Grove, Willow Park", "Willow Green",
                "Cartontroy", "Crystal Village", "Willow Grove", "Willow Cresent",
                "Thornbury Drive", "Willow Place, Willow Park", "Willow View", "Willow Drive",
                "Small Apartments", "Other"}));
        sexcb.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Sex", "Male", "Female"}));
        agecb.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Age", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40"}));
        coursecb.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Course", "Software Design", "Cooking", "Science" }));
        jobcb.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Job", "Guard", "Teacher", "Farmer" }));
        appartmentformbutton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        appartmentformbutton.setText("<html>Appartment<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Form</html>");
        logoutexitbutton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        logoutexitbutton.setText("Log Out");
        selectbutton.setFont(new java.awt.Font("Tahoma", 1, 11)); 
        selectbutton.setText("Select Row");
        btnSearch = new JButton("Search");
        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Get last name from the text field
                // Call DAO and get employees for the last name
                // If last name is empty, then get all employees
                // Print out employees              
                try {
                    String lastName = lastNameTextField.getText();
                    List<Employee> employees = null;
                    if (lastName != null && lastName.trim().length() > 0) {
                        employees = employeeDAO.searchEmployees(lastName);
                    } else {
                        employees = employeeDAO.getAllEmployees();
                    }
                    // create the model and update the "table"
                    EmployeeTableModel model = new EmployeeTableModel(employees);
                    table.setModel(model);
                    //model.removeRow(table.getSelectedRow());
                    //DefaultListModel<String> defaultListModel = (DefaultListModel<String>)EmployeeTableModel.getModel();      
                    /*
                    for (Employee temp : employees) {
                        System.out.println(temp);
                    }
                    */
                } catch (Exception exc) {
                    JOptionPane.showMessageDialog(EmployeeSearchApp.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE); 
                }   
            }
        });
        addresscb.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent event) 
            {
                try {
                    String lastName = addresscb.getSelectedItem().toString();
                    System.out.print(lastName);
                    List<Employee> employees = null;
                    if (lastName != null && lastName.trim().length() > 0) {
                        employees = employeeDAO.searchEmployeesbyaddress(lastName);
                    } else {
                        employees = employeeDAO.getAllEmployees();
                    }       
                        EmployeeTableModel model = new EmployeeTableModel(employees);
                        table.setModel(model);
                } catch (Exception exc) {
                    JOptionPane.showMessageDialog(EmployeeSearchApp.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE); 
                }   
            }
        });

//Add to arraylist where users id is equal to username only display these rows      
/*Create button on form
Add selected rows equal to Arraylist
Create an arraylist step 1 where users id ="bob"
Create button display
where users id="bob"
Display Arraylist*/

        selectbutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            /*int selectedRow = table.getSelectedRow();
            //TableRowModel tcm = table.getColumnModel();
    //      if(selectedRow != -1) {
                //table.removeColumn(table.getColumnModel().getSelectedColumn());
                table.getSelectedRow();
                //got selected row
                //database select Apartment get name and id
                //System.out.print(loginusername + ","+loginpassword);
                table.convertRowIndexToModel(table.getSelectedRow());
                System.out.print(selectedRow);
                try {
                    // get the selected row
                    int row = table.getSelectedRow();
                    // make sure a row is selected
                    if (row < 0) {
                        JOptionPane.showMessageDialog(EmployeeSearchApp.this, 
                                "You must select an employee", "Error", JOptionPane.ERROR_MESSAGE);             
                        return;
                    }
                    JTextField releaseYearField = new JTextField();
                    //int decision = JOptionPane.showConfirmDialog(null, fields , "Create Dialog" , JOptionPane.OK_CANCEL_OPTION);
                    // prompt the user
                    int response = JOptionPane.showConfirmDialog(
                            EmployeeSearchApp.this, "Are you sure this is the appartment you want to choose, Please take note of phone number and address", "Confirm", 
                            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (response != JOptionPane.YES_OPTION) {
                        return;
                    }
                    // get the current employee
                    Employee tempEmployee = (Employee) table.getValueAt(row, EmployeeTableModel.OBJECT_COL);
                    //((DefaultTableModel)table.getModel()).removeRow(table.getSelectedRow());
                    // delete the employee
                    employeeDAO.deleteEmployee(tempEmployee.getacc_id());
                    employeeDAO.updateEmployee(tempEmployee.getacc_id(), loginusername );
                    // refresh GUI
System.out.print(tempEmployee.getaddress());
                    // show success message
                    JOptionPane.showMessageDialog(EmployeeSearchApp.this,
                            "Appartment confirmed request."+ 
                            "\n\nAddress: " + tempEmployee.getaddress()
                            + "\nHouse number: " + tempEmployee.gethouse_num()
                            + "\nLandlords Name: " + tempEmployee.getLandlords_first_Name()
                            + " " + tempEmployee.getLandlords_Surname()
                            + "\nLandlords phone number: " + tempEmployee.getMobile_Num(), "Error",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception exc) {
                    JOptionPane.showMessageDialog(EmployeeSearchApp.this,
                            "Error deleting employee: " + exc.getMessage(), "Error",
                            JOptionPane.ERROR_MESSAGE);
                }

*/          
                 table.convertRowIndexToModel(table.getSelectedRow()); 
            }});
        panel.add(btnSearch);
        panel.add(appartmentformbutton);
        panel.add(selectbutton);
        panel.add(logoutexitbutton);
        panel.add(addresscb);
        panel.add(sexcb);
        panel.add(agecb );
        panel.add(coursecb);
        panel.add(jobcb);   
        scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.SOUTH);
        table = new JTable();
        scrollPane.setViewportView(table);
    }
}
公共类EmployeeSearchApp扩展JFrame{
专用静态连接;
私有JLabel title=新JLabel();
私有JPanel内容窗格;
私有JTextField lastnamextfield;
私人JButton btnSearch;
私有JScrollPane滚动窗格;
专用JTable表;
私有JButton装置formbutton=新JButton();
private JButton selectbutton=new JButton();
私有JButton logoutexitbutton=newjbutton();
私有JComboBox addresscb=新JComboBox();
私有JComboBox sexcb=新JComboBox();
private JComboBox agecb=new JComboBox();
private JComboBox coursebc=new JComboBox();
私有JComboBox jobcb=新JComboBox();
静态字符串登录名;
私人雇员道雇员道;
列出员工=空;
静态EmployeeSearchApp searchouse=新EmployeeSearchApp(loginusername);
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
searchouse.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共EmployeeSearchApp(字符串登录名){
this.loginusername=loginusername;
if(loginusername!=null){
系统输出打印(loginusername);
}
//创建刀
试一试{
employeeDAO=新的employeeDAO();
}捕获(异常exc){
showMessageDialog(此“错误:”+exc,“错误”,JOptionPane.Error\u消息);
}
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“搜索房屋”);
setAutoRequestFocus(假);
挫折背景(新java.awt.Color(204204255));
立根(100100450300);
contentPane=newjpanel();
setBackground(新的java.awt.Color(204204255));
setboorder(新的EmptyBorder(5,5,5,5));
setLayout(新的BorderLayout(0,0));
setContentPane(contentPane);
title.setFont(新java.awt.Font(“Tahoma”,1,24));//NOI18N
title.setText(“寻找公寓”);
JPanel面板=新的JPanel();
setBackground(新java.awt.Color(204204255));
FlowLayout=(FlowLayout)panel.getLayout();
flowLayout.setAlignment(flowLayout.LEFT);
添加(面板,BorderLayout.NORTH);
JLabel lblEnterLastName=新JLabel(“输入姓氏”);
小组.添加(标题);
panel.add(lblEnterLastName);
lastNameTextField=新的JTextField();
panel.add(lastNameTextField);
lastNameTextField.setColumns(10);
logoutexitbutton.addActionListener(新java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
组件帧=空;
int n=JOptionPane.showConfirmDialog(
框架
“退出主屏幕”,
“退出主屏幕”,
JOptionPane。警告消息);
searchouse.dispose();
intro cancelogin=新的intro();
取消登录设置可见(true);
}
});
addresscb.setModel(新javax.swing.DefaultComboxModel(新字符串[]{“位置”,
“柳树景观”、“纸箱驱动”、“巴里基兰”、“莫伊德鲁姆”,
“谷庭”、“奥本高地”、“柏树花园”、“老柳树公园”、“老柳树广场”,
“柳树公园”、“威尔蒙特公寓”、“埃里斯格罗夫、柳树公园”、“柳树绿”,
“Cartontroy”、“水晶村”、“柳树林”、“柳树峰”,
“桑伯里大道”、“柳树公园柳树广场”、“柳树景观”、“柳树大道”,
“小公寓”、“其他”});
setModel(新的javax.swing.DefaultComboxModel(新的字符串[]{“Sex”、“Male”、“Female”}));
agecb.setModel(新的javax.swing.DefaultComboxModel(新字符串[]{“Age”、“18”、“19”、“20”、“21”、“22”、“23”、“24”、“25”、“26”、“27”、“28”、“29”、“30”、“31”、“32”、“33”、“34”、“35”、“36”、“37”、“38”、“39”、“40”);
setModel(新的javax.swing.DefaultComboxModel(新字符串[]{“课程”、“软件设计”、“烹饪”、“科学”});
setModel(新的javax.swing.DefaultComboxModel(新字符串[]{“Job”、“Guard”、“Teacher”、“Farmer”});
ApactureFormButton.setFont(新的java.awt.Font(“Tahoma”,1,11));//NOI18N
appartmentformbutton.setText(“Appartment
表单”); logoutexitbutton.setFont(新的java.awt.Font(“Tahoma”,1,11));//NOI18N logoutexitbutton.setText(“注销”); selectbutton.setFont(新的java.awt.Font(“Tahoma”,1,11));
+----------+---------+-------------+
| user key | row key | primary key |
+----------+---------+-------------+
select * from data where rowKey in (select rowKey from wishList where data.rowKey = wishList.data and userKey = ?)