public void transmitMessage(String message) { if (serialPort == null) { showErrorMessage("Por favor seleccione un puerto"); return; } if (message.isEmpty()) { showErrorMessage("No se envian mensajes vacios"); return; } String text = message + '\n'; showSentMessage(text); try { serialPort.writeBytes(text.getBytes()); } catch (SerialPortException e) { showErrorMessage(e.getMessage()); e.printStackTrace(); } textField.setText(""); }
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(); }
private void onSendButtonClicked() { if (!transferingFile) transmitMessage(textField.getText()); }