双击 JList 元素后尝试将焦点集中到 JTextPane (Java)

发布于 2024-08-30 20:00:48 字数 1869 浏览 3 评论 0原文

问题:

我有以下 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 上,有什么想法吗?

如果这还不够清楚,我会详细说明。请帮忙,谢谢!

Problem:

I have the following JList which I add to the textPane, and show it upon the caret moving. However, after double clicking on the Jlist element, the text gets inserted, but the caret is not appearing on the JTextPane.

This is the following code:

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();

Any idea on how to "de-focus" the selection on the Jlist, or make the caret appear on the JTextPane after the text insertion?

I'll elaborate more if this is not clear enough. Please help, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2024-09-06 20:00:48

查看并使用 JComponent

特别是 grabFocusrequestFocusInWindow

例如,如果您在 textPane 之后添加 textPane.grabFocus() 会发生什么.moveCaretPosition(pos);?

Have a look and play around with the focus-methods in JComponent

Specifically grabFocus and requestFocusInWindow

What happens for instance, if you add textPane.grabFocus() after textPane.moveCaretPosition(pos);?

给我一枪 2024-09-06 20:00:48

虽然与您的问题无关,但您可能需要查看列表操作< /a>,它尝试以更通用的方式处理此类请求。

编辑:

这是我的简单 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 );
 }
}

Although not related to your problem, you may want to check out the List Action, which attempts to handle this type of request in a more general way.

Edit:

Here is my simple SSCCE that shows invokeLater is not required:

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 );
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文