Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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 );
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top