gpt4 book ai didi

java - 在 Java 中双击 JTable 上的监听器

转载 作者:IT老高 更新时间:2023-10-28 21:09:51 25 4
gpt4 key购买 nike

我很好奇只有​​在 JTable 中的一行被双击时如何调用 valueChanged 覆盖的方法。现在,下面的代码片段实现了单击操作或事件箭头键来浏览人员列表,并相应地调整 JLabel。我正在尝试做的事情类似于我单击一次所做的事情,但是这次如果并且仅当双击一行时 dto 会改变,否则什么也不会发生。我该怎么做:(

   class ListDataUI {

public void addListSelectionListener(ListSelectionListener listSelectionListener) {
summaryTable.getSelectionModel().addListSelectionListener(listSelectionListener);

public T getSelectedDTO() {
final int selectedRowIndex = summaryTable.getSelectedRow();
if (selectedRowIndex != -1) {
return data.get(summaryTable.convertRowIndexToModel(selectedRowIndex));
} else {
return null;
}
}
}
}




class MainMenu extends javax.swing.JFrame {
private void initListeners() {
searchTable.addListSelectionListener(new ListSelectionListener() {

@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
AcademicDTO dto = (AcademicDTO) searchTable.getSelectedDTO();
acImgLabel.setIcon(new ImageIcon());
label_name.setText(dto.getTitle() + " " + dto.getForename() + " " + dto.getSurname());
label_role.setText("Role: " + dto.getRole());
label_phone.setText("Phone: " + dto.getPhone());
label_room.setText("Room: " + dto.getRoom());
label_hours.setText("Hours: " + dto.getHours());
label_mobile.setText("Mobile: " + dto.getMobile());
if (dto.getImage() != null) {
acImgLabel.setIcon(new ImageIcon(dto.getImage()));
}
}
}
});
}

}


private void initListeners() {
contactTable.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
ContactDTO dto = (ContactDTO) contactTable.getSelectedDTO();
if (e.getClickCount() == 2) {
System.out.println(dto.getForename());
} else {
}

}
});
}

不确定上面的其余部分......

最佳答案

试试这个:

mytable.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
JTable table =(JTable) mouseEvent.getSource();
Point point = mouseEvent.getPoint();
int row = table.rowAtPoint(point);
if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) {
// your valueChanged overridden method
}
}
});

关于java - 在 Java 中双击 JTable 上的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14852719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com