Swing/ParticularyNonEditableSpinner のバックアップ(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/ParticularyNonEditableSpinner へ行く。
- 1 (2012-10-15 (月) 18:30:23)
- 2 (2013-01-02 (水) 14:37:24)
- 3 (2014-11-22 (土) 03:59:58)
- 4 (2015-03-06 (金) 19:04:10)
- 5 (2016-05-26 (木) 15:27:16)
- 6 (2017-08-10 (木) 14:44:43)
- 7 (2017-11-02 (木) 15:32:16)
- 8 (2018-08-17 (金) 13:29:49)
- 9 (2020-08-12 (水) 11:30:37)
- 10 (2022-01-05 (水) 08:27:21)
- 11 (2025-01-03 (金) 08:57:02)
- 12 (2025-01-03 (金) 09:01:23)
- 13 (2025-01-03 (金) 09:02:38)
- 14 (2025-01-03 (金) 09:03:21)
- 15 (2025-01-03 (金) 09:04:02)
TITLE:JSpinnerのテキストフィールド内に選択不可の文字を追加する
Posted by aterai at 2010-01-04
JSpinnerのテキストフィールド内に選択不可の文字を追加する
JSpinnerのテキストフィールド内に選択や編集ができない文字列を追加します。
- &jnlp;
- &jar;
- &zip;
サンプルコード
class StringBorder implements Border {
private final JComponent parent;
private final Insets insets;
private final Rectangle rect;
private final String str;
public StringBorder(JComponent parent, String str) {
this.parent = parent;
this.str = str;
FontRenderContext frc = new FontRenderContext(null, true, true);
rect = parent.getFont().getStringBounds(str, frc).getBounds();
insets = new Insets(0,0,0,rect.width);
}
@Override
public Insets getBorderInsets(Component c) {
return insets;
}
@Override
public boolean isBorderOpaque() {
return false;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D)g;
float tx = x + width - rect.width;
float ty = y - rect.y + (height - rect.height)/2;
//g2.setPaint(Color.RED);
g2.drawString(str, tx, ty);
}
}
View in GitHub: Java, Kotlin解説
- 上: JSpinner.NumberEditor + DecimalFormat
- JSpinnerの値をパーセントで指定
JSpinner spinner1 = new JSpinner(new SpinnerNumberModel(0, 0, 1, 0.01)); JSpinner.NumberEditor editor1 = new JSpinner.NumberEditor(spinner1, "0%"); spinner1.setEditor(editor1);
- JSpinnerの値をパーセントで指定
- 下: JSpinner + StringBorder
- JSpinnerのエディタに余白を設定し、そこに文字列を描画しています。
JSpinner spinner2 = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1)); JSpinner.NumberEditor editor2 = new JSpinner.NumberEditor(spinner2); spinner2.setEditor(editor2); editor2.setOpaque(true); editor2.setBackground(Color.WHITE); //Border b = new StringBorder(editor2, "percent"); Border b = new StringBorder(editor2, "%"); Border c = editor2.getBorder(); editor2.setBorder((c==null)?b:BorderFactory.createCompoundBorder(c,b));
- JSpinnerのエディタに余白を設定し、そこに文字列を描画しています。
以下のように、Component Border ≪ Java Tips Weblogを利用して、JLabelを余白に描画する方法もあります。
JLabel label = new JLabel("%");
label.setBorder(BorderFactory.createEmptyBorder());
label.setOpaque(true);
label.setBackground(Color.WHITE);
ComponentBorder cb = new ComponentBorder(label);
cb.setGap(0);
cb.install(editor2);