1

I have x ports in my computer. I woud like to add to menu any device is plug into and recive user input for listener. I wrote:

private void portSelectItem(){

    JMenu port=new JMenu("Port");

    String[] portTab= SerialIO.listPorts();

    for(String s : portTab){                    

        port.add(new JCheckBoxMenuItem(s));

    }

}

With way I generate dynamical x references and add listener in this ports?

user902691
  • 1,149
  • 4
  • 20
  • 46

2 Answers2

2

1) setActionCommand(String portTab)

2) you can add to JCheckBoxMenuItem

3) each of Listeners returns Object fired select/deselect event, and this Object returns getActionCommand(String portTab) if is defined

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Check Swing menus tutorial. Here is an example:

ButtonGroup buttonGroup = new ButtonGroup();
for(String s : portTab){                    
    JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem();  
    checkBoxMenuItem.setText(s);
    buttonGroup.add(checkBoxMenuItem);
    checkBoxMenuItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //TODO 
        }
    });         
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107