Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:25:24+00:00 2026-06-01T08:25:24+00:00

public class CursorAtStartFocusListener extends FocusAdapter { @Override public void focusGained(java.awt.event.FocusEvent evt) { Object source

  • 0
public class CursorAtStartFocusListener extends FocusAdapter {

@Override
public void focusGained(java.awt.event.FocusEvent evt) {
    Object source = evt.getSource();
    if (source instanceof JTextComponent) {
        JTextComponent comp = (JTextComponent) source;
        comp.setCaretPosition(0);
        comp.selectAll();
    } 
} }

jComboBox.getEditor().getEditorComponent().addFocusListener(new
CursorAtStartFocusListener());

As you see from code above I want to select all text in editable JComboBox and set cursor position to the start.
But I have problem if I first write comp.setCaretPosition(0) then comp.selectAll(), the text is selected but cursor is on the end of text, otherwise if I first write comp.selectAll() then comp.setCaretPosition(0), I get cursor in position that I want but text isn’t selected.
Have any idea how can I do this thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:25:25+00:00Added an answer on June 1, 2026 at 8:25 am

    Caret and selectAll is better to wrapping into invokeLater, but for JTextComponent (you can derive that from JComboBox too) you have to decide if you want to use

    • select all text == selectAll()

    or use

    • select Caret (from Document)

    EDIT

    1) for editable JComboBox

    Runnable doRun = new Runnable() {
    
        @Override
        public void run() {
            myComboBox.getEditor().setItem(0);
            myComboBox.getEditor().selectAll();
            myComboBox.requestFocus();
        }
    };
    SwingUtilities.invokeLater(doRun);
    

    2) or derive JTextField or JFormattedTextField from editable JComboBox

    ((JTextField) myComboBox.getEditor().getEditorComponent())
    

    then add FocusListener e.g.

        private FocusListener focsListener = new FocusListener() {
    
            @Override
            public void focusGained(FocusEvent e) {
                dumpInfo(e);
            }
    
            @Override
            public void focusLost(FocusEvent e) {
                //dumpInfo(e);
            }
    
            private void dumpInfo(FocusEvent e) {
                //System.out.println("Source  : " + name(e.getComponent()));
                //System.out.println("Opposite : " + name(e.getOppositeComponent()));
                //System.out.println("Temporary: " + e.isTemporary());
                final Component c = e.getComponent();
                if (c instanceof JFormattedTextField) {
                    EventQueue.invokeLater(new Runnable() {
    
                        public void run() {
                            ((JFormattedTextField) c).requestFocus();
                            ((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
                            ((JFormattedTextField) c).selectAll();
                        }
                    });
                } else if (c instanceof JTextField) {
                    EventQueue.invokeLater(new Runnable() {
    
                        public void run() {
                            ((JTextField) c).requestFocus();
                            ((JTextField) c).setText(((JTextField) c).getText());
                            ((JTextField) c).selectAll();
                        }
                    });
                }
            }
    
            private String name(Component c) {
                return (c == null) ? null : c.getName();
            }
        };
    

    EDIT 2 :

    SSCCE for editable JComboBox,

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.*;
    
    public class ComboRendererTest {
    
        public ComboRendererTest() {
            JComboBox comboBox = new JComboBox();
            comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX");
            comboBox.addItem(new Double(1));
            comboBox.addItem(new Double(2.25));
            comboBox.addItem(new Double(3.5));
            comboBox.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer()));
            comboBox.setEditable(true);
    
            JComboBox comboBox1 = new JComboBox();
            comboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX");
            comboBox1.addItem(new Double(1));
            comboBox1.addItem(new Double(2.25));
            comboBox1.addItem(new Double(3.5));
            comboBox1.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer()));
            comboBox1.setEditable(true);
    
            JFrame frame = new JFrame();
            frame.add(comboBox, BorderLayout.NORTH);
            frame.add(comboBox1, BorderLayout.SOUTH);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) throws Exception {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    ComboRendererTest comboRendererTest = new ComboRendererTest();
                }
            });
        }
    }
    
    class TwoDecimalRenderer extends DefaultListCellRenderer {
    
        private ListCellRenderer defaultRenderer;
    
        public TwoDecimalRenderer(ListCellRenderer defaultRenderer) {
            this.defaultRenderer = defaultRenderer;
        }
    
        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            Component c = defaultRenderer.getListCellRendererComponent(
                    list, value, index, isSelected, cellHasFocus);
            if (c instanceof JLabel) {
                c.setBackground(Color.red);
            } else {
                c.setBackground(Color.red);
                c = super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
            }
            return c;
        }
    }
    

    EDIT 3.

    dirty hack could be

    JTextComponent editor = ((JTextField) myComboBox.getEditor().getEditorComponent());
    editor.setCaretPosition(getLength());
    editor.moveCaretPosition(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*
public class Browser1Activity extends Activity { TextView url; WebView ourBrow; @Override protected void onCreate(Bundle
public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class HomeActivity extends Activity{ // public ArrayList<User> users1 = new ArrayList<User>(); @Override public
public class Boards : TabActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Tab);
public class a { public event eventhandler test; public void RaiseTest(){//fire test} } Is
public class PackageTabActivity extends ListActivity{ HashMap<String,Object> hm ; ArrayList<HashMap<String,Object>> applistwithicon ; private static final
public class Offer_Popup extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.offer_popup); //newly
public class CustomEditor : Editor { protected override void Render(HtmlTextWriter writer) { Toolbar topToolbar
public class TextBoxDerived : System.Web.UI.WebControls.TextBox { protected override void OnLoad(EventArgs e) { this.Controls.Add(new LiteralControl(Hello));

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.