Reputation: 9121
Take a look at the following image:
What component is used to make the "chat messages" display like on the picture? Is it just a simple JTextPane with html? What would the best approach here be to create something similar? I would love an approach where it is easy to target a message and remove/edit it after it has been printed.
Upvotes: 1
Views: 1775
Reputation: 285405
I would use a JList for this. Your renderer could allow for the display of multiple lines as your GUI shows, and a JList has improved efficiencies over say a JPanel with lots of JLabels since it doesn't display actual components but rather the rendering of a component. A JTextPane could work as well, but I don't think is necessary since the text in the display should not be directly editable. Rather the JTextField or JTextArea at the bottom is where the editing and input should occur. Another option is to use a JTable with a single column -- same idea as above, but allows for editing of cells as needed.
Edit
You state:
But how would i create the Name - Date (newline) message part in a JList? Would that be 1 list item?
No, the cell renderer would use a component that allowed for multiple line display, perhaps a JTextPane or a JPanel that holds two JLabels.
Edit 2
Ok so each item in the JList would be a JPanel?
No, not at all. The cell renderer could be a JPanel, but each item of the JList would be an object of a class created for the purpose, that has fields for the Chatter, Date, and String (text).
Edit 3
Understand that the JList's model doesn't hold GUI components but rather the logical information that is displayed by the JList.
For instance, you could create a class,
public class ChatEntry {
private Chatter chatter; // holds the chatter's name, and any other relevant info
Date date; // time of chat entry
String text; // the text entered
//..... getters setters and constructor
}
Then your JList would be a JList<ChatEntry>
but more importantly the list's model would hold ChatEntry items.
Then you'd create a cell renderer that translates the ChatEntry information into a displayable/renderable component.
Upvotes: 4