• category: swing folder: BreadcrumbList title: FlowLayoutでボタンを重ねてパンくずリストを作成する tags: [FlowLayout, JRadioButton, JPanel, Shape, Icon] author: aterai pubdate: 2013-12-02T00:03:12+09:00 description: FlowLayoutの水平間隔をマイナスにして、JRadioButtonを重ねて表示し、パンくずリスト風のコンポーネントを作成します。 image: https://lh5.googleusercontent.com/-aKK_2LaPfVQ/UpsqWSS4lUI/AAAAAAAAB7c/VSzPRuRu3IY/s800/BreadcrumbList.png hreflang:
       href: http://java-swing-tips.blogspot.com/2013/12/breadcrumb-navigation-with-jradiobutton.html
       lang: en

概要

FlowLayoutの水平間隔をマイナスにして、JRadioButtonを重ねて表示し、パンくずリスト風のコンポーネントを作成します。

サンプルコード

private static JComponent makeBreadcrumbList(int overlap, List<String> list) {
  JPanel p = new JPanel(new FlowLayout(FlowLayout.LEADING, -overlap, 0));
  p.setBorder(BorderFactory.createEmptyBorder(4, overlap + 4, 4, 4));
  p.setOpaque(false);
  ButtonGroup bg = new ButtonGroup();
  for (String title: list) {
    AbstractButton b = makeButton(title, Color.PINK);
    p.add(b);
    bg.add(b);
  }
  return p;
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、FlowLayoutの水平間隔にマイナスの値(px)を指定して、各コンポーネントがその分重なるように設定しています。このため、左側のJRadioButtonの下に右側のJRadioButtonが配置されている状態になっています。各JRadioButton自体は重なっていますが、描画とマウスクリックは重ならないように以下の設定を追加しています。

  • 描画
    • 矢羽型のアイコンを設定、このアイコン以外は、setContentAreaFilled(false);などで透明化
  • マウスクリック
    • JRadioButton#contains(...)をオーバーライドして、上記の矢羽図形の外では反応しないよう設定

参考リンク

コメント