【问题标题】:Delete/Hide margins of a Jtable删除/隐藏 Jtable 的边距
【发布时间】:2015-08-27 03:39:59
【问题描述】:

我使用 JTable 仅用于对齐我想要显示的值。 到目前为止,我的桌子看起来像这样:

我想要的是删除表格的边距,因此无法注意到它是表格。有没有办法让这成为可能?

我的代码:

displayNames = new ArrayList<String>();
displayClasses = new ArrayList<String>();
for (int i=0; i<displayName.size(); i++) {
    displayNames.add(displayName.get(i));
    displayClasses.add(classes.get(i));
    }
    Object rowData[][] = { displayNames.toArray(), displayClasses.toArray() };
    Object columnNames[] = displayNames.toArray();
    JTable table = new JTable(rowData, columnNames);
    table.setTableHeader(null);
    table.setShowGrid(false);
    table.setBackground(Color.LIGHT_GRAY);
    tablePane = new JScrollPane(table);
    tablePane.setPreferredSize(new Dimension(765,40));
    tablePane.setBackground(Color.LIGHT_GRAY);
    rightPanel.removeAll();
    rightPanel.updateUI();
    rightPanel.add(tablePane);

public void showGUI() {
        JFrame frame = new JFrame();
        frame.add(leftPanel,BorderLayout.EAST);
        frame.add(listScrollPane,BorderLayout.WEST);
        frame.add(rightPanel);
        frame.setSize(1000,500);
        frame.setLocation(200,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

【问题讨论】:

  • “我使用 JTable 仅用于对齐值” 请改用 GridBagLayout
  • 我会直接将 JTable 放到容器中,不使用 JSrollPane,必须从 getPreferredScrollableViewportSize() 覆盖 getPreferedSize

标签: java swing


【解决方案1】:

听起来你想在 JScrollPane 上使用 setBorder

tablePane.setBorder(BorderFactory.createEmptyBorder());

详细回答here

【讨论】:

    【解决方案2】:
    tablepane.setBorder(BorderFactory.createEmptyBorder());
    

    【讨论】:

      【解决方案3】:

      显示边距(和边框)是因为您将表格放置在 JScrollPane 中。

      如果你使用rightPanel.add(**table**),边距就没有了。

      如果还需要边框,可以使用table.setBorder(...).手动为表格添加边框

      如果不使用 JScrollPane,您显然也会失去滚动能力...我不确定这是否对您来说是个问题。

      我也同意 Andrew:您不应该为此使用表格,而是使用 GridBagLayout。

      【讨论】:

        猜你喜欢
        • 2014-09-24
        • 2012-10-27
        • 2023-02-24
        • 2016-12-19
        • 2017-03-11
        • 2012-11-17
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多