如何使用 JTable / JScrollPane 消除边框
如果运行下面的小样本,您将看到中心区域周围有一个边框。我不确定为什么会显示此边框。
当 JTable 位于 JScrollPane 中时会发生这种情况。我尝试了各种方法来删除它,但到目前为止还没有成功。没有 JScrollPane 的 JTable 不显示边框。
请参阅下面的示例。 TIA。
public class TestScrollPane extends JFrame {
public static void main(String[] args) {
JFrame frame = new TestScrollPane();
JPanel panel = new JPanel();
JTable table = new JTable();
panel.setLayout(new BorderLayout());
panel.add(new JLabel("NORTH"), BorderLayout.NORTH);
panel.add(new JLabel("SOUTH"), BorderLayout.SOUTH);
JScrollPane sp = new JScrollPane(table);
// None of these have any effect
sp.setBorder(null);
sp.getInsets().set(0, 0, 0, 0);
sp.setViewportBorder(null);
sp.getViewport().setBorder(null);
sp.getViewport().getInsets().set(0, 0, 0, 0);
sp.getViewport().setOpaque(true);
panel.add(sp, BorderLayout.CENTER);
// Adding the table alone shows no border
// panel.add(table, BorderLayout.CENTER);
frame.add(panel);
frame.setVisible(true);
}
public TestScrollPane() throws HeadlessException {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(100, 100));
}
}
If you run the small sample below you'll see a border around the center region. I'm not sure why this border is showing.
It happens when a JTable is in a JScrollPane. I tried various things to remove it but so far no luck. A JTable without the JScrollPane shows no border.
See sample below. TIA.
public class TestScrollPane extends JFrame {
public static void main(String[] args) {
JFrame frame = new TestScrollPane();
JPanel panel = new JPanel();
JTable table = new JTable();
panel.setLayout(new BorderLayout());
panel.add(new JLabel("NORTH"), BorderLayout.NORTH);
panel.add(new JLabel("SOUTH"), BorderLayout.SOUTH);
JScrollPane sp = new JScrollPane(table);
// None of these have any effect
sp.setBorder(null);
sp.getInsets().set(0, 0, 0, 0);
sp.setViewportBorder(null);
sp.getViewport().setBorder(null);
sp.getViewport().getInsets().set(0, 0, 0, 0);
sp.getViewport().setOpaque(true);
panel.add(sp, BorderLayout.CENTER);
// Adding the table alone shows no border
// panel.add(table, BorderLayout.CENTER);
frame.add(panel);
frame.setVisible(true);
}
public TestScrollPane() throws HeadlessException {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(100, 100));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 BorderFactory.createEmptyBorder() 而不是 null...
通过使用:
它有效。
你的主要方法变成:
Use BorderFactory.createEmptyBorder() instead of null...
by using:
it works.
Your main method becomes:
有趣的是,当您删除此行时,边框就会消失:
Interestingly the border disappears when you remove this line:
我正在寻找同一问题的答案,但上述答案无法做到......所以我找到了更好的答案:
I was looking for the answer for the same question but above answers could not do... so I found a better answer:
对于 JTable
table.setIntercellSpacing(new Dimension(0, 0))
有效。For JTable
table.setIntercellSpacing(new Dimension(0, 0))
works.我认为正确的解决方法是将 viewportView 上的边框设置为“null”。
I think the proper fix is to set the border on the viewportView to 'null'.
要从 JScrollPane 的所有部分(包括垂直和水平栏)删除边框,可以使用以下代码
To remove the border from all parts of the JScrollPane including the vertical and horizontal bar the following code works