简体   繁体   English

在Java中创建自定义表

[英]Create a custom table in java

I would like to create a table looking like this but with the titles in-sync with the table: 我想创建一个像这样的表,但标题与该表同步:

在此处输入图片说明

Any tips on how make the columns being aligned with the rows and also allow auto resize in x-axis? 关于如何使列与行对齐以及如何在X轴上自动调整大小的任何技巧? For this I used GridBagLayout . 为此,我使用了GridBagLayout Also tried with JTable but had problems to customize it to hide all default JTable components(like in my picture). 也尝试过使用JTable但在自定义它以隐藏所有默认JTable组件时遇到问题(如我的图片所示)。

public class MyTableExample extends JFrame {

    public MyTableExample(){
        JPanel contentPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        contentPanel.add(getTitles(),c);
        c.gridy = 2;
        contentPanel.add(getRow(),c);
        c.gridy = 3;
        contentPanel.add(getRow(),c);

        // MainPanel: Helps positioning the contentPanel
        JPanel mainPanel = new JPanel(new GridBagLayout());
        c.weighty = 1;
        c.anchor = GridBagConstraints.NORTH;
        c.insets = new Insets(20,20,20,20);
        mainPanel.add(contentPanel,c);

        this.add(mainPanel);
        this.pack();
        this.setVisible(true);
    }

    private JPanel getRow(){
        JPanel rowPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.WEST;

        rowPanel.setOpaque(false);
        JLabel col1 = new JLabel("#rowItem");
        JComboBox<String> col2 = new JComboBox<String>();
        JButton col3 = new JButton("rowItem");
        JLabel col4 = new JLabel("rowItem");
        rowPanel.add(col1,c);
        rowPanel.add(col2,c);
        rowPanel.add(col3,c);
        rowPanel.add(col4,c);

        return rowPanel;
    }

    private JPanel getTitles(){
        JPanel titlePanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.WEST;

        titlePanel.setOpaque(false);
        JLabel t1 = new JLabel("T1");
        JLabel t2 = new JLabel("Title2");
        JLabel t3 = new JLabel("Title33333333");
        JLabel t4 = new JLabel("T4");
        titlePanel.add(t1,c);
        titlePanel.add(t2,c);
        titlePanel.add(t3,c);
        titlePanel.add(t4,c);

        return titlePanel;
    }

    public static void main(String[] args){
        new MyTableExample();
    }
}

If you're okay with all the cells being the same size, GridLayout would be an easy solution. 如果您对所有单元格的大小GridLayout ,则GridLayout将是一个简单的解决方案。 I'm behind the times on what new layouts are available which might be helpful, but GridBag can have a bit of a learning curve to it. 在什么可用的新布局方面我落后了,这可能会有所帮助,但是GridBag可能会有一些学习上的弯路。 If you want everything to line up, you need to have one GridBagLayout, not one per row. 如果希望所有内容都对齐,则需要一个GridBagLayout,而不是每行一个。

Use a single panel and place all your components directly in this panel, using a GridBagLayout. 使用一个面板,然后使用GridBagLayout将所有组件直接放置在此面板中。 That way, all the components will be placed in a single grid, and will thus be aligned correctly. 这样,所有组件都将放置在单个网格中,因此将正确对齐。

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

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