Groovy JComboBox Does Not Show New Data

Ok, this really stumped me for several days.  If you copy this code into a groovyConsole session started from your command terminal, you’ll see something nice.

For a long time i could not get a jcombobox to include and show new items (strings in this example) despite best atttempts. I first thought it was a repainting or layout validation issue. Then i thought i’d try a list of strings. Since we’re using the groovy console, i didn’t put a ‘def’ before the list=[]  but if you use this within a class you’ll need to declare it possibly as a ‘def’.

Screen shot 2013-11-25 at 12.27.52 AMMistakenly thought i had to add to my ‘list’ variable since the combo box had it’s ‘items:’ attached to my list. But no, just adding more items to the list did not automatically make them appear in the combo, despite any other repaint() or revalidate() attempts for a panel refresh. I played with models too but no joy.

Eventually found the combobox addItem method ( ‘cb.addItem( “yyy” );’ in my example )  did the trick and this will successfully add new string items to a combo box. We might also need ‘gui.repaint();’ as it’s necessary to repaint the gui to show new values (at least in my system).

Just to keep your internal list in sync with the combobox, you should also add the new item into your list as well as the combobox. My sample below is adding ‘xxx’s to the list but ‘yyy’s to the combobox – that’s just for this demo. It really should have added ‘yyy’s to both list and ‘cb’ combobox. Good luck !

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import java.awt.FlowLayout
import java.awt.*
import java.awt.BorderLayout
import javax.swing.*
import javax.swing.JComboBox;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

swing = new SwingBuilder()
def flag = true


def cb = swing.comboBox(id: 'cb', items:list, maximumSize:[200,40] )
cb.addItemListener(new ItemListener()
  public void itemStateChanged(ItemEvent e)
    System.out.println(e.getItem() + " and state change:" + e.getStateChange() );

gui = swing.frame(id:'gui',title:'JComboBox', layout:new BorderLayout(),pack:false, show:true,
   minimumSize:[300,100], defaultCloseOperation:WC.DISPOSE_ON_CLOSE)
    label "JComboBox : "
    button(id: "button", text: "Press",
    actionPerformed :
      gui.background = (flag) ? java.awt.Color.RED : java.awt.Color.WHITE
      flag = !flag
      list += "xxx" // mistakenly thought i had to add to my 'list' variable
      cb.addItem( "yyy" );  // this successfully adds new string items to my combo box
gui.repaint(); // necessary to repaint the gui to show new values
    }) // end of button
  } // end of hbox
} // end of gui

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s