JTable 选择侦听器

pun*_*711 5 java swing jtable jtextarea

我有一个代码,它在小程序中显示表格 & 由两列组成:-

  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);
  }

} 
Run Code Online (Sandbox Code Playgroud)

现在我想知道的是如何在我的表格上实现选择侦听器或鼠标侦听器事件,以便它应该从我的表格中选择一个特定的图像并显示在文本区域或文本字段上(我的表格包含图像文件的路径)?

我可以在表格和框架上的表格上添加文本字段吗?如有需要,请随时提出查询。

Mac*_*ife 6

在我的代码中,我有一个表格,我在其中设置了单选模式;就我而言,如何编写列表选择侦听器(带有从 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));
}
Run Code Online (Sandbox Code Playgroud)

我认为这个解决方案不需要在 start 和 stop 之间使用 for 循环来检查选择了哪个元素,当表处于单选模式时更合适

  • 请不要那样做!如果表格处于单选模式,`e.getFirstIndex()` 或 `e.getLastIndex()` 会给你选择的行。然后调用`ListSelectionModel`的任何方法,例如`((ListSelectionModel)e.getSource()).isSelectedIndex(e.getFirstIndex())`。为了代码的可理解性... (11认同)

박찬신*_*박찬신 5

这个怎么样?

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

            final DefaultListSelectionModel target = (DefaultListSelectionModel)e.getSource();
            iSelectedIndex = target.getAnchorSelectionIndex();
        }
Run Code Online (Sandbox Code Playgroud)


cam*_*ckr 3

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

您无法将文本字段添加到表格中,但可以将文本字段和表格添加到同一框架中。