有谁知道为什么
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());不是一个好主意。您确实希望将其留给滚动窗格来做出此类决定。如果您确实想影响表格的宽度,则可以更改列的大小。

Java命名约定建议所有实例变量均以小写字母开头,因此JTbl将变为jTbl,而所有类均以大写字母开头,因此mainFrame将变为MainFrame ...小东西,但这是人们真正习惯了什么。

10-08 09:20