gpt4 book ai didi

java - ListSelection 监听器未触发事件

转载 作者:行者123 更新时间:2023-12-01 14:38:26 25 4
gpt4 key购买 nike

有谁知道为什么
System.out.println(e.getFirstIndex());System.out.println(e.getLastIndex()); 两者什么都不做?我的监听器没有正确注册吗?我想知道何时选择某个单元格并对其执行某些操作。就像通过鼠标或按键选择该单元格时设置默认文本一样。

package VLGui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.table.*;

import VLCore.cellSelectedListener;


public class mainFrame extends JFrame{

private JLabel lblTime;
private Vector columnName = new Vector();
private Vector tblData = new Vector();
private JTable JTbl;
private Timer timer = new Timer(1000,new MyListener());

private JPanel topPnl,cenPnl,btmPnl;
private Calendar time;

private Object[] columnNames = {"Veh No.","Description","Time In","Time Out"};

public mainFrame()
{
timer.start();
//jtable settings
JTbl = new JTable(new DefaultTableModel(columnNames,1));
JTbl.setPreferredScrollableViewportSize(JTbl.getPreferredSize());
JTbl.setFillsViewportHeight(true);
JTbl.setCellSelectionEnabled(false);
JTbl.setColumnSelectionInterval(0,0);
JTbl.setRowSelectionInterval(0,0);
JTbl.getSelectionModel().addListSelectionListener(new cellSelectedListener(JTbl));
//Settings
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Vehicle Log");
setSize(500,1000);
setVisible(true);

//Dec
JScrollPane tableContainer = new JScrollPane(JTbl);
topPnl = new JPanel();
cenPnl = new JPanel();
btmPnl = new JPanel();
lblTime = new JLabel();


//Adding Components
topPnl.add(lblTime,BorderLayout.CENTER);
cenPnl.add(tableContainer);


getContentPane().add(topPnl,BorderLayout.NORTH);
getContentPane().add(cenPnl,BorderLayout.CENTER);
//add(btmPnl);

}

public class MyListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
time = Calendar.getInstance();
lblTime.setText(time.getTime().toString());
lblTime.repaint();
}

}
}




package VLCore;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class cellSelectedListener implements ListSelectionListener {
private JTable jTbl;

public cellSelectedListener()
{

}

public cellSelectedListener(JTable tbl)
{
jTbl = tbl;
}

@Override
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
System.out.println(e.getFirstIndex());
System.out.println(e.getLastIndex());
}

}

最佳答案

问题是,该行已被选中。

表格选择模型与行的选择变化相关。

如果您按如下方式更改注册监听器的顺序...

JTbl.getSelectionModel().addListSelectionListener(new cellSelectedListener(JTbl));
JTbl.setColumnSelectionInterval(0, 0);
JTbl.setRowSelectionInterval(0, 0);

您将看到行选择发生变化。

更新代码审查...

一般来说,这个JTbl.setPreferredScrollableViewportSize(JTbl.getPreferredSize());不是一个好主意。您确实希望将其留给滚动 Pane 来做出此类决定。如果您确实想影响表格的宽度,则可以更改列大小。

Java 命名约定建议所有实例变量都以小写字符开头,因此 JTbl 将变为 jTbl 并且所有类都以大写字符开头,因此 mainFrame 将成为 MainFrame...小事,但这是人们真正习惯的。

关于java - ListSelection 监听器未触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16249348/

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