I am currently making a java chatroom using Java Sockets. I have the basic UI working with properly functioning text, but I am attempting to add elements to the text (bold font, italics, etc.) and cannot seem to get it working. I have attached the code that effects the text, if anyone could help me out it would be greatly appreciated. The main issue is that getDocument() is underlined in red, saying that it is undefined for the type JPanel.
public void addText(String text) {
JPanel content = chatArea;
// Get the document of the existing JEditorPane
Document doc = content.getDocument();
// Process text for bold markup
String processedText = processBoldMarkup(text);
// Create a new JEditorPane
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditable(false);
editorPane.setText(processedText);
// Insert the editor pane into the document
try {
doc.insertString(doc.getLength(), "n", null); // Ensure the text is added on a new line
content.add(editorPane);
} catch (BadLocationException e) {
e.printStackTrace();
}
// Scroll down on new message
JScrollBar vertical = ((JScrollPane) chatArea.getParent().getParent()).getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
}
private String processBoldMarkup(String text) {
// Replace "&b" with HTML bold tags
return text.replaceAll("&b\s*", "<b>");
}