如何在JTable中创建自定义表格单元格?

时间:2014-03-21 14:16:26

标签: java swing jtable multiline tablecellrenderer

我想创建一个如下所示的自定义表:

+----------------------------------+
|  +-----+                         |
|  |     |   [Some JLabel]         |
|  |Icon |   [Some other JLabel]   |
|  +-----+                         |
+----------------------------------+

我目前有以下设置:

 //scroll view for the table
 JScrollPane scrollPane = new JScrollPane(eventsTable);
 scrollPane.setBorder(BorderFactory.createEmptyBorder());
 eventsTable.setFillsViewportHeight(true);
 scrollPane.setPreferredSize(new Dimension(tableWidth,500));

  //create the events table
  EventsTableView eventsTable = new EventsTableView(new EventsTableModel()){

   public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
                    Component c = super.prepareRenderer(renderer, row, column);

                    if (!isRowSelected(row)) {

                        c.setForeground(new Color(214,214,214,255));
                        c.setBackground(new Color(27,36,50,255));

                    }else{

                        c.setForeground(new Color(40,40,40,255));
                        c.setBackground(new Color(250,250,250,255)); 


                    }
                    return c;
                }
            };

        eventsTable.setBackground(new Color(19,24,30,255));
        eventsTable.setShowHorizontalLines(false);
        eventsTable.setShowVerticalLines(false);
        eventsTable.setTableHeader(null);
        eventsTable.setIntercellSpacing(new Dimension(0,0));
        int tableWidth = 250;

我还没有想到整个渲染器的东西,所以我不确定这是否是正确的方法。

有没有办法指定自定义,比如JPanel并为其添加标签并在自定义渲染器中返回?

0 个答案:

没有答案