I have tried a few different ways to to highlight the text in a jcombobox when it receives focus but none have work. Any have a code example that could help?

Some examples I have tried:

private void fromComboBoxFocusFocusGained(FocusEvent event) {
  JTextField field = (JTextField)fromComboBox.getEditor().getEditorComponent();
  field.selectAll();
        
}
private void fromComboBoxFocusFocusGained(FocusEvent event) {
  fromComboBox.getEditor().selectAll();
}

All you need to is monitor mouse clicks in/over the component and mark starting caret and ending caret. Magic done

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

public class JTextFieldDemo {

  JLabel jlabAll;
  JLabel jlabSelected;

  JTextField jtf;

  JButton jbtnCut;
  JButton jbtnPaste;

  public JTextFieldDemo(){
    JFrame jfrm = new JFrame("Use JTextField");

    jfrm.getContentPane().setLayout(new FlowLayout());
    jfrm.setSize(200, 150);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jlabAll = new JLabel("All text: ");
    jlabSelected = new JLabel("Selected text: ");

    jtf = new JTextField("This is a test.", 15);
    jtf.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent le){
         jlabAll.setText("All text: " + jtf.getText());
         jlabSelected.setText("Selected text: "+ jtf.getSelectedText());
       }
    });

    jbtnCut = new JButton("Cut");
    jbtnPaste = new JButton("Paste");

    jbtnCut.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent le){
        jtf.cut();
        jlabAll.setText("All text: "+ jtf.getText());
        jlabSelected.setText("Selected text: "+ jtf.getSelectedText());
      }
    });

    jbtnPaste.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent le){
        jtf.paste();
      }
    });

    jtf.addCaretListener(new CaretListener(){
      public void caretUpdate(CaretEvent ce){
        jlabAll.setText("All text: " + jtf.getText());
        jlabSelected.setText("Selected text: "+ jtf.getSelectedText());
      }
    });

    jfrm.getContentPane().add(jtf);
    jfrm.getContentPane().add(jbtnCut);
    jfrm.getContentPane().add(jbtnPaste);
    jfrm.getContentPane().add(jlabAll);
    jfrm.getContentPane().add(jlabSelected);

    jtf.setCaretPosition(5);
    jtf.moveCaretPosition(7);

    jfrm.setVisible(true);
  }

  public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable(){
      public void run(){
        new JTextFieldDemo();
      }
    });
  }
}

Code as can be found in Swing - A Beginner's Guide by Herbert Schildt pg.262-264

Ahhh sorry this one is wrong. Above example is for text field not combo box.(bad idea trying to reply at midnight when sleepy)
I will look into it again once I get home, sorry again

OK, please let me know what you find. Also, to let you know, the only input device we have for the application is a scan gun. There is no mouse or keyboard. So when focus is on the combobox i just need the text highlighted.

Thanks!

I figured out my problem. The below code does work, I just didn't have it in all the necessary places. Thanks for your help.

JTextField field = (JTextField)fromComboBox.getEditor().getEditorComponent();
field.selectAll();
This question has already been answered. Start a new discussion instead.