简体   繁体   English

如何检查Swing中是否选中了文本?

[英]How to check that text was selected in Swing?

I have simple JTextField and KeyListener. 我有简单的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();
   }
});

How do I check that someone select text with key combination (SHIFT + LEFT or RIGHT ARROW) ? 如何检查是否有人用组合键(SHIFT + LEFT或RIGHT ARROW)选择文本?

Swing makes heavy use of the Key Bindings API to make it easy to work with existing functionality. Swing大量使用了Key Bindings API ,以使其易于使用现有功能。 We already know the JTextField is fully capable of performing selection, we just need to be able to plug into it. 我们已经知道JTextField完全有能力执行选择,我们只需要将其插入即可。

The JTextField uses the selection-backward and selection-forward to execute the required functionality when the system dependent key strokes are activated, we just need to inject our code into it. 当依赖于系统的按键被激活时, JTextField使用selection-backward selection-forward来执行所需的功能,我们只需要向其中注入代码即可。

For this, I wrote a simple ReplaceAction action, which takes the old Action we are interested, and calls two methods, one before and one after the old Action is called. 为此,我编写了一个简单的ReplaceAction动作,该动作采用了我们感兴趣的旧Action ,并调用了两个方法,一个在调用旧Action之前,另一个在调用之后。 This allows you to inject your required functionality into whatever point is required to achieve whatever functionality you are trying to implement... 这使您可以将所需的功能注入实现您要尝试实现的任何功能所需的任何点。

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() {
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM