• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableの罫線の有無とセルの内余白を変更
#navi(../)
#tags(JTable)
RIGHT:Posted by &author(aterai); at 2011-05-30
* JTableの罫線の有無とセルの内余白を変更 [#mb66743f]
---
category: swing
folder: IntercellSpacing
title: JTableの罫線の有無とセルの内余白を変更
tags: [JTable]
author: aterai
pubdate: 2011-05-30T16:37:49+09:00
description: JTableの罫線の表示非表示とセルの内余白を変更します。
image: https://lh3.googleusercontent.com/-zDg_KUxGwU4/TeNHkhhJYGI/AAAAAAAAA8M/G5R8rKLVzUg/s800/IntercellSpacing.png
---
* 概要 [#summary]
`JTable`の罫線の表示非表示とセルの内余白を変更します。

- &jnlp;
- &jar;
- &zip;
#download(https://lh3.googleusercontent.com/-zDg_KUxGwU4/TeNHkhhJYGI/AAAAAAAAA8M/G5R8rKLVzUg/s800/IntercellSpacing.png)

#ref(https://lh3.googleusercontent.com/-zDg_KUxGwU4/TeNHkhhJYGI/AAAAAAAAA8M/G5R8rKLVzUg/s800/IntercellSpacing.png)

** サンプルコード [#hf7ab7cd]
* サンプルコード [#sourcecode]
#code(link){{
Dimension d = table.getIntercellSpacing();
table.setShowVerticalLines(false);
table.setIntercellSpacing(new Dimension(0,d.height));
//table.setShowHorizontalLines(false);
//table.setIntercellSpacing(new Dimension(d.width,0));
add(new JCheckBox(new AbstractAction("setShowVerticalLines") {
  @Override public void actionPerformed(ActionEvent e) {
    Dimension d = table.getIntercellSpacing();
    if (((JCheckBox) e.getSource()).isSelected()) {
      table.setShowVerticalLines(true);
      table.setIntercellSpacing(new Dimension(1, d.height));
    } else {
      table.setShowVerticalLines(false);
      table.setIntercellSpacing(new Dimension(0, d.height));
    }
  }
}));
add(new JCheckBox(new AbstractAction("setShowHorizontalLines") {
  @Override public void actionPerformed(ActionEvent e) {
    Dimension d = table.getIntercellSpacing();
    if (((JCheckBox) e.getSource()).isSelected()) {
      table.setShowHorizontalLines(true);
      table.setIntercellSpacing(new Dimension(d.width, 1));
    } else {
      table.setShowHorizontalLines(false);
      table.setIntercellSpacing(new Dimension(d.width, 0));
    }
  }
}));
}}

** 解説 [#y7260618]
`JTable`の罫線を非表示にしてもセルの内余白が`0`でない場合、選択状態でその内余白が表示されるので、上記のサンプルでは、`JTable#setShowVerticalLines(boolean)`などと一緒に、`JTable#setIntercellSpacing(Dimension)`で余白を`0`に切り替えています。
* 解説 [#explanation]
- `JTable`の罫線を非表示にしてもセルの内余白が`0`でない場合、セル選択でその内余白分の塗り残しが発生し分割状態で表示される
- `JTable#setShowVerticalLines(boolean)`メソッドなどと合わせて`JTable#setIntercellSpacing(Dimension)`メソッドを使用し、セルの内余白を`0`に変更
- 罫線の設定
-- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setShowVerticalLines-boolean- JTable#setShowVerticalLines(boolean)]
-- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setShowHorizontalLines-boolean- JTable#setShowHorizontalLines(boolean)]
-- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setShowGrid-boolean- JTable.html#setShowGrid(boolean)]
--- `JTable#setShowVerticalLines(boolean)`と`JTable#setShowHorizontalLines(boolean)`をまとめて実行
- セル内余白の設定
-- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setIntercellSpacing-java.awt.Dimension- JTable#setIntercellSpacing(Dimension intercellSpacing)]
--- `JTable#setRowMargin(intercellSpacing.height)`と`JTable#getColumnModel().setColumnMargin(intercellSpacing.width)`をまとめて実行

- 罫線
-- `JTable#setShowVerticalLines(boolean);`
-- `JTable#setShowHorizontalLines(boolean);`
- セルの内余白
-- `JTable#setIntercellSpacing(Dimension intercellSpacing);`
--- `JTable#setRowMargin(intercellSpacing.height);`
--- `JTable#getColumnModel().setColumnMargin(intercellSpacing.width);`
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setShowVerticalLines-boolean- JTable#setShowVerticalLines(boolean) (Java Platform SE 8)]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setShowHorizontalLines-boolean- JTable#setShowHorizontalLines(boolean) (Java Platform SE 8)]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setIntercellSpacing-java.awt.Dimension- JTable#setIntercellSpacing(Dimension) (Java Platform SE 8)]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setShowGrid-boolean- JTable#setShowGrid(boolean) (Java Platform SE 8)]
- [[JTableのグリッド線描画をUIDefaultsから復元する>Swing/TableShowGrid]]

//**参考リンク
** コメント [#j8f013a4]
* コメント [#comment]
#comment
#comment