- PR -

JComboBox オートコンプリートについて

1
投稿者投稿内容
ミジンコ
会議室デビュー日: 2003/12/20
投稿数: 2
投稿日時: 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();
}
   }
 }
}
まさ
ベテラン
会議室デビュー日: 2002/11/15
投稿数: 74
投稿日時: 2003-12-22 12:37
まさです。

CBDocument で下記のようにしてみてください。
どうでしょうか?

コード:
    public void insertString(int offset, String str, AttributeSet a)
            throws BadLocationException {

        //  インプットメソッドのみ確定文字列の判定
        if ( a!=null && a.isDefined(StyleConstants.ComposedTextAttribute) ) {
            /*
             *  たとえば、IME での入力中(未確定文字列)に対してこの部分の処理が
             *  行われる。入力中の文字は表示として一応 insertString するが、
             *  これが確定されたときに再び insertString が呼び出され、
             *  そのときには else 部分に処理が移る( a==null となる)。
             *  確定された文字列が有効文字列かどうかのチェックを行うのは
             *  else 部分の処理として行う。
             */
            super.insertString(offset, str, a);

        } else {
            :
            :
        }


ミジンコ
会議室デビュー日: 2003/12/20
投稿数: 2
投稿日時: 2003-12-25 09:54
まささん. ご教授本当に有難う御座いました.

今の所,例えば「テ」で入力すると「テ」候補が反映されるようになりました、が
次に「ス」と入れても「テス」の候補がでて来ません.

if ( a!=null && a.isDefined(StyleConstants.ComposedTextAttribute) ) {
 super.insertString(offset,str,a);
}else{
..}

の様にするとIMEタイプされた文字は次々とたまっていかないのが
原因のような気がしてます。 
日本語の場合でも,文字入力毎に候補を表示させる方法は
御座いますでしょうか。 お願いします

ミジンコ
@ビギナーです。 
1

スキルアップ/キャリアアップ(JOB@IT)