- - PR -
JComboBox オートコンプリートについて
1
| 投稿者 | 投稿内容 | ||||
|---|---|---|---|---|---|
|
投稿日時: 2003-12-20 14:07
皆さんのお力を頂きたくて投稿させて頂きました.
ご教授お願いします. JComboBoxにおいて,オートコンプリート機能をつけたいと考えております. アルファベットの場合うまく作動しますが,日本語入力の場合どうしても変換のとき スペースキーを押し動作がおかしくなってしまいます. setKeySelectionManager, KeyListner,など色々試してみました.が、 どれもこれもうまくいかず悩んでいます. お願いします. 以下のようなコードを使うとアルファベットを入力毎に 候補をテキストボックスに表示させることが出来ます. がしかし,日本語においてはスペースキーを押した時点で候補が出てきてしまい 次にReturnを押すと,(String)候補+入力下文字となってしまいます。 addActionListener(new ModifyText());をテキストフィールドのアクション を作動変更させるため以下のコード tf.addActionListener(new ModifyText()); を変えてみると,テキストに日本語入力<スペースを押し候補選び<return確定 でその文字で始まる該当するメニューが表示されますが,キーを押すごとに 候補を見つけ出すということが出来なく常に候補を見つけるとき Returnを押してしまわなければなりません. MS Accessのような機能をつけたいと思っていますが, どうしたらよろしいでしょうか. public class JComboBoxExtextends JComboBox{ public JComboBoxExt(ComboBoxModel aModel) { super(aModel); initialize(aModel); } public void initialize(ComboBoxModel aModel) { JTextField tf; if (getEditor() != null) { tf = (JTextField) getEditor().getEditorComponent(); if (tf != null) { tf.setDocument(new CBDocument()); addActionListener(new ModifyText()); } } } public void fireActionEvent() { super.fireActionEvent(); } class ModifyText implements ActionListener { public void actionPerformed(ActionEvent evt) { JTextField tf = (JTextField) getEditor().getEditorComponent(); String text = tf.getText(); ComboBoxModel aModel = getModel(); String current; for (int i = 0; i < aModel.getSize(); i++) { current = aModel.getElementAt(i).toString(); if (current.toLowerCase().startsWith(text.toLowerCase())) { tf.setText(current); tf.setSelectionStart(text.length()); tf.setSelectionEnd(current.length()); break; } } } } public class CBDocument extends PlainDocument { public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { if (str == null) return; super.insertString(offset, str, a); if (!isPopupVisible() && str.length() != 0) { fireActionEvent(); } } } } | ||||
|
投稿日時: 2003-12-22 12:37
まさです。
CBDocument で下記のようにしてみてください。 どうでしょうか?
| ||||
|
投稿日時: 2003-12-25 09:54
まささん. ご教授本当に有難う御座いました.
今の所,例えば「テ」で入力すると「テ」候補が反映されるようになりました、が 次に「ス」と入れても「テス」の候補がでて来ません. if ( a!=null && a.isDefined(StyleConstants.ComposedTextAttribute) ) { super.insertString(offset,str,a); }else{ ..} の様にするとIMEタイプされた文字は次々とたまっていかないのが 原因のような気がしてます。 日本語の場合でも,文字入力毎に候補を表示させる方法は 御座いますでしょうか。 お願いします ミジンコ @ビギナーです。 | ||||
1
