JSpinnerのArrowButtonのサイズを変更
Total: 2829, Today: 1, Yesterday: 3
Posted by aterai at
Last-modified:
Summary
JSpinnerのArrowButtonの幅と高さを変更します。
Screenshot

Advertisement
Source Code Examples
JSpinner spinner4 = new JSpinner(model) {
@Override public void updateUI() {
super.updateUI();
setFont(getFont().deriveFont(32f));
stream(this)
.filter(JButton.class::isInstance)
.map(JButton.class::cast)
.forEach(b -> {
Dimension d = b.getPreferredSize();
d.width = 50;
b.setPreferredSize(d);
});
}
};
View in GitHub: Java, KotlinDescription
defaultJSpinnerで使用するBasicArrowButtonの推奨サイズは幅が16pxで固定、高さはJSpinnerの高さの半分
Spinner.arrowButtonSizeUIManager.put("Spinner.arrowButtonSize", new Dimension(60, 0));などで幅を指定可能(高さの指定は無視される)- 幅固定の
BasicArrowButtonを使用するMetalLookAndFeelやMotifLookAndFeelなどでは無効
setPreferredSizeJSpinnerの子コンポーネントからJButtonを検索してJButton#setPreferredSize(...)で幅を変更- 幅固定の
BasicArrowButtonを使用するMetalLookAndFeelやMotifLookAndFeelなどでは無効
setLayoutLayoutManager#layoutContainer(...)メソッドをオーバーライドしてArrowButtonの幅を推奨サイズを無視して変更LookAndFeelに関係なく幅を変更可能
setPreferredSizeでの幅変更と合わせてJSpinnerのフォントサイズを変更してArrowButtonの高さも変更
Reference
- Containerの子Componentを再帰的にすべて取得する
JButtonの検索に使用
- JComboBoxのArrowButtonを隠す
JSpinnerには、JComboBoxのUIManager.put("ComboBox.squareButton", Boolean.FALSE);のような設定はない
- JSpinnerのボタンを左右に配置する
JSpinnerのレイアウト変更方法