3

問題:

次の JList を textPane に追加し、キャレットの移動時に表示します。ただし、Jlist 要素をダブルクリックすると、テキストが挿入されますが、JTextPane にキャレットが表示されません。

これは次のコードです。

listForSuggestion = new JList(str.toArray());
        listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listForSuggestion.setSelectedIndex(0);
        listForSuggestion.setVisibleRowCount(visibleRowCount);
        listScrollPane = new JScrollPane(listForSuggestion);
        MouseListener mouseListener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent mouseEvent) {
                JList theList = (JList) mouseEvent.getSource();
                if (mouseEvent.getClickCount() == 2) {
                    int index = theList.locationToIndex(mouseEvent.getPoint());
                    if (index >= 0) {
                        Object o = theList.getModel().getElementAt(index);
                        //System.out.println("Double-clicked on: " + o.toString());
                        //Set the double clicked text to appear on textPane
                        String completion = o.toString();
                        int num= textPane.getCaretPosition();
                        textPane.select(num, num);
                        textPane.replaceSelection(completion);
                        textPane.setCaretPosition(num + completion.length());
                        int pos = textPane.getSelectionEnd();
                        textPane.select(pos, pos);
                        textPane.replaceSelection("");
                        textPane.setCaretPosition(pos);
                        textPane.moveCaretPosition(pos);
                    }
                }
                theList.clearSelection();

Jlist で選択を「焦点を外す」方法、またはテキスト挿入後にキャレットを JTextPane に表示する方法についてのアイデアはありますか?

これが十分に明確でない場合は、さらに詳しく説明します。助けてください、ありがとう!

4

2 に答える 2

4

JComponentのfocus-methodsを見て、試してみてください

具体的grabFocusにはrequestFocusInWindow

textPane.grabFocus()たとえば、後に追加するとどうなりますtextPane.moveCaretPosition(pos);か?

于 2010-05-03T17:28:24.773 に答える
0

あなたの問題とは関係ありませんが、このタイプのリクエストをより一般的な方法で処理しようとするList Actionを確認することをお勧めします。

編集:

これは、invokeLater が不要であることを示す単純な SSCCE です。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ListActionTest
{
 public static void main(String[] args)
  throws Exception
 {
  final JTextField textField = new JTextField();

  Action displayAction = new AbstractAction()
  {
   public void actionPerformed(ActionEvent e)
   {
    JList list = (JList)e.getSource();
    System.out.println(list.getSelectedValue());
    textField.setText(list.getSelectedValue().toString());
    textField.requestFocusInWindow();
   }
  };

  String[] data = { "zero", "one", "two", "three", "four", "five" };
  JList list = new JList( data );

  ListAction la = new ListAction(list, displayAction);

  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  frame.getContentPane().add( new JScrollPane(list) );
  frame.add(textField, BorderLayout.SOUTH);
  frame.setSize(400, 100);
  frame.setLocationRelativeTo( null );
  frame.setVisible( true );
 }
}
于 2010-05-03T19:47:31.460 に答える