0

有谁知道为什么
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());
    }

}
4

1 回答 1

1

问题是,该行已经被选中。

表选择模型涉及行的选择更改。

如果您更改注册侦听器的顺序如下...

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

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

更新代码审查...

一般来说,这JTbl.setPreferredScrollableViewportSize(JTbl.getPreferredSize());不是一个好主意。你真的想把它留给滚动窗格来做出这些决定。如果你真的想影响表格的宽度,你可以改变列的大小。

Java 命名约定建议所有实例变量都以小写字符开头,so JTblwould become jTbl,所有类都以大写字母开头,so mainFramewill become MainFrame...small thing,但这是人们真正习惯的。

于 2013-04-27T07:18:32.210 回答