Highlight ComboBox

Hi everybody,

i want to set editable a combobox and set selected(highligted) the first index when i pressed F2. The code i write below;

comboBox.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_F2){

//?

comboBox.setEditable(true);

}

}

});

how can i set highlighted ?

[387 byte] By [asilazmaza] at [2007-11-27 11:54:08]
# 1

next time use code tag.

comboBox.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_F2){

comboBox.setSelectedIndex(0); // select the first index

comboBox.setEditable(true);

}

}

});

Yannixa at 2007-7-29 18:54:13 > top of Java-index,Desktop,Core GUI APIs...
# 2

Add a renderer to the combobox (list renderer), and implement the getListCellRendererComponent() method of the interface. Inside that, setBackground(Color) and setForeground(Color) as required based on conditions.

Attach this renderer to the combo box. The renderer should look like this:

class MyCellRenderer extends JLabel implements ListCellRenderer {

public MyCellRenderer() {

setOpaque(true);

}

public Component getListCellRendererComponent(

JList list,

Object value,

int index,

boolean isSelected,

boolean cellHasFocus)

{

setText(value.toString());

setBackground(isSelected ? Color.red : Color.white);

setForeground(isSelected ? Color.white : Color.black);

return this;

}

}

JavaStarter2897a at 2007-7-29 18:54:13 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks every body, this solution is another alternative,

JTextComponent editor = (JTextComponent) comboBox2.getEditor().getEditorComponent();

editor.setCaretPosition(((String)comboBox2.getSelectedItem()).length());

editor.moveCaretPosition(0);

asilazmaza at 2007-7-29 18:54:13 > top of Java-index,Desktop,Core GUI APIs...