java - JScrollPane 边框重叠在 JTable 边框上

标签 java swing jtable border jscrollpane

我在 JScrollPane 中有一个 JTable,我试图隐藏 JScrollPaneViewPortBorder。为此,我将其边框设置为空边框,但看起来它在 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 提供的边框

(无边框/有边框)

enter image description here

这实际上是(原因之一)表格左侧没有网格线的原因;)

你可以做的是对表格应用一个MatteBorder

Color color = table.getGridColor();
table.setBorder(new MatteBorder(0, 1, 0, 0, color));

(无边框/有边框)

enter image description here

关于java - JScrollPane 边框重叠在 JTable 边框上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24317964/

相关文章:

java - 如何正确关闭 MappedByteBuffer?

java - Sun 的演示在 EDT 之外做 GUI 工作?

Java让GUI等待计时器

java - 禁止选择 JTable 上的特定行

java - JScrollPane 问题

java - QR码扫描后如何添加重定向

java - 在运行时扫描 Java 注释

java - 获取触发 RowSorterEvent 的列

java - Spring Controller 中的正则表达式

java - Java中的JPanel与JFrame