What was the last component of text input?

Suppose I have a Java application that contains more than one component into which you can enter text. Now suppose that this application also has a dialog box that allows you to insert one character into these components (for example, the dialog in Word that appears when you select Insert from the Edit menu). You want it to insert a character into any text component that was the last time.

But how did you know which text component was the last?

I could track this manually by reporting each text component to the application when it receives focus, and then the application inserts a new character into any component that last had focus.

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

+4
source share
4 answers

Is there something already built into Swing that allows you to get the handle to the last text component that had focus?

You create an action that extends TextAction. The TextAction class has a method that allows you to get the last text component with focus.

Edit:

You can create your own action and do whatever you want. You can then add the action to any JMenuItem or JButton. For instance:

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 in the caret position of the text field, you can simply do

 component.replaceSelection(...); 

Edit 2:

I do not understand what confusion is with this answer. Here is a simple example:

  • select text
  • use mouse to click this check box.
  • or use the mouse to click the "Cut" button

It doesn't matter that the text field currently has no focus when calling Action. TextAction tracks the last text component with 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(); } } } 
+5
source

Just as suggested by @lesmana (+1 for this). Here you have an example that shows that on focusLost, the focus listener returns a previously focused component.

 import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Focusing { public static void main(String[] args) { JPanel p = new JPanel(); JTextField tf1 = new JTextField(6); tf1.setName("tf1"); p.add(tf1); JTextField tf2 = new JTextField(6); tf2.setName("tf2"); p.add(tf2); FocusListener fl = new FocusListener() { @Override public void focusGained(FocusEvent e) { System.out.println("focusGained e.getSource().c=" + ((JComponent) e.getSource()).getName()); } @Override public void focusLost(FocusEvent e) { System.out.println("focusLost e.getSource().c=" + ((JComponent) e.getSource()).getName()); } }; tf1.addFocusListener(fl); tf2.addFocusListener(fl); JPanel contentPane = new JPanel(); contentPane.add(p); JFrame f = new JFrame(); f.setContentPane(contentPane); f.setSize(800, 600); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } 

All the best, Borough.

+1
source

I never did this directly, but you could take a peek at FocusEvents and the Focus Subsystem .

I hope there is something in the Focus subsystem that could trigger events that you could listen to.

0
source

You can register a FocusListener for each text component. The FocusEvent object has a link to the last component with focus.

0
source

Source: https://habr.com/ru/post/1348160/


All Articles