java - JTable 选择监听器

标签 java swing jtable jtextarea

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

  • 图像图标
  • 说明

  • 这是我的代码:
        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);
      }
    
    } 
    

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

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

    最佳答案

    在我的代码中,我有一个表格,我在其中设置了单选模式;就我而言,听众在 How to Write a List Selection Listener 中描述(使用从 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));
    }
    

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

    关于java - JTable 选择监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15906001/

    相关文章:

    java - 未检测到按下两个箭头 - java swing

    java - 在 Android 中的 viewpager 和 tablayout 中查看 fragment 中的可见性检查

    java - INSERT 行不会更新 GUI 中的 JTable

    java - 如何使用jtable第一列中的复选框将数据库中的数据显示到jtable?

    java - JTable 图像渲染在应用程序中承受过多负载

    java - OutputStream 未转换为字符串变量

    java.io.FileNotFoundException : open failed: EISDIR (Is a directory) 错误

    java - 在 ActionListener 中持续设置 JTextArea 文本

    java - JScrollPane 内的 JDesktopPane 调整大小问题

    java - 为 Java 应用程序创建启动屏幕的最佳方法?