【问题标题】:Trying to get focus onto JTextPane after doubleclicking on JList element (Java)双击 JList 元素(Java)后试图将注意力集中在 JTextPane 上
【发布时间】:2010-05-03 17:23:39
【问题描述】:

问题:

我将以下 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 上

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

【问题讨论】:

    标签: java swing jlist jtextpane caret


    【解决方案1】:

    看看JComponent中的焦点方法并尝试一下

    特别是grabFocusrequestFocusInWindow

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

    【讨论】:

    • @aioobe:嗯。首先尝试它有效。然后我再次双击列表,文本出现在 textPane 上后,不再有插入符号。为什么这么奇怪?
    • 我注意到第二次双击后,焦点运行到JToolBar(其中一个按钮)。我该怎么办?
    • 尝试将抓取焦点包含在 SwingUtilities.invokeLater 中。
    • 成功了。在我感谢你之前,你介意告诉我你怎么知道使用 invokeLater 吗?另外,我应该在哪里寻找更多关于 Swing 的知识?谢谢!
    • @camickr:感谢您指出这一点。我应该改用requestFocusInWindow()
    【解决方案2】:

    虽然与您的问题无关,但您可能需要查看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 );
     }
    }
    

    【讨论】:

    • @camickr:感谢您的链接。非常感谢。编辑:......哇,这很整洁。
    • 嗯,它是否与另一个 InputMap 一起工作,因为我有一个用于 UndoAction 和 RedoAction(取自 Sun 的示例)?如果您不向两者添加相同的键和操作,应该没问题吧?
    • 只要 KeyStroke 不同,它就可以工作。但是在这种情况下甚至不应该成为问题,因为这会增加对 JList 的绑定,我猜您的 Undo/Redo 已绑定到文本窗格,因此这是两个完全不同的输入映射。每个组件都有自己的输入映射。
    猜你喜欢
    • 2021-06-15
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 2016-01-03
    相关资源
    最近更新 更多