0

I was trying to modify the data type of a combo box named cbLicense in Swing. In this case, I wanna change from String to CustomerItem which is a class. I use Swing design, so I selected the combo box, went to Properties/Code/TypeParameters. But the problem is that on the Generated Code section, there's still a line:

cbLicense.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

This line has an incompatible type error. So how do I fix it? Do I need to add each CustomerItem object into cbLicense via Design view? Because the CustomerItem value is retrieved from the database.

Find the way to customize combo box in Swing

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Lucia
  • 51
  • 1
  • Is "Swing design" the name of the sofware you're using to design Swing forms? – Maurice Perry May 23 '23 at 05:36
  • [This post](https://stackoverflow.com/a/22652008/4725875) may assist you. – DevilsHnd - 退職した May 23 '23 at 05:39
  • @MauricePerry No. I use Swing design in Netbeans 8.2. – Lucia May 23 '23 at 05:40
  • @DevilsHnd Thank you. But I use the design option in Netbeans, which is drag and drop. So some code is auto-generated and cannot be modified. – Lucia May 23 '23 at 05:42
  • @Lucia it can be modified: https://stackoverflow.com/a/43892689/7036419 – Maurice Perry May 23 '23 at 05:54
  • @MauricePerry Yes, I've already followed that link. But when I drag a new combo box, the line `cbLicense.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));` is created automatically. Even I changed data type after that, that line is still there. – Lucia May 23 '23 at 06:01
  • Use `Custom creation code` in the `Code` tab of the combobox properties – Maurice Perry May 23 '23 at 06:03
  • @Lucia or right-click on the combobox, and select "Customize code..." – Maurice Perry May 23 '23 at 06:08
  • @MauricePerry I have declared CustomerItem outside of the generation code section. Now I added those code into Custom creation code: `cbLicense = CustomerItem[] items = new CustomerItem[3]; items[0] = new CustomerItem("oh", false); items[1] = new CustomerItem("My", false); items[2] = new CustomerItem("God", false); DefaultComboBoxModel model = new DefaultComboBoxModel<>(items); cbLicense.setModel(model); cbLicense.setRenderer(new RenderCheckComboBox());` – Lucia May 23 '23 at 06:11
  • @MauricePerry I do apologise if it's hard to read my comment. I dont know to format code on comment. But an error has occured on the line: `cbLicense = CustomerItem[] items = new CustomerItem[3];`. And with this way, can I retrieve value from the database into combo box? – Lucia May 23 '23 at 06:14
  • Try `new DefaultComboBoxModel<>(new CustomerItem[]{ new CustomerItem("oh", false), new CustomerItem("My", false), new CustomerItem("God", false) })` – Maurice Perry May 23 '23 at 06:16
  • @MauricePerry It automatically added `cbLicense =` before your code. `cbLicense = DefaultComboBoxModel model = new DefaultComboBoxModel<>(new CustomerItem[]{ new CustomerItem("oh", false), new CustomerItem("My", false), new CustomerItem("God", false) }); cbLicense.setModel(model); cbLicense.setRenderer(new RenderCheckComboBox());` – Lucia May 23 '23 at 06:22
  • @MauricePerry Oh, this is `Custom creation Code` so it will automatically add `cbLicense =`. But I wanna make changes on `cbLicense.setModel`. – Lucia May 23 '23 at 06:26
  • @Lucia two possible solutions: 1) right-click on the combobox and select "Customize code...", 2) `new JComboBox<>(new DefaultComboBoxModel<>(new CustomerItem[]{ new CustomerItem("oh", false), new CustomerItem("My", false), new CustomerItem("God", false) }))` – Maurice Perry May 23 '23 at 06:37

1 Answers1

0

Right-click on the combobox, and select "Customize code...":

enter image description here

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24