有谁知道为什么
的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());
}
}
答案 0 :(得分:1)
问题是,该行已被选中。
表格选择模型与行的选择变化有关。
如果您按照以下方式更改注册听众的顺序......
JTbl.getSelectionModel().addListSelectionListener(new cellSelectedListener(JTbl));
JTbl.setColumnSelectionInterval(0, 0);
JTbl.setRowSelectionInterval(0, 0);
您将看到行选择更改。
更新代码审核...
通常,这个JTbl.setPreferredScrollableViewportSize(JTbl.getPreferredSize());
不是一个好主意。你真的想把它留在滚动窗格来做出这些决定。如果您真的想要影响表的宽度,可以更改列大小。
Java命名约定表明所有实例变量都以小写字符开头,因此JTbl
将成为jTbl
,所有类都以大写字母开头,因此mainFrame
将成为{{ 1}} ......小事,但这是人们真正习惯的。