So, I'm confused as to how to change the options in a combo box based off of the available serial ports. Could anyone please help me figure this out? I think I need to use javax.swing.getModel, but I'm unsure as to how to do that.
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
ArrayList<String> serialports = new ArrayList<String>();
serialports.add(portId.getName());
String[] ports = new String[serialports.size()];
ports = serialports.toArray(ports);
GUI.jComboBox2 = new JComboBox(ports);
GUI.jComboBox2.addActionListener(GUI.jComboBox2);
wantedPortName = (String) GUI.jComboBox2.getSelectedItem();
There are two issues...because of the lack of context, it's difficult to know which one is correct
You've previously created the combo box and added it to the screen....
If this is the case, then your code has just de-referenced it. Meaning that the control that is on the screen is no longer the one you are interacting with.
In this case, you should update the model only...
ArrayList<String> serialports = new ArrayList<String>();
serialports.add(portId.getName());
String[] ports = new String[serialports.size()];
ports = serialports.toArray(ports);
ComboBoxModel<String> model = new DefaultComboBoxModel<>(ports);
GUI.jComboBox2.setModel(model);
You've never added the combo box to the screen before...
In which case, you should...but there's not enough context to describe how you would achieve that with your code...
I have no idea what you're talking about...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.