Adding items to a jComboBox
What is the preferred way of adding new items to a jComboBox? I am learning to use Swing and the NetBeans IDE. When coding Swing from scratch, you can add new items in the jComboBox's constructor. However, when I let NetBeans create the code for me, this is in a guarded block and I cannot edit it. By default { "Item 1", "Item 2", "Item 3", "Item 4" } are added to the combobox. The workaround I'm using is to removeAllItems() when the window is first activated. Then add my own items to the list.
Code:
private void formWindowActivated(java.awt.event.WindowEvent evt) {
jComboBox1.removeAllItems();
jComboBox1.addItem("red");
jComboBox1.addItem("blue");
jComboBox1.addItem("green");
jComboBox1.addItem("yellow");
}
Is there a better way of accomplishing this?
Thanks
--Donovan