2

Is there any limitations in JComboBox? Iam trying to populate JComboBox(), Not all values are getting populated.

JComboBox combo = new JComboBox();
String[] lines = edit.getText().split("\n");

for(String line : lines){
   String subLine = line.substring(0, 15);
   combo.addItem(subLine);
}
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • 1
    Please post an [SSCCE](http://www.sscce.org) to illustrate your problem. – Code-Apprentice Sep 25 '12 at 20:23
  • 1
    As practical limits are typically more restrictive than implementation limits, please indicate your actual, constructive requirements. – trashgod Sep 25 '12 at 20:38
  • @trashgod thanks for responding. The lines can be Max 5000-10000, from all these lines the first 15 characters should be displayed in JComboBox – FirmView Sep 25 '12 at 20:44
  • 1
    @FirmView if I remember correctly this question was asked last year (uo to 1mio), no issue with number of Item on todays PC, sure without using the Renderer, issue must be somewhere in your code, follows suggestion by Code-Guru – mKorbel Sep 25 '12 at 20:47

2 Answers2

2

no issue with number of Items, but start_up is very lazy

import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalComboBoxButton;

public class MyComboBox {

    private Vector<String> listSomeString = new Vector<String>();
    private JComboBox someComboBox = new JComboBox(listSomeString);
    private JComboBox editableComboBox = new JComboBox(listSomeString);
    private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
    private JFrame frame;

    public MyComboBox() {
        for (int i = 0; i < 100000; i++) {
            listSomeString.add("-");
            listSomeString.add("Snowboarding");
            listSomeString.add("Rowing");
            listSomeString.add("Knitting");
            listSomeString.add("Speed reading");
        }
//
        someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        someComboBox.setEditable(true);
        someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
        ((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
//
        editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        editableComboBox.setEditable(true);
        JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
        text.setBackground(Color.YELLOW);
        JComboBox coloredArrowsCombo = editableComboBox;
        Component[] comp = coloredArrowsCombo.getComponents();
        for (int i = 0; i < comp.length; i++) {// hack valid only for Metal L&F
            if (comp[i] instanceof MetalComboBoxButton) {
                MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
                coloredArrowsButton.setBackground(null);
                break;
            }
        }
//
        non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
//
        frame = new JFrame();
        frame.setLayout(new GridLayout(0, 1, 10, 10));
        frame.add(someComboBox);
        frame.add(editableComboBox);
        frame.add(non_EditableComboBox);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);

        System.out.println(listSomeString.size());
        System.out.println(someComboBox.getItemCount());
        System.out.println(editableComboBox.getItemCount());
        System.out.println(non_EditableComboBox.getItemCount());
    }

    public static void main(String[] args) {
        UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
        UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
        UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
        UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyComboBox aCTF = new MyComboBox();
            }
        });
    }
}

output could be

run:
500000
500000
500000
500000
BUILD SUCCESSFUL (total time: 4 seconds)
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    @FirmView you can to use [AutoComplete too](http://stackoverflow.com/questions/7255636/is-it-possible-to-have-an-autocomplete-using-jtextfield-and-a-jlist/7255918#7255918), notice maybe there is one important issue, one issue, ---> JComboBox is based on premature arrays and doesn't works if there are duplicates Items, remove duplicates, or create own model, not standard models implemented in the API – mKorbel Sep 25 '12 at 21:18
2

If the problem space is amenable, also consider two (or more) combo-tiers to minimize scrolling, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045