[英]Java: How to use an object from one mouseListener to another class?
I need to use the object 'urObjectInCell' in mouseListener of 'table' to another class BtnDelete1. 我需要将“表”的mouseListener中的对象“ urObjectInCell”用于另一个类BtnDelete1。
Here's my Mouse Listener Code: 这是我的鼠标侦听器代码:
JTable table;
public FirstSwingApp(){
super();
table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
if (e.getClickCount() == 1) {
final JTable target = (JTable)e.getSource();
final int row = target.getSelectedRow();
final int column = 1;
// Cast to ur Object type
urObjctInCell = target.getValueAt(row, column);
}
}
});
friendNo = urObjctInCell.toString();
I tried storing the object in friendNo string which has been declared earlier. 我尝试将对象存储在前面已声明的friendNo字符串中。 But I don't think the friendNo is taking the value of the object.
但是我不认为friendNo正在获取对象的价值。
Here's my Class BtnDelete1 code: 这是我的BtnDelete1类代码:
public class BtnDelete1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String fnumber = friendNo;
CallableStatement dstmt = null;
CallableStatement cstmt = null;
ResultSet rs;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Contact_Manager?user=root");
String SQL = "{call delete_contact (?)}";
String disQuery = "select * from FRIEND";
dstmt = conn.prepareCall(disQuery);
cstmt = conn.prepareCall(SQL);
cstmt.setString(1, fnumber);
cstmt.executeQuery();
rs = dstmt.executeQuery();
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
// It creates and displays the table
model.setDataVector(data, columnNames);
// Closes the Connection
dstmt.close();
System.out.println("Success!!");
} catch (SQLException ex) {
System.out.println("Error in connection: " + ex.getMessage());
}
}
}
The value of urObjectInCell object obtained from mouseListener is to be used to delete a row in the Jtable 'table'. 从mouseListener获得的urObjectInCell对象的值将用于删除Jtable“表”中的一行。
I think that you may be thinking this out wrong. 我认为您可能会认为这是错误的。 Rather than trying to mix a MouseListener and an ActionListener in some unholy matrimony, why not instead simply get the selected cell from the JTable when the ActionListener is notified?
与其尝试在一些邪恶的婚姻中混合使用MouseListener和ActionListener,还不如在通知ActionListener时从JTable中简单地获取选定的单元格呢?
You could do this by giving the JTable-holding class a method for extracting a reference to the selected JTable cell, give the ActionListener a reference to this class, and have the ActionListener call this method when it has been notified and its callback method called. 您可以通过为JTable-holding类提供一种方法来提取对选定JTable单元的引用,为ActionListener提供对该类的引用,并让ActionListener在收到通知后调用此方法并调用其回调方法来实现此目的。
For example say you have a JTable held in a GUI called NoMouseListenerNeeded: 例如,假设您有一个名为NoMouseListenerNeeded的GUI中保存的JTable:
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
@SuppressWarnings("serial")
public class NoMouseListenerNeeded extends JPanel {
private static final Integer[][] DATA = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
private static final String[] COLS = { "A", "B", "C" };
private DefaultTableModel tblModel = new DefaultTableModel(DATA, COLS);
private JTable table = new JTable(tblModel);
public NoMouseListenerNeeded() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new MyButtonListener(this)));
setLayout(new BorderLayout());
add(new JScrollPane(table));
add(btnPanel, BorderLayout.PAGE_END);
}
// get data held by selected cell in JTable
// returns null if no cell selected
public Object getSelectedCell() {
int col = table.getSelectedColumn();
int row = table.getSelectedRow();
if (col < 0 || row < 0) {
return null; // no selection made, return null
} else {
return table.getValueAt(row, col);
}
}
private static void createAndShowGui() {
NoMouseListenerNeeded mainPanel = new NoMouseListenerNeeded();
JFrame frame = new JFrame("NoMouseListenerNeeded");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
You could use an ActionListener (or AbstractAction) to get the selected cell by passing the GUI into the listener, and calling the GUI's method that returns the selected data: 您可以使用ActionListener(或AbstractAction)通过将GUI传递到侦听器,并调用GUI的方法来返回选定的数据来获取选定的单元格:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
@SuppressWarnings("serial")
public class MyButtonListener extends AbstractAction {
private NoMouseListenerNeeded mainGui;
public MyButtonListener(NoMouseListenerNeeded mainGui) {
super("Press Me");
putValue(MNEMONIC_KEY, KeyEvent.VK_P);
this.mainGui = mainGui;
}
@Override
public void actionPerformed(ActionEvent e) {
Object cell = mainGui.getSelectedCell();
if (cell != null) {
String message = "Selection is: " + cell;
JOptionPane.showMessageDialog(mainGui, message, "Selection", JOptionPane.PLAIN_MESSAGE);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.