how to change Jtable image from one column to another column using mouse click event?

白昼怎懂夜的黑 提交于 2019-12-02 15:42:46

问题


I am trying to use the following java class to add an image to a Jtable. It has worked properly. My problem is when I try to change the third column picture to second column (swap) using a mouse click event. But it will not work. I change the place of testIcon2, testIcon1 in the mouse click event.

First I load the image like Object[][] data = {{testIcon, "book1"}, {testIcon1, "book2"}, {testIcon2, "book3"}, {testIcon3, "book4"}};

In mouse click: Object[][] data1 = {{testIcon, "book1"}, {testIcon2, "book2"}, {testIcon1, "book3"}, {testIcon3, "book4"}};

How to change the second column image to first column when click the row?

public class TableIcon1 extends JFrame {
    private JTable table;
    private int pHeight = 60;

    public TableIcon1() {
        URL url = getClass().getResource("image/Pointer.GIF");
        final ImageIcon testIcon = new ImageIcon(url);
        URL url1 = getClass().getResource("image/1.jpg");
        final ImageIcon testIcon1 = new ImageIcon(url1);
        URL url2 = getClass().getResource("image/2.jpg");
        final ImageIcon testIcon2 = new ImageIcon(url2);
        URL url3 = getClass().getResource("image/3.jpg");
        final ImageIcon testIcon3 = new ImageIcon(url3);
        String[] columnNames = {"Picture", "Description"};
        Object[][] data = {{testIcon  , "book1"}, {testIcon1, "book2"}, {testIcon2, "book3"},{testIcon3, "book4"}};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);

        table = new JTable(model) {
            public Class getColumnClass(int column) {
                return getValueAt(1, column).getClass();
            }
        };

        table.setRowHeight(pHeight);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        table.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent event) {
                String[] columnNames1 = {"Picture", "Description"};
                Object[][] data1 = {{testIcon  , "book1"}, {testIcon2, "book2"}, {testIcon1, "book3"},{testIcon3, "book4"}};
                DefaultTableModel model1 = new DefaultTableModel(data1, columnNames1);
                table = new JTable(model1) {
                    public Class getColumnClass(int column) {
                        return getValueAt(1, column).getClass();
                    }
                };
            }
        });
    }

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

来源:https://stackoverflow.com/questions/9239270/how-to-change-jtable-image-from-one-column-to-another-column-using-mouse-click-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!