如何检查在Swing中选择的文本?

时间:2016-01-14 09:46:17

标签: java swing jtextfield

我有简单的JTextField和KeyListener。

JTextField textField = new JTextField();
textField.addKeyListener(new KeyListener() {

@Override
   public void keyTyped(KeyEvent e)
   {
   }

   @Override
   public void keyPressed(KeyEvent e)
   {
   }

   @Override
   public void keyReleased(KeyEvent e)
   {
      validateThatTextWasSelectedWithShiftAndArrow();
   }
});

如何检查有人选择带有组合键的文本(SHIFT + LEFT或RIGHT ARROW)?

1 个答案:

答案 0 :(得分:2)

Swing大量使用Key Bindings API,以便轻松使用现有功能。我们已经知道JTextField完全能够执行选择,我们只需要能够插入它。

JTextField使用selection-backwardselection-forward在激活系统相关的键击时执行所需的功能,我们只需要将代码注入其中。

为此,我编写了一个简单的ReplaceAction操作,它采用了我们感兴趣的旧Action,并调用了两个方法,一个在调用旧Action之前和之后。这允许您将所需的功能注入到实现您尝试实现的任何功能所需的任何点......

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MonitorSelection {

    public static void main(String[] args) {
        new MonitorSelection();
    }

    public MonitorSelection() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JTextField field = new JTextField(10);
            add(field);
            InputMap im = field.getInputMap(WHEN_FOCUSED);
            ActionMap am = field.getActionMap();

            Action oldAction = am.get("selection-backward");
            am.put("selection-backward", new ReplacedAction(oldAction){
                @Override
                protected void doAfterReplacedAction() {
                    System.out.println("Before selection-backward");
                }

                @Override
                protected void doBeforeReplacedAction() {
                    System.out.println("After selection-backward");
                }

            });
            oldAction = am.get("selection-forward");
            am.put("selection-forward", new ReplacedAction(oldAction){
                @Override
                protected void doAfterReplacedAction() {
                    System.out.println("Before selection-forward");
                }

                @Override
                protected void doBeforeReplacedAction() {
                    System.out.println("After selection-forward");
                }

            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class ReplacedAction extends AbstractAction {
        private Action replaced;

        public ReplacedAction(Action replaced) {
            this.replaced = replaced;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            doBeforeReplacedAction();
            replaced.actionPerformed(e);
            doAfterReplacedAction();
        }

        protected void doBeforeReplacedAction() {
        }

        protected void doAfterReplacedAction() {
        }

    }

}