我正在制作我的项目并且存在这个问题。我无法选择整行。我尝试了setRowSelectionAllowed(true)并重写了isCellEditable(),但没有真正有效。
我有这段代码。
我有一张桌子,首先显示我现有的所有产品。
String query = "select * from tbl_items where status = 'Available'";
Connection cn_oh_readRows = null;
Connection cn_oh_readContent = null;
int countRows = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
cn_oh_readRows = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");
Statement state = cn_oh_readRows.createStatement();
ResultSet rowsCount = state.executeQuery(query);
while (rowsCount.next()){
countRows++;
}
}
catch(Exception ex){}
Object [][] rows_data = new Object [countRows][7];
try {
Class.forName("com.mysql.jdbc.Driver");
cn_oh_readContent = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");
Statement state = cn_oh_readContent.createStatement();
ResultSet rowsRead = state.executeQuery(query);
int c = 0;
while (rowsRead.next()) {
rows_data[c][0] = rowsRead.getString(1);
rows_data[c][1] = rowsRead.getString(2);
rows_data[c][2] = rowsRead.getString(3);
rows_data[c][3] = rowsRead.getString(4);
rows_data[c][4] = rowsRead.getString(5);
rows_data[c][5] = rowsRead.getString(6);
rows_data[c][6] = rowsRead.getString(7);
c++;
}
}
catch(Exception ex){};
Object columnNames [] = {"Item code", "Item name", "Brand", "Original Price", "Retail Price", "Quantity", "Classification"};
DefaultTableModel tableModel = new DefaultTableModel(rows_data, columnNames){
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
tbl_onHand = new JTable(rows_data, columnNames);
tbl_onHand.setAutoCreateRowSorter(true);
tbl_onHand.setShowGrid(false);
tbl_onHand.getTableHeader().setReorderingAllowed(false);
tbl_onHand.getTableHeader().setResizingAllowed(false);
tbl_onHand.setModel(tableModel);
tbl_onHand.setRowSelectionAllowed(true);
tbl_onHand.setSelectionBackground(Color.pink);
sp_tbl_onHand = new JScrollPane(tbl_onHand);
sp_tbl_onHand.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sp_tbl_onHand.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp_tbl_onHand.setBounds(0, 40, 780, 232);
然后就是这个用于搜索的JTextField。
txt_search_onHand = new JTextField();
txt_search_onHand.setBorder(new border_round().round);
txt_search_onHand.setBounds(2, 15, 210, 20);
我有这个DocumentListener来在将字符串插入JtextField时更改JTable的值。
DocumentListener dl_onHand = new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {proc_onHand();}
@Override
public void insertUpdate(DocumentEvent e) {proc_onHand();}
@Override
public void changedUpdate(DocumentEvent e) {}
};
这是proc_onHand():
private void proc_onHand() {
String search_onHand = txt_search_onHand.getText();
String query_onHand = "select * from tbl_items where status = 'Available' and item_code like '%" + search_onHand + "%' or "
+ "status = 'Available' and name like '%"+ search_onHand +"%' or "
+ "status = 'Available' and brand like '%" + search_onHand + "%' or "
+ "status = 'Available' and classification like '%" + search_onHand + "%'";
Connection cn_oh_readRows = null;
Connection cn_oh_readContent = null;
int countRows = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
cn_oh_readRows = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");
Statement state = cn_oh_readRows.createStatement();
ResultSet rowsCount = state.executeQuery(query_onHand);
while (rowsCount.next()){
countRows++;
}
}
catch(Exception ex){}
Object [][] rows_data = new Object [countRows][7];
try {
Class.forName("com.mysql.jdbc.Driver");
cn_oh_readContent = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");
Statement state = cn_oh_readContent.createStatement();
ResultSet rowsRead = state.executeQuery(query_onHand);
int c = 0;
while (rowsRead.next()) {
rows_data[c][0] = rowsRead.getString(1);
rows_data[c][1] = rowsRead.getString(2);
rows_data[c][2] = rowsRead.getString(3);
rows_data[c][3] = rowsRead.getString(4);
rows_data[c][4] = rowsRead.getString(5);
rows_data[c][5] = rowsRead.getString(6);
rows_data[c][6] = rowsRead.getString(7);
c++;
}
}
catch(Exception ex){};
Object columnNames [] = {"Item code", "Item name", "Brand", "Original Price", "Retail Price", "Quantity", "Classification"};
DefaultTableModel tableModel = new DefaultTableModel(rows_data, columnNames){
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
tbl_onHand = new JTable(rows_data, columnNames);
tbl_onHand.setAutoCreateRowSorter(true);
tbl_onHand.setShowGrid(false);
tbl_onHand.setSelectionBackground(Color.pink);
tbl_onHand.getTableHeader().setReorderingAllowed(false);
tbl_onHand.getTableHeader().setResizingAllowed(false);
tbl_onHand.setModel(tableModel);
tbl_onHand.setRowSelectionAllowed(true);
sp_tbl_onHand = new JScrollPane(tbl_onHand);
sp_tbl_onHand.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sp_tbl_onHand.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp_tbl_onHand.setBounds(0, 40, 780, 232);
pnl_onHand.add(sp_tbl_onHand, BorderLayout.CENTER);
}
我可以在临时查看表格时选择整行,其中显示所有现有产品。但是当我在txt_search_onHand中插入一个字符串来过滤JTable中的值时,无法选择JTable中的整行结果,而是选择了它,而只选择了几个单元格。我必须拖动鼠标才能选择所有单元格。
请帮帮我!我做得好吗?请指导我。我是Java新手!感谢。
答案 0 :(得分:0)
table.setSelectionModel(ListSelectionModel.SINGLE_SELECTION);