[英]How to get cell value of jtable depending on which row is clicked
我試圖在我的jtable上使用一個更新方法,它連接到數據庫,並希望根據用戶點擊的哪一行填寫表單上的文本字段。 我知道我將需要一個getValueAt()方法但是我不確定如何根據用戶點擊的哪一行填寫哪一行。 我無法在Google或其他任何地方找到任何內容,因此任何信息都會有所幫助!
private final UrTableModel urTableModel;
private JTable urTable;
...
// 1. Create your table model class that should extends from DefaultTableModel, instantiate it
urTableModel=new UrTableModel();
// 2. creates table
table = TableUtils.createStandardSortableTable(urTableModel);
// 3. customize your table
table.setBackground(Color.WHITE);
table.getTableHeader().setReorderingAllowed(false);
// 4. Add the mouse listner to it
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
if (e.getClickCount() == 1) {
final JTable target = (JTable)e.getSource();
final int row = target.getSelectedRow();
final int column = target.getSelectedColumn();
// Cast to ur Object type
final UrObjctInCell urObjctInCell = (UrObjctInCell)target.getValueAt(row, column);
// TODO WHAT U WANT!
}
}
});
干杯,
您需要調用getValueAt()表的模型來獲取所需的值。 您還需要一個監聽器來監聽選擇。 因此,一旦用戶選擇了一行,您就可以調用getValueAt()來獲取該行中特定數據列的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.