添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

For appeals, questions and feedback about Oracle Forums, please email [email protected] . Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Oracle has updated its online Terms of Use and Community Guidelines and introduced a Community Integrity Policy . These changes document Community specific rules and Oracle’s content moderation practices including use of automated tools, appeals process, and Oracle’s contact details. If you object to any changes, you may request that your account be closed by contacting [email protected] . Your continued use of Oracle Communities means that you are consenting to the updated terms.

focusGained() on JSpinner / JTextFields (select all text)

843804 Jun 10 2003 — edited Mar 18 2008

Trying to implement it so that the text in my JSpinner / JTextField's are selected when it gains focus I implemented this class (modified version of one I found in this forum):
public class CommonUtilityMethods {
    private static FocusAdapter allTextSelector;
    // Ensures that all the text in a JTextComponent will be
    // selected whenever the cursor is in that field (gains focus):
    public static void setSelectAllFieldText(JComponent comp) {
        if (allTextSelector == null) {
            // Lazy initialization of one adapter for all components:
            allTextSelector = new java.awt.event.FocusAdapter() {
                public void focusGained(FocusEvent ev) {
                    if(ev.getSource() instanceof JTextComponent) {
                        JTextComponent textComp = (JTextComponent) ev.getSource();
                        textComp.selectAll();
                    } else if(ev.getSource() instanceof valueSpinner) {
                        valueSpinner vcomp = (valueSpinner)ev.getSource();
                        ((JSpinner.DefaultEditor )vcomp.getEditor ()).getTextField().selectAll();
        comp.addFocusListener(allTextSelector);
}
And then I add the listener using the setSelectAllFieldText method.
It does however not work as expected. On JTextField's a selection is only made the first time the textfield gains focus (so I get it to select all text if I remove the focus from the JInternalFrame which it is located in and then give it focus again).
On JSpinner no selection is made at all..