How to get rid of the border with a JTable / JScrollPane

前端 未结 6 1272
既然无缘
既然无缘 2021-02-06 22:18

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.

相关标签:
6条回答
  • 2021-02-06 22:42

    Interestingly the border disappears when you remove this line:

    sp.setBorder(null);
    
    0 讨论(0)
  • 2021-02-06 22:51

    I was looking for the answer for the same question but above answers could not do... so I found a better answer:

    JScrollPane jsp = new JScrollPane();
    
    //ur other codes
    
    jsp.setViewportBorder(null);
    
    0 讨论(0)
  • 2021-02-06 22:51

    To remove the border from all parts of the JScrollPane including the vertical and horizontal bar the following code works

    JScrollPane jsp = new JScrollPane();
    jsp.getVerticalScrollBar().setBorder(null);
    jsp.getHorizontalScrollBar().setBorder(null);
    jsp.setBorder(null);
    
    0 讨论(0)
  • 2021-02-06 22:59

    I think the proper fix is to set the border on the viewportView to 'null'.

    0 讨论(0)
  • 2021-02-06 23:00

    Use BorderFactory.createEmptyBorder() instead of null...

    by using:

    sp.setBorder(createEmptyBorder());
    

    it works.

    Your main method becomes:

    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);
        sp.setBorder(BorderFactory.createEmptyBorder());
        panel.add(sp, BorderLayout.CENTER);
        frame.add(panel);
    
        frame.setVisible(true);
    }
    
    0 讨论(0)
  • 2021-02-06 23:00

    For JTable table.setIntercellSpacing(new Dimension(0, 0)) works.

    0 讨论(0)
提交回复
热议问题