我在 JScrollPane
中有一个 JTable
,我试图隐藏 JScrollPane
的 ViewPortBorder
。为此,我将其边框设置为空边框,但看起来它在 JTable 外边框(第一列的左边框)上绘制了空边框。
如何在不隐藏 JTable
边框的情况下隐藏 JScrollPane
边框?问题仅出现在左侧,边界在右侧不重叠。
public static void main(String[] args) {
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
JTable table = new JTable(data, columnNames);
JScrollPane scroll = new JScrollPane(table);
Border border = BorderFactory.createEmptyBorder(0, 0, 0, 0);
scroll.setViewportBorder(border);
scroll.setBorder(border);
JPanel panel = new JPanel();
panel.add(scroll);
JFrame frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
最佳答案
JTable
没有边框,您看到的是滚动 Pane 提供的边框
(无边框/有边框)
这实际上是(原因之一)表格左侧没有网格线的原因;)
你可以做的是对表格应用一个MatteBorder
Color color = table.getGridColor();
table.setBorder(new MatteBorder(0, 1, 0, 0, color));
(无边框/有边框)
关于java - JScrollPane 边框重叠在 JTable 边框上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24317964/