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中的焦点方法

具体grabFocusrequestFocusInWindow

例如,如果添加textPane.grabFocus()after会发生什么textPane.moveCaretPosition(pos);

于 2010-05-03T17:28:24.773 回答
0

尽管与您的问题无关,但您可能需要查看List Action,它尝试以更通用的方式处理此类请求。

编辑:

这是我的简单 SSCCE,显示不需要 invokeLater:

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 回答