当前位置: 技术问答>java相关
请问怎么获得jTable中的所选中的一行的其中一列的值?
来源: 互联网 发布时间:2015-11-02
本文导语: 请问怎么获得jTable中的所选中的一行的其中一列的值? | table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ... ListSelectionModel rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener...
请问怎么获得jTable中的所选中的一行的其中一列的值?
|
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
...
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm =
(ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
...//no rows are selected
} else {
int selectedRow = lsm.getMinSelectionIndex();
...//selectedRow is selected
}
}
});
...
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm =
(ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
...//no rows are selected
} else {
int selectedRow = lsm.getMinSelectionIndex();
...//selectedRow is selected
}
}
});
|
table.getValueAt(table.getSelectedRow(), 0);
|
最好是
((DefaultTableModel)table.getModel()).getValueAt(table.getSelectedRow(), 0);
或者将列转化为模型列。不让显示的列变了之后会出错的。
具体的方法:int intmodelColumn = jTable1.convertColumnIndexToModel(inttableColumn);
((DefaultTableModel)table.getModel()).getValueAt(table.getSelectedRow(), 0);
或者将列转化为模型列。不让显示的列变了之后会出错的。
具体的方法:int intmodelColumn = jTable1.convertColumnIndexToModel(inttableColumn);
|
先定义:
DefaultTableModel tableModel=new DefaultTableModel;
JTable jTable1=new JTable(tableModel);
然后:
int row=jTable1.getSelectedRow;
int col=jTables.getSelectedCol;
取值:value=tableModel.getValueAt(row,col);
DefaultTableModel tableModel=new DefaultTableModel;
JTable jTable1=new JTable(tableModel);
然后:
int row=jTable1.getSelectedRow;
int col=jTables.getSelectedCol;
取值:value=tableModel.getValueAt(row,col);