Java Utililty Methods JComboBox

List of utility methods to do JComboBox

Description

The list of methods to do JComboBox are organized into topic(s).

Method

StringgetEOL(JComboBox eolCombo)
get EOL
switch (eolCombo.getSelectedIndex()) {
case 0:
    return "\n";
case 1:
    return "\r";
case 2:
    return "\r\n";
return "\n";
StringgetJComboBoxString(JComboBox comboBox)
get J Combo Box String
String result = "";
Object obj = comboBox.getSelectedItem();
if (obj != null) {
    result = obj.toString();
} else {
    JTextField jtf = getJComboBoxJTextField(comboBox);
    if (jtf != null) {
        result = jtf.getText();
...
intgetMaxWidth(final JComboBox combo, final FontMetrics fm)
get Max Width
int maxLen = 0;
for (int i = 0; i < combo.getItemCount(); i++) {
    String str = combo.getItemAt(i);
    int wi = fm.stringWidth(str);
    if (wi > maxLen) {
        maxLen = wi;
return maxLen + 20; 
JComboBoxgetOnlyComboBox(Container owner)
get Only Combo Box
return getOnlyComboBox(owner, false);
JComboBoxgetPrinterJComboBox()
get Printer J Combo Box
JComboBox<String> box = new JComboBox<>();
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printService : services) {
    box.addItem(printService.getName());
box.setSelectedItem(getDefaultPrinterName());
return box;
intgetScrollBarWidth(JComboBox comboBox, JScrollPane scrollPane)
get Scroll Bar Width
int scrollBarWidth = 0;
if (comboBox.getItemCount() > comboBox.getMaximumRowCount()) {
    JScrollBar vertical = scrollPane.getVerticalScrollBar();
    scrollBarWidth = vertical.getPreferredSize().width;
return scrollBarWidth;
voidinitCombo(JComboBox combo)
Fail safe combo init
combo.setSelectedIndex((combo.getItemCount() == 0) ? -1 : 0);
return;
voidinputComboBox(JComboBox cbx, String[] str)
input Combo Box
for (int i = 0; i < str.length; i++) {
    cbx.addItem(str[i]);
booleanisCbEquals(JComboBox cb, String str)
is Cb Equals
if (cb.getSelectedIndex() == -1) {
    if (str.equals("")) {
        return (true);
} else {
    if (str.equals("")) {
        return (false);
    if (cb.getSelectedItem().equals(str)) {
        return (true);
return (false);
booleanisComboBoxButton(AbstractButton button)
Checks and answers if the specified button is in a combo box.
Container parent = button.getParent();
return (parent != null) && ((parent instanceof JComboBox) || (parent.getParent() instanceof JComboBox));