• category: swing folder: RoundedScrollPane title: JScrollPaneの角を丸める tags: [JScrollPane, Border, JComboBox] author: aterai pubdate: 2020-05-18T15:10:30+09:00 description: JScrollPaneに角丸表示をおこなうためのBorderを設定します。 image: https://drive.google.com/uc?id=1po7Hxu9X7xIcMO6Vs6YOZkzbZmMqyrN3

概要

JScrollPaneに角丸表示をおこなうためのBorderを設定します。

サンプルコード

JScrollPane scroll = new JScrollPane(tree) {
  @Override public void updateUI() {
    super.updateUI();
    getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
    getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
  }
};
scroll.setBackground(tree.getBackground());
scroll.setBorder(new RoundedCornerBorder());
View in GitHub: Java, Kotlin

解説

  • JScrollPane
    • JScrollPaneRoundedCornerBorderを設定して角を丸める
    • JScrollPaneの背景色を内部に配置したコンポーネントの背景色と同じ色に変更
    • JScrollBarの角を丸めて矢印ボタンを非表示化
    • 参考: JTextFieldの角を丸める
  • JComboBox
JComboBox<String> combo = new JComboBox<String>(makeModel()) {
  private transient MouseListener handler;
  private transient PopupMenuListener listener;
  @Override public void updateUI() {
    removeMouseListener(handler);
    removePopupMenuListener(listener);
    UIManager.put(KEY, new TopRoundedCornerBorder());
    super.updateUI();
    setUI(new BasicComboBoxUI() {
      @Override protected JButton createArrowButton() {
        JButton b = new JButton(new ArrowIcon(BACKGROUND, FOREGROUND));
        b.setContentAreaFilled(false);
        b.setFocusPainted(false);
        b.setBorder(BorderFactory.createEmptyBorder());
        return b;
      }

      @Override protected ComboPopup createPopup() {
        return new BasicComboPopup(comboBox) {
          @Override protected JScrollPane createScroller() {
            JScrollPane sp = new JScrollPane(list) {
              @Override public void updateUI() {
                super.updateUI();
                getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
                getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
              }
            };
            sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
            sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            sp.setHorizontalScrollBar(null);
            return sp;
          }
        };
      }
    });
    handler = new ComboRolloverHandler();
    addMouseListener(handler);
    listener = new HeavyWeightContainerListener();
    addPopupMenuListener(listener);
    Object o = getAccessibleContext().getAccessibleChild(0);
    if (o instanceof JComponent) {
      JComponent c = (JComponent) o;
      c.setBorder(new BottomRoundedCornerBorder());
      c.setForeground(FOREGROUND);
      c.setBackground(BACKGROUND);
    }
  }
};

参考リンク

コメント