JTableの本体、ヘッダ、親JScrollPaneなどにBorderを設定する
Total: 6097, Today: 1, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
JTableやその内部のJTableHeader、親のJScrollPaneやJViewportにそれぞれ異なるBorderを設定するテストを行います。
Screenshot

Advertisement
Source Code Examples
JTable table = new JTable(new DefaultTableModel(15, 3));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getTableHeader().setBorder(
BorderFactory.createMatteBorder(0, 5, 0, 5, Color.ORANGE));
table.setBorder(BorderFactory.createLineBorder(Color.GREEN, 5));
JScrollPane scroll = new JScrollPane(table);
scroll.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
scroll.setViewportBorder(BorderFactory.createLineBorder(Color.RED, 5));
scroll.setBackground(Color.YELLOW);
scroll.getViewport().setBackground(Color.PINK);
table.setBackground(Color.WHITE);
table.getTableHeader().setBackground(Color.MAGENTA);
EventQueue.invokeLater(() -> {
JViewport vp = scroll.getColumnHeader();
vp.setOpaque(true);
vp.setBackground(Color.CYAN);
});
View in GitHub: Java, KotlinDescription
GREENJTable自体にJTable#setBorder(...)で線幅5pxのLineBorderを設定- セル内部に
LineBorderが入り込んでしまう
ORANGEJTableHeaderにJTableHeader#setBorder(...)で左右の幅が5pxのMatteBorderを設定
BLUE- 親
JScrollPaneにJScrollPane#setBorder(...)で線幅5pxのLineBorderを設定
- 親
RED- 親
JScrollPaneのJViewportにJScrollPane#setViewportBorder(...)で線幅5pxのLineBorderを設定 - セルの縦罫線が
JTableとJTableHeaderでズレてしまう
- 親
YELLOW- 親
JScrollPaneの背景色 JScrollBarが表示される場合、その余白の背景色になる
- 親
PINK- 親
JScrollPaneの中央JViewportの背景色
- 親
WHITEJTableの背景色
MAGENTAJTableHraderの背景色WindowsLookAndFeelの場合、ヘッダーセルの縦罫線なる?
CYAN- 親
JScrollPaneのColumnHeader用JViewportの背景色 - 列ヘッダをドラッグしたあとのヘッダ背景色になる
- 親