gpt4 book ai didi

java - 调用 JTable 的 listselectionlistener

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:21 24 4
gpt4 key购买 nike

成功添加 ListSelectionListener 并让它注册用户操作后,我现在需要在从不同的类初始化包含它的 JFrame 期间在 JTable View 中预选一行:

// in constructor
rowSM_treatments = table_histories.getSelectionModel();
rowSM_treatments.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No rows are selected.");
} else {
selectedRow_treatments = lsm.getMinSelectionIndex();
System.out.println("selected Row> " + selectedRow_treatments);
//do more
}}});
// later on is this method which I mean to call from outside this class
public void setSelectedRow(int row){
rowSM_treatments.setLeadSelectionIndex(row);
}

最佳答案

我通常只是访问表格,而不是直接使用选择模型:

table.changeSelection(row, 0, false, false);

因此,只要您的其他类引用了该表,您就可以执行此操作。

关于java - 调用 JTable 的 listselectionlistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050402/

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