【问题标题】:JTable to a Parent JPanel with GridBagConstraints带有 GridBagConstraints 的 JTable 到父 JPanel
【发布时间】:2018-01-21 21:00:42
【问题描述】:

因此,对于学校项目,我必须在单独的类中将 JTable 添加到 JPanel 而不修改后者。这是JPanel所在的类。

public class ThisSwingView extends SwingView implements ThisTCRMView {

public ThisSwingView() {
    super();
    setTitle("This");

    JScrollPane centerScrollPane = new JScrollPane();
    centerScrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
    centerScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    centerScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    setCenterPanel(centerScrollPane);


    JPanel centerGrid = new JPanel();
    centerScrollPane.setViewportView(centerGrid);
    GridBagLayout gbl_centerGrid = new GridBagLayout();
    gbl_centerGrid.columnWidths = new int[]{100, 475, 0};
    gbl_centerGrid.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    gbl_centerGrid.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    gbl_centerGrid.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    centerGrid.setLayout(gbl_centerGrid);}}

这是我可以修改的类:

public class NewThisSwingView extends ThisSwingView implements ThisTCRMSales{

public NewThisSwingView(){
    super();
    JPanel centerGrid = new JPanel();
    JTable table;
    Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
            { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
    Object columnNames[] = { "Column One", "Column Two", "Column Three" };
    table = new JTable(rowData, columnNames);
    GridBagConstraints gbc_Table = new GridBagConstraints();
    gbc_contactsTable.insets = new Insets(0, 0, 5, 0);
    gbc_contactsTable.fill = GridBagConstraints.HORIZONTAL;
    gbc_contactsTable.gridx = 1;
    gbc_contactsTable.gridy = 12;
    centerGrid.add(table, gbc_Table);
}}

但是,当我这样做时,我的表格不会打印在屏幕上。我已经修改了主类来运行我正在修改的类,但它不起作用。 提前致谢。

【问题讨论】:

  • centerGrid 从未添加到 NewThisSwingView ... 但我看不到您正在使用 NewThisSwingView 的任何地方,因此,它也永远不会添加到任何东西中。这引发了一系列新问题——比如为什么你不将JTable 包装在它自己的JScrollPane 中?
  • 如需尽快获得更好的帮助,请发帖minimal reproducible exampleShort, Self Contained, Correct Example

标签: java swing jtable layout-manager gridbaglayout


【解决方案1】:

如下更改代码,在 JScrollPane 中添加表格

public class NewThisSwingView extends ThisSwingView implements ThisTCRMSales{

    public NewThisSwingView(){
        super();
        JPanel centerGrid = new JPanel();
        JTable table;
        Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
                { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
        Object columnNames[] = { "Column One", "Column Two", "Column Three" };
        table = new JTable(rowData, columnNames);
        GridBagConstraints gbc_Table = new GridBagConstraints();
        gbc_contactsTable.insets = new Insets(0, 0, 5, 0);
        gbc_contactsTable.fill = GridBagConstraints.HORIZONTAL;
        gbc_contactsTable.gridx = 1;
        gbc_contactsTable.gridy = 12;

        // wrap table inside JScrollPane
        centerGrid.add(new JScrollPane(table), gbc_Table);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-09
    • 2014-04-15
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多