JTable中的JTable无法呈现

时间:2019-03-06 16:42:14

标签: java swing jtable

我正在尝试将 JTable 放入 JTable 。一切正常,我可以获取所有 track 信息,并将其成功地放入 model insideTable 中,唯一的问题是我无法获取呈现。

甚至尝试将 insideTable 前景色设置为红色,但仍然看不到任何变化

  table.setDefaultRenderer(List.class, new DefaultTableCellRenderer(){
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            String[] columns = {"Name","Duration","Location"};
            List<Track> tracks = (ArrayList<Track>) value;

            DefaultTableModel model = new DefaultTableModel(columns,0);
            JTable insideTable = new JTable(model);

            for(Track t: tracks){
                String[] r = {t.getName(),t.getDuration(),t.getLocation()};
                model.addRow(r);
            }
            model.fireTableDataChanged();
            insideTable.setVisible(true);

            return insideTable;
        }
    });

这是它的外观。

Using custom render

这是使用默认渲染。

Using default render

编辑已解决

 table.setDefaultRenderer(List.class, new DefaultTableCellRenderer(){
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            String[] columns = {"Name","Duration","Location"};
            List<Track> tracks = (ArrayList<Track>) value;

            DefaultTableModel model = new DefaultTableModel(columns,0);
            JTable insideTable = new JTable(model);

            for(Track t: tracks){
                String[] r = {t.getName(),t.getDuration(),t.getLocation()};
                model.addRow(r);
            }
            table.setRowHeight(row, 20 + (15 * insideTable.getRowCount()));
            JScrollPane columnFix = new JScrollPane(insideTable);
            columnFix.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

            return columnFix;
        }
    });

Fixed

1 个答案:

答案 0 :(得分:0)

好的,我解决了该问题,为行提供了自定义高度,然后修复了未显示的columnNames。

table.setRowHeight(row, 20 + (15 * insideTable.getRowCount()));
JScrollPane columnFix = new JScrollPane(insideTable);
columnFix.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);