【发布时间】:2015-07-08 00:53:07
【问题描述】:
我正在制作我的项目,但存在这个问题。我无法选择整行。我尝试了 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);
当一个字符串被插入到 JtextField 时,我有这个 DocumentListener 来改变 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 新手!谢谢。
【问题讨论】: