'table'のmouseListenerで 'urObjectInCell'オブジェクトを別のクラスBtnDelete1に使用する必要があります。Java:あるmouseListenerから別のクラスへのオブジェクトの使用方法
は、ここに私のマウスリスナーのコードです:
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();
私は前に宣言されたfriendNo文字列にオブジェクトを格納しようとしました。しかし、私は友人がオブジェクトの価値を取っているとは思わない。
ここで私のクラス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());
}
}
}
のMouseListenerから得urObjectInCellオブジェクトの値は、JTableの「テーブル」の行を削除するのに使用されます。
あなたは 'FirstSwingApp'、あなたは' mouseEvent'をやっているのと同じ方法で、 'BtnDelete'アクションイベントを持っていない理由は? –
私はすべての異なるボタンのアクションを実行するために別々のクラスを使用しています。 'firstSwingsApp()'はGUIがインスタンス化されるコンストラクタです。私はテーブル上でマウスがクリックされたときにアクションを実行するための別のクラスを作っていましたが、 'mouseClick'は別の' actionListener'を許可しません。 –