JTable选择监听器

7

我有一段代码,用于在小程序中显示表格,包含两列:

  1. 图片图标
  2. 描述

以下是我的代码:

    import javax.swing.table.*;

    public class TableIcon extends JFrame
     {
    public TableIcon()
    {
    ImageIcon aboutIcon = new ImageIcon("about16.gif");
    ImageIcon addIcon = new ImageIcon("add16.gif");
    ImageIcon copyIcon = new ImageIcon("copy16.gif");

    String[] columnNames = {"Picture", "Description"};
    Object[][] data =
    {
        {aboutIcon, "About"},
        {addIcon, "Add"},
        {copyIcon, "Copy"},
    };

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable( model )
    {
        //  Returning the Class of each column will allow different
        //  renderers to be used based on Class
        public Class getColumnClass(int column)
        {
            return getValueAt(0, column).getClass();
        }
    };
    table.setPreferredScrollableViewportSize(table.getPreferredSize());

    JScrollPane scrollPane = new JScrollPane( table );
    getContentPane().add( scrollPane );
}

public static void main(String[] args)
{
    TableIcon frame = new TableIcon();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setVisible(true);
  }

} 

现在我想知道如何在我的表格上实现选择监听器或鼠标监听器事件,使其选择表格中的特定图像并显示在文本区域或文本字段中(我的表格包含图像文件的路径)?
我可以在表格上添加文本字段和表格到框架上吗?如有需要,请随时提出问题。
3个回答

8

在我的代码中,我有一个表格,我设置了单选模式;在我的情况下,使用如何编写列表选择监听器中描述的监听器(使用从getMinSelectionIndex到getMaxSelectionIndex的for循环)是没有用的,因为释放鼠标按钮时我确定只选择了一行。

所以我解决了如下问题:

....

int iSelectedIndex =-1;

....

JTable jtable = new JTable(tableModel); // tableModel defined elsewhere
jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

ListSelectionModel selectionModel = jtable.getSelectionModel();

selectionModel.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
        handleSelectionEvent(e);
    }
});

....

protected void handleSelectionEvent(ListSelectionEvent e) {
    if (e.getValueIsAdjusting())
        return;

    // e.getSource() returns an object like this
    // javax.swing.DefaultListSelectionModel 1052752867 ={11}
    // where 11 is the index of selected element when mouse button is released

    String strSource= e.getSource().toString();
    int start = strSource.indexOf("{")+1,
        stop  = strSource.length()-1;
    iSelectedIndex = Integer.parseInt(strSource.substring(start, stop));
}

我认为这个解决方案更适用于表格处于单选模式下,因为它不需要在开始和结束之间使用for循环来检查选择了哪个元素。


11
请不要那样做!如果表格处于单选模式,e.getFirstIndex()e.getLastIndex() 将会给出所选择的行。 然后调用任何 ListSelectionModel 的方法,例如 ((ListSelectionModel)e.getSource()).isSelectedIndex(e.getFirstIndex())。为了使代码易于理解... - Matthieu

5
这个怎么样?
        protected void handleSelectionEvent(ListSelectionEvent e) {
            if (e.getValueIsAdjusting())
                return;

            final DefaultListSelectionModel target = (DefaultListSelectionModel)e.getSource();
            iSelectedIndex = target.getAnchorSelectionIndex();
        }

3

阅读Swing教程中关于如何编写列表选择监听器的部分。

你无法将文本框添加到表格中,但是你可以在同一窗口中添加一个文本框和一个表格。


1
他可以触发表格中给定单元格的编辑,以使文本字段出现。我对要求不太清楚。 - Guillaume Polet
@GuillaumePolet 我更关心的是将动作监听器添加到我的表格中,就这样。 - puneetverma0711
表格没有ActionListener。 - camickr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接