开发者

Setting caret position in editable JComboBox

When an item is selected from a combobox that is longer than the combobox is wide, the end of the string is truncated to show only the beginning portion of the string that will fit. When the combobox is set to editable, it is the end of the string which is shown, with the begining truncated (which makes sense, as it is as if the user had typed it)

Is there a way to set the caret position to the start of the entry to display the beginning of the value, whilst still allowing the combobox to be editable? Is this something to be achieved by writing a custom editor for the c开发者_JAVA技巧ombobox?


This is one way:

JComboBox comboBox = new JComboBox( ... )
{
    @Override
    public void setSelectedItem(Object item)
    {
        super.setSelectedItem( item );
        ComboBoxEditor editor = getEditor();
        JTextField textField = (JTextField)editor.getEditorComponent();
        textField.setCaretPosition(0);
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜