-1

I have a weird problem with my combo box... (I use Netbeans 7.1 and Java code) when I add an item cbxUnidad.addItem("TODOS"); it takes 1300 ms to add it, so when I open that form (on execution time) it takes more than 4 seconds to open it. I know that that specific code takes that long because i have used this to know how much time takes that line:



long empieza  = 0;
long duracion = 0;
empieza  = System.currentTimeMillis();
cbxUnidad.addItem("TODOS");
duracion = (System.currentTimeMillis() - empieza);
System.out.pr

intln("Adding the item TODOS last " + duracion + " ms");

So in that way i get 1.3 seconds (1300 ms) when java execute that sentence... that's kinda weird and very slow... so i wanna know if anyone knows why or how to change the sentence.

Here i Leave the initCode for the combo-box:



cbxUnidad = new javax.swing.JComboBox();
cbxUnidad.setMaximumRowCount(25);
cbxUnidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
cbxUnidad.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        cbxUnidadItemStateChanged(evt);
        }
});

Thank you a lot :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2284257
  • 93
  • 1
  • 1
  • 5

1 Answers1

1

You'r probably doing some other operations unless adding only one item to a combo doesn't take too much time (actually it doesn't make sense adding one item taking 1.3 seconds).
My advice is :

  • Create another class having one combobox .
  • Add item(s) to the combobox and see how much time it takes.

    long empieza  = 0;
    long duracion = 0;
    empieza  = System.currentTimeMillis();
    cbxUnidad.addItem("TODOS");
    duracion = (System.currentTimeMillis() - empieza);
    java.text.NumberFormat nf = new java.text.DecimalFormat("#0.00000");
    String totalTime = nf.format(totalTime/1000d);
    System.out.println("Adding the item TODOS last " + totalTime+ " seconds");
    
Azad
  • 5,047
  • 20
  • 38
  • Yeah its weird, but that sentence takes 1.3 seconds... At the method I add that only item and then in a `for` sentence I add more items from a `list`, and that `for` takes about 2 ms, that's why I'm really confused because adding 1 element takes 1.3 sec and adding multiple items from a `for` takes 2 ms! that's really WEIRD. – user2284257 May 29 '13 at 17:18
  • I'm trying to add your code but I'm having some errors, it says that i cant make `java.text.NumberFormat nf = new java.text.NumberFormat("#0.00000");` because `NumberFormat` is abstract. – user2284257 May 29 '13 at 17:22
  • Ahh, Sorry about that it's `...NumberFormat = new java.text.DecimalFormat("#0.00000");` – Azad May 29 '13 at 17:26
  • thank you a lot, but, somehow using `cbxUnidad.addItem("TODOS");` lates 1300 ms and using `cbxUnidad.insertItemAt("TODOS", 0);` lates 2 ms... maybe it has to do something that my combo-box has a model? `cbxUnidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));` Do you know why? And don't worry, its ok that I add "TODOS" at the first positicion. – user2284257 May 29 '13 at 17:35