这里使用 mouseClicked 事件来获取字段上的数据,同时单击表格,我使用了如下代码
scrollPane.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int rowIndex= table.getSelectedRow();
DefaultTableModel model=(DefaultTableModel) table.getModel();
txt_Product_ID.setText(model.getValueAt(rowIndex,0).toString());
txt_Product_Code.setText(model.getValueAt(rowIndex,1).toString());
txt_Product_Name.setText(model.getValueAt(rowIndex,2).toString());
}
});
这里的问题是,当我单击行或列时,数据没有出现在相应的字段上,而是在单击行或列并单击表格上的剩余可用空间时出现。因此双击仅产生结果.请帮我解决我的问题
最佳答案
scrollPane.addMouseListener(new MouseAdapter() {
Here the problem is when i click on the row or column the data is not appearing on the corresponding fields
不要将 MouseListener 添加到滚动 Pane 。 MouseListener 应添加到表中,因为这是您正在单击的组件。
关于java - java中鼠标单击操作事件执行方式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45385510/