Frage

Problem:

Ich habe folgendes JList, die ich den TextPane hinzufügen, und zeigen Sie es auf dem caret bewegend. Doch nach einem Doppelklick auf dem JList Elemente, wird der Text eingefügt, aber die Einfügemarke nicht, die auf der JTextPane.

Dies ist der folgende 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();

Jede Idee, wie man "de-Fokus", um die Auswahl auf dem JList oder machen die auf dem JTextPane nach dem Text erscheinen CARET Einfügen

Ich werde mehr erarbeiten, wenn dies nicht klar genug ist. Bitte helfen, danke!

War es hilfreich?

Lösung

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

Andere Tipps

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 );
 }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top