I am trying to make a simple chat program with the username in a certain font styles while the chat text in default. However, the results is both the username and the chat text have the same style... What did I go wrong?...
I have also tried to look at the official tutorial at this link but i doesn't seem to help much...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (jTextArea2.getText().length() != 0) { user_ = SimpleChatLogin.getUser(); appendUserText(user_); Style st = jTextPane1.addStyle("Black", null); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, true); appendChatText(jTextArea2.getText()); jTextArea2.setText(""); } } public void appendUserText(String text) { try { StyledDocument chatName = jTextPane1.getStyledDocument(); // Setting the font style Style style = jTextPane1.addStyle("Blue", null); StyleConstants.setForeground(style, Color.blue); StyleConstants.setBold(style, true); // Append to document chatName.insertString(chatName.getLength(), SimpleChatLogin.getUser() + " ", style); } catch (BadLocationException e) { System.out.println("Failed to append text: " + e); } } // Appends chat text to the document and ensure that it is visible public void appendChatText(String text) { try { Document chatText = jTextArea2.getDocument(); // Move the insertion point to the end jTextArea2.setCaretPosition(chatText.getLength()); // Insert the text jTextPane1.replaceSelection(text + "\n"); // Convert the new end location // to view co-ordinates Rectangle r = jTextArea2.modelToView(chatText.getLength()); // Finally, scroll so that the new text is visible if (r != null) { jTextPane1.scrollRectToVisible(r); } } catch (BadLocationException e) { System.out.println("Failed to append text: " + e); } }