简体   繁体   English

在组合框中创建选项作为可用串行端口

[英]Creating Options in Combo Boxes as Available Serial Ports

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. 我想我需要使用javax.swing.getModel,但我不确定如何做到这一点。

  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 有两个问题......由于缺乏背景,很难知道哪一个是正确的

Possibility #1 可能性#1

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);

Possibility #2 可能性#2

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... 在这种情况下,您应该...但是没有足够的上下文来描述如何使用您的代码实现这一目标......

Possibility #3 可能性#3

I have no idea what you're talking about... 我不知道你在谈论什么......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM