简体   繁体   English

双排 header 在 JTable

[英]double row header in JTable

im coding soft that uses table and jtable gives me hard time again.使用表格和 jtable 的即时编码软件让我再次感到困难。

for single row header im using:对于单行 header 我使用:

    private class make_table extends AbstractTableModel {
        private String[] headers = headers();
                                            
        Object[][] rows = rows();
        
        public int getColumnCount() {
            return headers.length;
        }

        public int getRowCount() {
            return rows.length;
        }

        public String getColumnName(int col) {
            return headers[col];
        }

        public Object getValueAt(int row, int col) {
            return rows[row][col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
    }   

then i just pass it into:然后我把它传递给:

table = new JTable(new make_table());

headers();标头(); method just returns String[] with names of columns - 1 dimensional array.方法只返回带有列名的 String[] - 一维数组。

i've tried to make headers() retrun 2D array - String[][] and headers within make_table String[][] instead of String[].我试图让 headers() 重新运行 2D 数组 - String[][] 和 make_table String[][] 中的 headers 而不是 String[]。 my problem is how to render it/show it in JTable.我的问题是如何在 JTable 中渲染/显示它。

i've just copy-pasted that whole method long time ago into another project and i cant understand how it works.我很久以前刚刚将整个方法复制粘贴到另一个项目中,我无法理解它是如何工作的。 i guess i could somehow use way im handling rows since its 2D array but i cant get my head around it.我想我可以以某种方式使用我处理行的方式,因为它是二维数组,但我无法理解它。

note: i've read some stuff but it all seems to be focusing on not same number of cells in row 1 and row 2. i want first row header to have 6 cells and second row in header 6 cells as well.注意:我已经阅读了一些内容,但似乎都集中在第 1 行和第 2 行中不同数量的单元格上。我希望第一行 header 有 6 个单元格,第二行 header 也有 6 个单元格。

my question: how could i pass into & show two row header JTable?我的问题:我怎样才能进入并显示两行 header JTable? preferably via 2D array.最好通过二维阵列。

Two different approaches:两种不同的方法:

  1. Use HTML for the column name with the <br> tag to denote each line使用 HTML 作为带有<br>标签的列名来表示每一行
  2. Use a custom renderer to parse the column name into multiple lines使用自定义渲染器将列名解析为多行

In both cases the height of the column header will be determined the the number of lines in the first column:在这两种情况下,列 header 的高度将取决于第一列中的行数:

在此处输入图像描述

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableHeaderLines extends JPanel
{
    public TableHeaderLines()
    {
        add( createHeaderUsingHTML() );
        add( createHeaderUsingCustomRenderer() );
    }

    private JComponent createHeaderUsingHTML()
    {
        // the height of the header is determined by the height of the first column

        Object[] columnNames =
        {
            "<html>Column 1<br>multi<br>line</html",
            "Column 2",
            "<html>Column 3<br>more</html",
            "<html>one<br>two<br>three<br>four</html>" // lines won't display completely
        };

        DefaultTableModel model = new DefaultTableModel(columnNames, 5);
        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        return new JScrollPane( table );
    }

    private JComponent createHeaderUsingCustomRenderer()
    {
        // the height of the header is determined by the height of the first column
        // the multi line renderer will only work if the first line is multi line

        Object[] columnNames =
        {
            "Column 1,multi,line",
            "Column 2",
            "Column 3,more",
            "one,two,three,four" // 4th line won't display
        };

        DefaultTableModel model = new DefaultTableModel(columnNames, 5);
        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        table.getTableHeader().setDefaultRenderer( new LineRenderer() );

        return new JScrollPane( table );
    }

    class LineRenderer extends JPanel implements TableCellRenderer
    {
        public LineRenderer()
        {
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            setOpaque( false );
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, final int row, final int column)
        {
            removeAll();

            String[] result = ((String)value).split(",");

            for (int i = 0; i < result.length; i++)
            {
                JLabel label = new JLabel(result[i]);
                label.setFont( table.getTableHeader().getFont() );
                label.setAlignmentX(0.5f);
                add(label);
            }

            return this;
        }
    }

    public static void createAndShowGUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add(new TableHeaderLines());
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

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

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