Java jtable 允许行选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14748110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java jtable allow row selection
提问by user667430
I am trying to make a jtable which displays a list of users. The table should allow users to select an entire row but not allow editing of the cells.
我正在尝试制作一个显示用户列表的 jtable。该表格应允许用户选择整行但不允许编辑单元格。
So far i have this, it stops them from editing cells but how to i allow them to select the rows instead of cells?
到目前为止,我有这个,它阻止他们编辑单元格,但如何让他们选择行而不是单元格?
DefaultTableModel userTableModel = new DefaultTableModel(new Object[]{"Customer ID", "First Name", "Last Name"}, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
And this i show i am populating the table:
这表明我正在填充表格:
public void refreshCustomersList() throws SQLException, ClassNotFoundException {
UserBeanList userList = dbConnector.getUserData();
for (int i = 0; i < userList.size(); i++) {
UserBean userBean = userList.getUserBeanAt(i);
String[] data = new String[3];
data[0] = userBean.getCustomerID();
data[1] = userBean.getFirstName();
data[2] = userBean.getLastName();
userTableModel.addRow(data);
}
tableCustomers.setModel(userTableModel);
}
As i said i have disabled cell editing but how do i only allow row selection.
正如我所说,我已禁用单元格编辑,但我如何只允许行选择。
I have seen posts from other people saying i should put this but i not sure were to put it.
我看到其他人的帖子说我应该放这个,但我不确定是否放它。
selectionTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Any help would be great.
任何帮助都会很棒。
回答by JustDanyul
Have you tried setRowSelectionAllowed(true)
on your JTable instance?
你试过setRowSelectionAllowed(true)
你的 JTable 实例吗?
I would suggest trying to look at the javadocs http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html#setRowSelectionAllowed(boolean)
我建议尝试查看 javadocs http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html#setRowSelectionAllowed(boolean)
and read the tutorial linked from the javadocs: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
并阅读从 javadocs 链接的教程:http: //docs.oracle.com/javase/tutorial/uiswing/components/table.html
They are pretty thorough :)
他们非常彻底:)
回答by Java Man
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int selectedRow;
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
int numCols = jTable1.getColumnCount();
model = (DefaultTableModel) jTable1.getModel();
System.out.print(" \n row " + selectedRow + ":");
for (int j = 0; j < numCols; j++)
{
System.out.print(" " + model.getValueAt(selectedRow, j));
}
}
});
}
Using this you can get value of whole row where u click on particular row.
使用它,您可以获得整行的值,您可以在其中单击特定行。