什么文本输入组件最后的焦点? [英] What text input component last had the focus?

查看:146
本文介绍了什么文本输入组件最后的焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Java应用程序,它有多个可以输入文本的组件。现在假设这个应用程序也有一个对话框,让你插入一个单独的字符(比如当你从编辑菜单中选择插入时出现的Word中的对话框)到这些组件中。您希望它将字符插入到最后有焦点的文本组件中。



但是你怎么知道哪个文本组件最后有焦点?

我可以跟踪这个手动通过让每个文本组件在获得焦点时向应用程序报告,然后让应用程序将新的字符插入到最后有焦点的组件中去。



必须是一个常见的问题(考虑粘贴工具栏中的按钮---它怎么知道粘贴到哪里?)。是否有一些已经内置到Swing,让你得到最后的文本组件的焦点?或者我需要自己写这个?

解决方案


让你得到最后一个有焦点的文本组件的句柄?


创建一个扩展TextAction的Action。 TextAction类有一个方法,可以让你获得最后一个有焦点的文本组件。



编辑:

您可以创建自己的操作并执行任何您想要的操作。 Action可以被添加到任何JMenuItem或者JButton中。例如:

  class SelectAll extends TextAction 
{
public SelectAll()
{
super(全选);
}

public void actionPerformed(ActionEvent e)
{
JTextComponent component = getFocusedComponent();
component.selectAll();






$ p如果你只是想插入字符文本字段的位置,那么你可能只需要做

pre $ component $ .replaceSelection(...);

编辑2:

<不要理解这个答案有什么困惑。这是一个简单的例子:


  1. 选择一些文本

  2. 使用鼠标点击box

  3. 标签或使用鼠标点击剪切按钮

当Action被调用时,文本字段当前没有焦点。 TextAction跟踪最后有焦点的文本组件。

  import java.awt。*; 
import java.awt.event。*;
import javax.swing。*;
import javax.swing.text。*;

public class TextActionTest extends JFrame
{
JTextField textField = new JTextField(Select Me);
JTabbedPane tabbedPane;
$ b $ public TextActionTest()
{
add(textField,BorderLayout.NORTH);
add(new JCheckBox(Click Me!));
add(new JButton(new CutAction()),BorderLayout.SOUTH);


public static void main(String [] args)
{
TextActionTest frame = new TextActionTest();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

$ b $ class CutAction扩展TextAction
$ b $ public CutAction()
{
super(Click to Cut Text);
}

public void actionPerformed(ActionEvent e)
{
JTextComponent component = getFocusedComponent();
// JTextComponent component = getTextComponent(e);
component.cut();
}
}
}


Suppose I have a Java application that has more than one component in which you can enter text. Now suppose this application also has a dialog that lets you insert a single character (like the dialog in Word that comes up when you select Insert from the Edit menu) into those components. You want it to insert the character into whichever text component last had the focus.

But how do you know which text component last had the focus?

I could keep track of this manually, by having each text component report to the application whenever it gets the focus and then have the application insert the new character into whichever component that last had the focus.

But this must be a common problem (consider Paste buttons in tool bars---how does it know where to paste it into?). Is there something already built in to Swing that lets you get a handle to the last text component that had the focus? Or do I need to write this myself?

解决方案

Is there something already built in to Swing that lets you get a handle to the last text component that had the focus?

You create an Action that extends TextAction. The TextAction class has a method that allows you to obtain the last text component that had focus.

Edit:

You can create your own Action and do whatever you want. The Action can then be added to any JMenuItem or JButton. For example:

class SelectAll extends TextAction
{
    public SelectAll()
    {
        super("Select All");
    }

    public void actionPerformed(ActionEvent e)
    {
        JTextComponent component = getFocusedComponent();
        component.selectAll();
    }
}

If you just want to insert a character at the caret position of the text field then you can probably just do

component.replaceSelection(...);

Edit 2:

I don't understand what the confusion is with this answer. Here is a simple example:

  1. select some text
  2. use the mouse to click on the check box
  3. tab or use the mouse to click on the "Cut" button

It doesn't matter that the text field doesn't currently have focus when the Action is invoked. The TextAction tracks the last text component that had focus.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextActionTest extends JFrame
{
    JTextField textField = new JTextField("Select Me");
    JTabbedPane tabbedPane;

    public TextActionTest()
    {
        add(textField, BorderLayout.NORTH);
        add(new JCheckBox("Click Me!"));
        add(new JButton(new CutAction()), BorderLayout.SOUTH);
    }

    public static void main(String[] args)
    {
        TextActionTest frame = new TextActionTest();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }

    class CutAction extends TextAction
    {
        public CutAction()
        {
            super("Click to Cut Text");
        }

        public void actionPerformed(ActionEvent e)
        {
            JTextComponent component = getFocusedComponent();
//          JTextComponent component = getTextComponent(e);
            component.cut();
        }
    }
}

这篇关于什么文本输入组件最后的焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆