Nếu bạn chạy mẫu nhỏ bên dưới, bạn sẽ thấy một đường viền xung quanh khu vực trung tâm. Tôi không chắc tại sao đường viền này lại hiển thị.Cách loại bỏ đường viền bằng JTable/JScrollPane
Điều này xảy ra khi một JTable nằm trong JScrollPane. Tôi đã thử những thứ khác nhau để loại bỏ nó nhưng cho đến nay không có may mắn. Một JTable không có JScrollPane cho thấy không có đường viền.
Xem ví dụ bên dưới. 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));
}
}
Thay vì đặt đường viền rỗng, bạn có thử dùng BorderFactory.createEmptyBorder() không? –
Ngoài ra, đừng bỏ qua 'pack()' 'frame'. – trashgod