简体   繁体   English

具有不同颜色值的JTable

[英]JTable with different color value

I want to display data in a JTable like in the picture bellow: 我想在JTable显示数据,如下图所示:

在此处输入图片说明

It can be observed that in the second row appear only 3 values, which I want to be colored. 可以观察到,在第二行中仅出现3个值,我希望它们是彩色的。 Also I have a problem in dispaying those rows, with the indentation. 另外,在缩进这些行方面我也有问题。 My disply so far looks like this: 到目前为止,我的显示如下: 在此处输入图片说明

I would like to fix the propblem with the indentation and remove th , . 我想用缩进修复propblem和删除次,

My code: 我的代码:

leftList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent event) {
                ArrayList<String> codes = new ArrayList<String>();
                ArrayList<String> values = new ArrayList<String>();

                if(!event.getValueIsAdjusting()) {
                    int proteinIndex = leftList.getSelectedIndex();

                            Object rowData[][] = { {codes}, {values} };
                            Object columnNames[] = {codes};
                            JTable table = new JTable(rowData, columnNames);
                            table.setTableHeader(null);
                            table.setShowGrid(false);
                            tablePane = new JScrollPane(table);
                            tablePane.setPreferredSize(new Dimension(765,40));
                            rightPanel.removeAll();
                            rightPanel.updateUI();
                            rightPanel.add(tablePane);
                }
            }
        });
    }

    public void showGUI() {
        JFrame frame = new JFrame();
        frame.add(leftPanel,BorderLayout.EAST);
        frame.add(listScrollPane,BorderLayout.WEST);
        frame.add(rightPanel);
        frame.setTitle("GUI");
        frame.setSize(1000,500);
        frame.setLocation(200,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

Can you please help me achieve a result like in the first picture? 您能帮我实现第一张照片中的效果吗?

It can be observed that in the second row appear only 3 values, which I want to be colored. 可以观察到,在第二行中仅出现3个值,我希望它们是彩色的。

You will have to create a custom TableCellRenderer that colors each cell corresponding to the item in that cell. 您将必须创建一个自定义TableCellRenderer ,为该单元格中与该项目相对应的每个单元格着色 But... 但...

I would like to fix the propblem with the indentation and remove th ,. 我想解决与缩进的问题,并删除th ,。

The items within the List are not placed individually in a table cell. List中的项目没有单独放置在表格单元格中。 Rather, the call... 而是电话...

Object rowData[][] = { {codes}, {values} };

...uses the toString method of ArrayList , resulting in a table with 2 rows and 1 column. ...使用ArrayListtoString方法,生成一个具有2行1列的表。 An alternative approach to get each item of the List into it's own table cell is to convert the List to arrays List每个项目放入其自己的表格单元格的另一种方法是将List转换为数组

Object rowData[][] = { codes.toArray(), values.toArray() };
Object columnNames[] = codes.toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM