elementList = new JList<>();
+ elementList.setFocusTraversalKeysEnabled(false);
+
+ elementList.setSelectionForeground(new Color(255, 255, 255));
+ elementList.setSelectionBackground(new Color(102, 0, 153));
+ elementList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
+ elementList.setForeground(new Color(255, 255, 255));
+ elementList.setBackground(new Color(51, 51, 51));
+
+ elementList.setModel(listModel);
+ elementList.setFont(new Font("Arial", Font.PLAIN, 17));
+ elementList.setFixedCellHeight(60);
+ elementList.setBorder(new EmptyBorder(5, 5, 5, 5));
+
+ JScrollPane scrollPane = new JScrollPane();
+ scrollPane.setForeground(new Color(0, 0, 0));
+ scrollPane.setBackground(new Color(51, 51, 51));
+ scrollPane.setViewportView(elementList);
+ scrollPane.setBorder(null);
+
+ GridBagConstraints gbc_scrollPane = new GridBagConstraints();
+ gbc_scrollPane.fill = GridBagConstraints.BOTH;
+ gbc_scrollPane.gridwidth = 2;
+ gbc_scrollPane.gridx = 1;
+ gbc_scrollPane.gridy = 1;
+
+ gbc_scrollPane.insets = new Insets(10, 10, 10, 10);
+
+ contentPane.add(scrollPane, gbc_scrollPane);
+
+ // Message enter field
+ JTextArea messageEnterTextfield = new JTextArea();
+ messageEnterTextfield.setCaretColor(new Color(255, 255, 255));
+ messageEnterTextfield.setForeground(new Color(255, 255, 255));
+ messageEnterTextfield.setBackground(new Color(51, 51, 51));
+ messageEnterTextfield.setLineWrap(true);
+ messageEnterTextfield.setBorder(null);
+ messageEnterTextfield.setFont(new Font("Arial", Font.PLAIN, 17));
+ messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
+
+ GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints();
+ gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH;
+ gbc_moveSelectionMessageEnterTextfield.gridx = 1;
+ gbc_moveSelectionMessageEnterTextfield.gridy = 2;
+
+ gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10);
+
+ contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
+
+ // Post Button
+ JButton postButton = new JButton("Post");
+ postButton.setForeground(new Color(255, 255, 255));
+ postButton.setBackground(new Color(102, 51, 153));
+ postButton.setBorderPainted(false);
+
+ GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
+
+ gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
+ gbc_moveSelectionPostButton.gridx = 2;
+ gbc_moveSelectionPostButton.gridy = 2;
+
+ gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
+
+ contentPane.add(postButton, gbc_moveSelectionPostButton);
+
+ postButton.addActionListener((evt) -> {
+ if (!messageEnterTextfield.getText().isEmpty()) try {
+ final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText());
+ envoyClient.sendMessage(message);
+ appendMessageToChat(message);
+ messageEnterTextfield.setText("");
+ } catch (Exception e) {
+ JOptionPane.showMessageDialog(this,
+ "An exception occured while sending a message. See the log for more details.",
+ "Exception occured",
+ JOptionPane.ERROR_MESSAGE);
+ e.printStackTrace();
+ }
+ });
+ }
+
+ /**
+ * Extracts the first text content from a message.
+ *
+ * @param message The message from which to return the text content
+ * @return The first content of type 'text'
+ */
+ public String getFirstTextContent(Message message) {
+ return message.getContent().get(0).getText();
+ }
+
+ /**
+ * Appends a message with sender and message content to the message list.
+ *
+ * @param message The message to append
+ */
+ private void appendMessageToChat(Message message) {
+ listModel.addElement("" + " " + message.getMetaData().getSender()
+ + " " + "
" + "
" + getFirstTextContent(message)
+ + "");
+ }
+
+ public static void main(String[] args) {
+ EventQueue.invokeLater(() -> {
+ try {
+ ChatWindow frame = new ChatWindow();
+ frame.setVisible(true);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ });
+ }
+}
\ No newline at end of file