Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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 我在jtable中选择时遇到了一个问题_Java - Fatal编程技术网

Java 我在jtable中选择时遇到了一个问题

Java 我在jtable中选择时遇到了一个问题,java,Java,我试图从table reading用户数据库中选择一行,并根据我在表中选择的选定行对其进行更改,并将字段和组合框更改为表中的选定行 public class Users extends JFrame implements ActionListener { JLabel username,password,type,department; JButton update,delete; JTextField user_f;JPasswordField password_f; JComboBox ct

我试图从table reading用户数据库中选择一行,并根据我在表中选择的选定行对其进行更改,并将字段和组合框更改为表中的选定行

public class Users extends JFrame implements ActionListener {
JLabel username,password,type,department;
JButton update,delete;
JTextField user_f;JPasswordField password_f;
JComboBox ctype,cdepartment;
JTable user_table;
JScrollPane scroll;
String[] ltype={"ST","TA","PR"};
String[] ldepartment={"CS","IT","IS"};
String Data[][];
String Header[]={"USERNAME","PASSWORD","TYPE","DEPARTMENT"};
ArrayList<domain.user> array=users_db.get_users();

public Users(){
    //show_Users();
}
public void show_Users(){
    Data=new String[array.size()][4];
    for(int i=0;i<array.size();i++){
       Data[i][0]=array.get(i).getUsername();
       Data[i][1]=array.get(i).getPassword();
       Data[i][2]=array.get(i).getType();
       Data[i][3]=array.get(i).getDepartment();}
    //Defining  Table
    user_table=new JTable(Data,Header);
    scroll=new JScrollPane(user_table);scroll.setBounds(0,0,500,375);
    ///Windows Components
    username=new JLabel("Username");password=new JLabel("Password");
    user_f=new JTextField();password_f=new JPasswordField();
    update=new JButton("Update");delete=new JButton("Delete");
    type=new JLabel("Type");ctype=new JComboBox(ltype);
    department=new JLabel("Department");cdepartment=new JComboBox(ldepartment);
    username.setBounds(15, 350, 90, 80);user_f.setBounds(15,400,200,30);
    password.setBounds(220, 350, 90, 80);password_f.setBounds(220,400,200,30);
    type.setBounds(15, 400, 90, 80);ctype.setBounds(15,450,200,30);
    department.setBounds(220, 400, 90, 80);cdepartment.setBounds(220,450,200,30);
    update.setBounds(120, 520, 90, 30);delete.setBounds(230, 520, 90, 30);

    //Adding Actions to button
    //update.addActionListener(this);delete.addActionListener(this);
    //Creating Object of mouse listener to the table
    DefaultTableModel model=(DefaultTableModel)user_table.getModel();
    int SRN=user_table.getSelectedRow();
    user_f.setText(model.getValueAt(SRN,0).toString());
    password_f.setText(model.getValueAt(SRN, 1).toString());
    ctype.setSelectedItem(model.getValueAt(SRN, 2).toString());
    cdepartment.setSelectedItem(model.getValueAt(SRN, 3).toString());            

    //End creating
    //Adding Components+ Window Settings
    add(scroll);
    add(username);add(user_f);
    add(password);add(password_f);
    add(type);add(ctype);add(department);add(cdepartment);
    add(update);add(delete);
    setSize(500,650);
    setLayout(null);
    setVisible(true);   
}
 }  
下面是一个画面截图,以澄清问题:


也许这段代码能帮上忙。您必须在行选择上附加侦听器,如下所示:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent event) {
            // do some actions here, for example
            // print first column value from selected row
            System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
        }
    });

您想了解java命名约定。你有点违反了它们,这让人很难读懂。。。我想你在这里没有一个好主意。没有主要的方法?旁白:你是否在建立一个计算机系统,人们的密码以明文形式存储/可用?这通常是不鼓励的。查找密码散列。不,实际上我有一个主要方法,但我正在为同事们构建这个项目。我还有一个问题要澄清我对event MouseAdapter和MouseListener以及ListSelectionListener的疑问。为什么ListSelectionEvent可以与组合框一起工作而MouseListenEvent不能?当我单击一行时,它在组合框中添加类型和部门,但不会更改组合框的选择,因为它是在所选行中指定的