• category: swing folder: title: JTableの本体、ヘッダ、親JScrollPaneなどにBorderを設定する tags: [JTable, Border, JTableHeader, JScrollPane] author: aterai pubdate: 2018-06-11T15:29:35+09:00 description: JTableやその内部のJTableHeader、親のJScrollPaneやJViewportにそれぞれ異なるBorderを設定するテストを行います。 image: https://drive.google.com/uc?id=1QoXjiHkRpuR6I_IrsTRbEOcVi3-02nmReg

概要

JTableやその内部のJTableHeader、親のJScrollPaneJViewportにそれぞれ異なるBorderを設定するテストを行います。

サンプルコード

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, Kotlin

解説

  • GREEN
    • JTable自体にJTable#setBorder(...)で線幅5pxLineBorderを設定
    • セル内部でLineBorderが入り込んでしまう
  • ORANGE
    • JTableHeaderJTableHeader#setBorder(...)で左右の幅5pxMatteBorderを設定
  • BLUE
    • JScrollPaneJScrollPane#setBorder(...)で線幅5pxLineBorderを設定
  • RED
    • JScrollPaneJViewportJScrollPane#setViewportBorder(...)で線幅5pxLineBorderを設定
    • セルの縦罫線がJTableJTableHeaderでズレてしまう

  • YELLOW
    • JScrollPaneの背景色
    • JScrollBarが表示れる場合、その余白の背景色になる
  • PINK
    • JScrollPaneの中央JViewportの背景色
  • WHITE
    • JTableの背景色
  • MAGENTA
    • JTableHraderの背景色
    • WindowsLookAndFeelの場合、ヘッダーセルの縦罫線なる?
  • CYAN
    • JScrollPaneColumnHeaderJViewportの背景色
    • 列ヘッダをドラッグしたあとのヘッダ背景色になる

参考リンク

コメント