2010-05-03 27 views
3

问题:试图让聚焦到JTextPane中双击JList的元素(爪哇)后

我有以下的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?

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

回答

3

看一看在玩的对焦方法JComponent

具体grabFocusrequestFocusInWindow

如果​​后添加textPane.grabFocus()例如,会发生什么?

+0

@aioobe:嗯。首先尝试它的工作。然后我再次双击列表,并在textPane上显示文本后,不再有插入符号。为什么这很奇怪? – 2010-05-03 18:55:19

+0

我注意到,第二次双击后,焦点运行到'JToolBar'(其中一个按钮)。我该怎么办? – 2010-05-03 18:59:07

+2

尝试在SwingUtilities.invokeLater中放置grabfocus。 – aioobe 2010-05-03 19:29:38

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); 
} 
} 
+0

@camickr:感谢您的链接。非常感谢。编辑:...哇,这很整齐。 – 2010-05-03 19:55:10

+0

嗯,它是否与另一个InputMap一起工作,因为我有一个用于UndoAction和RedoAction的(从Sun的例子中)?如果你不添加相同的键和动作,它应该没问题吧? – 2010-05-03 20:03:07

+0

只要KeyStroke不同,它应该可以工作。然而,在这种情况下,甚至不应该是一个问题,因为这增加了JList的绑定,我猜你的撤消/重做被绑定到文本窗格,所以这些是两个完全不同的输入地图。每个compnent都有自己的输入映射。 – camickr 2010-05-03 23:48:14