Swing/HtmlSpinnerEditor のバックアップ差分(No.1)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/HtmlSpinnerEditor へ行く。
- 1 (2018-11-28 (水) 16:07:56)
- 2 (2019-08-26 (月) 18:22:31)
- 3 (2019-09-03 (火) 14:08:26)
- 4 (2021-04-23 (金) 20:09:06)
- 5 (2024-03-01 (金) 15:55:10)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: HtmlSpinnerEditor title: JSpinnerのエディタをJLabelに変更してHTMLを表示する tags: [JSpinner, HTML] author: aterai pubdate: 2018-04-09T14:48:47+09:00 description: JSpinnerのListEditorをJLabelに変更してテキストをHTMLで表示します。 image: https://drive.google.com/uc?id=1SC3ViNpsTsIE2fgR1wtXawwKHpOD8P1PYg --- * 概要 [#summary] `JSpinner`の`ListEditor`を`JLabel`に変更してテキストをHTMLで表示します。 #download(https://drive.google.com/uc?id=1SC3ViNpsTsIE2fgR1wtXawwKHpOD8P1PYg) * サンプルコード [#sourcecode] #code(link){{ class HtmlListEditor extends JLabel implements ChangeListener { protected HtmlListEditor(JSpinner spinner) { super(); if (!(spinner.getModel() instanceof SpinnerListModel)) { throw new IllegalArgumentException("model not a SpinnerListModel"); } spinner.addChangeListener(this); setText(Objects.toString(spinner.getValue())); setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); setOpaque(true); setBackground(Color.WHITE); setInheritsPopupMenu(true); String toolTipText = spinner.getToolTipText(); if (Objects.nonNull(toolTipText)) { setToolTipText(toolTipText); } } @Override public void stateChanged(ChangeEvent e) { JSpinner spinner = (JSpinner) e.getSource(); setText(Objects.toString(spinner.getValue())); } @Override public Dimension getPreferredSize() { Dimension d = super.getPreferredSize(); d.width = 200; return d; } // @see javax/swing/JSpinner.DefaultEditor.html#dismiss(JSpinner) public void dismiss(JSpinner spinner) { spinner.removeChangeListener(this); } } }} * 解説 [#explanation] - 上: `ListEditor(default)` -- デフォルトの`ListEditor`を使用 -- エディタは`JFormattedTextField`なので、`HTML`は使用できない - 下: `HtmlListEditor` -- `JLabel`を継承する`HtmlListEditor`を作成して`JSpinner#setEditor(...)`で設定 -- `JLabel`がエディタになるので、`HTML`が使用可能で編集不可になる -- `JSpinner.DefaultEditor`を継承していないので、`JSpinner#setEditor(...)`でエディタを変更する場合は、以下のように`JSpinner#removeChangeListener(...)`を呼んで`ChangeListener`を除去する必要がある #code{{ JSpinner spinner = new JSpinner(new SpinnerListModel(items)) { @Override public void setEditor(JComponent editor) { JComponent oldEditor = getEditor(); if (!editor.equals(oldEditor) && oldEditor instanceof HtmlListEditor) { ((HtmlListEditor) oldEditor).dismiss(this); } super.setEditor(editor); } }; }} * 参考リンク [#reference] - [http://www.java2s.com/Tutorials/Java/Swing_How_to/JSpinner/Create_custom_renderer_for_JSpinner_to_show_customized_content_icons.htm Java Swing How to - Create custom renderer for JSpinner to show customized content icons] - [[JComboBoxをJSpinnerの代わりに使用する>Swing/SpinnerTextColor]] -- `JComboBox`を使用することで同様に`HTML`を表示可能だが、`LookAndFeel`が変化してしまう * コメント [#comment] #comment #comment