public void refreshSerialPortList() { String[] serialPortList = SerialPortList.getPortNames(); portMenu.removeAll(); portMenu.add(refreshItem); JMenuItem menuItem; for (String serialPort : serialPortList) { menuItem = new JMenuItem(serialPort); menuItem.addActionListener(e -> onSerialPortClicked(e.getActionCommand())); portMenu.add(menuItem); } }
private void initializeUI() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle(Constants.WINDOW_TITLE); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { closePort(); } }); JMenuBar menuBar = new JMenuBar(); portMenu = new JMenu(Constants.WINDOW_PORT); menuBar.add(portMenu); refreshItem = new JMenuItem(Constants.WINDOW_REFRESH_PORTS); refreshItem.addActionListener(e -> refreshSerialPortList()); portMenu.add(refreshItem); setJMenuBar(menuBar); Container mainPane = getContentPane(); mainPane.setLayout(new BorderLayout()); textArea = new JTextArea(); textArea.setRows(16); textArea.setColumns(40); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea); mainPane.add(scrollPane, BorderLayout.CENTER); JPanel lowerPane = new JPanel(); lowerPane.setLayout(new BoxLayout(lowerPane, BoxLayout.X_AXIS)); lowerPane.setBorder(new EmptyBorder(4, 4, 4, 4)); JButton transferFileButton = new JButton(Constants.WINDOW_TRANSFER_FILE); transferFileButton.addActionListener(e -> onTransferFileClicked()); lowerPane.add(transferFileButton); lowerPane.add(Box.createRigidArea(new Dimension(4, 0))); textField = new JTextField(40); textField.addKeyListener(this); JButton sendButton = new JButton(Constants.WINDOW_SEND); JButton clearButton = new JButton(Constants.WINDOW_CLEAN); clearButton.addActionListener(e -> textArea.setText("")); sendButton.addActionListener(e -> onSendButtonClicked()); lowerPane.add(textField); lowerPane.add(Box.createRigidArea(new Dimension(4, 0))); lowerPane.add(sendButton); lowerPane.add(Box.createRigidArea(new Dimension(4, 0))); lowerPane.add(clearButton); mainPane.add(lowerPane, BorderLayout.SOUTH); pack(); refreshSerialPortList(); }