java - 在java中创建一个自定义表

标签 java swing jtable gridbaglayout

我想创建一个如下所示的表格,但标题与表格同步:

enter image description here

有关如何使列与行对齐并允许在 x 轴上自动调整大小的任何提示?为此,我使用了 GridBagLayout。还尝试使用 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();
    }
}

最佳答案

如果您同意所有单元格大小相同,GridLayout 将是一个简单的解决方案。我在哪些可用的新布局方面落后于时代,这可能会有所帮助,但 GridBag 可能有一些学习曲线。如果您希望所有内容都对齐,则需要一个 GridBagLayout,而不是每一行一个。

关于java - 在java中创建一个自定义表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18402114/

相关文章:

java - 如何从 Java 运行 shell 脚本并让它在 JVM 关闭后继续运行?

java - 如何在tomcat上的 Jersey 服务器中启用CORS

java - 如何在 IntelliJ 中使用拖放进行 java swing

Java - 列表中的所有元素变得相同

java - 动态模块 : How to add auto increment numbers to key field while adding record to table

java - 我可以在输入时清除 JTable 中的文本吗?

java - 如何为 JTable 中的单元格着色?

java - my 给出一个空结果集。我的 JTable 是空的。不应该是这样

java - 将图像添加到每个 JTable 单元格

java - 从 JPanel 中动态删除组件