使用 Java 从 JTable 中按下按钮获取数据
Get data with button press from a JTable with Java
我的计算机科学学期项目我需要获取选定的行并从选定行的第一列中获取存储在 table 中的信息(按下按钮后)。获取信息后,我需要将其存储到先前初始化的变量中。我想不通,非常感谢任何帮助。
这就是我选择行并尝试从行中获取信息的方法。我正在使用一个按钮来 运行 这样它会根据从 table.
中存储的字符串中收集的信息从数据库中删除一行
if (theMainViewDisplay.bookingData.getSelectedRow() != -1) {
String bookingID;
DefaultTableModel model = (DefaultTableModel)theMainViewDisplay.bookingData.getModel();
model.removeRow(theMainViewDisplay.bookingData.getSelectedRow());
// Get stored varible
bookingID = model.getColumnName(1);
System.out.println(bookingID);
}
我尝试获取所选模型的索引。
为此,您可以为 table
编写一个鼠标侦听器
要获取值,您可以使用 table.getModel().getValueAt(ROW_NUMBER,COLUMN_NUMBER)
函数。在此代码中,raw 由鼠标单击事件选择。因此 table.rowAtPoint(e.getPoint())
函数 returns 鼠标点击的原始索引。
示例代码如下所示;
table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
String symbol=(String)table.getModel().getValueAt(table.rowAtPoint(e.getPoint()),0);
}
}
}
您可以阅读更多关于 JTABLES 和
Java Mouse click Events
没有鼠标侦听器也可以做到。您可以通过以下方式完成。
private class RemoveBookings implements ActionListener {
String bookingID;
@Override
public void actionPerformed(ActionEvent ae) {
if (theMainViewDisplay.bookingData.getSelectedRow() != -1) {
final DefaultTableModel model = (DefaultTableModel) theMainViewDisplay.bookingData.getModel();
// Get stored varible
int index = theMainViewDisplay.bookingData.getSelectedRow();
System.out.println(index);
theMainViewDisplay.bookingID = model.getValueAt(index, 0).toString();
theMainViewDisplay.pickUpLocation = model.getValueAt(index, 2).toString();
theMainViewDisplay.dropOffLocation = model.getValueAt(index, 3).toString();
System.out.println(theMainViewDisplay.bookingID);
// Remove booking from database
model.removeRow(theMainViewDisplay.bookingData.getSelectedRow());
theBackendModel.thePowList.remove(bookingID);
}
}
}
我的计算机科学学期项目我需要获取选定的行并从选定行的第一列中获取存储在 table 中的信息(按下按钮后)。获取信息后,我需要将其存储到先前初始化的变量中。我想不通,非常感谢任何帮助。
这就是我选择行并尝试从行中获取信息的方法。我正在使用一个按钮来 运行 这样它会根据从 table.
中存储的字符串中收集的信息从数据库中删除一行 if (theMainViewDisplay.bookingData.getSelectedRow() != -1) {
String bookingID;
DefaultTableModel model = (DefaultTableModel)theMainViewDisplay.bookingData.getModel();
model.removeRow(theMainViewDisplay.bookingData.getSelectedRow());
// Get stored varible
bookingID = model.getColumnName(1);
System.out.println(bookingID);
}
我尝试获取所选模型的索引。
为此,您可以为 table
编写一个鼠标侦听器要获取值,您可以使用 table.getModel().getValueAt(ROW_NUMBER,COLUMN_NUMBER)
函数。在此代码中,raw 由鼠标单击事件选择。因此 table.rowAtPoint(e.getPoint())
函数 returns 鼠标点击的原始索引。
示例代码如下所示;
table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
String symbol=(String)table.getModel().getValueAt(table.rowAtPoint(e.getPoint()),0);
}
}
}
您可以阅读更多关于 JTABLES 和 Java Mouse click Events
没有鼠标侦听器也可以做到。您可以通过以下方式完成。
private class RemoveBookings implements ActionListener {
String bookingID;
@Override
public void actionPerformed(ActionEvent ae) {
if (theMainViewDisplay.bookingData.getSelectedRow() != -1) {
final DefaultTableModel model = (DefaultTableModel) theMainViewDisplay.bookingData.getModel();
// Get stored varible
int index = theMainViewDisplay.bookingData.getSelectedRow();
System.out.println(index);
theMainViewDisplay.bookingID = model.getValueAt(index, 0).toString();
theMainViewDisplay.pickUpLocation = model.getValueAt(index, 2).toString();
theMainViewDisplay.dropOffLocation = model.getValueAt(index, 3).toString();
System.out.println(theMainViewDisplay.bookingID);
// Remove booking from database
model.removeRow(theMainViewDisplay.bookingData.getSelectedRow());
theBackendModel.thePowList.remove(bookingID);
}
}
}