I have an Array List of ‘n’ number of different Chats That are Wrapped with a width below is the code for a TextArea Block
JTextArea chat2 = new JTextArea(); chat2.setText("this is chat 2"); chat2.setWrapStyleWord(true); chat2.setLineWrap(true); chat.setColumns(20); chat2.setEditable(false); chat2.setBorder(borderLine); chat2.setVisible(true);
And an Array List That looks Like
ArrayList<JTextArea> c = new ArrayList<JTextArea>(); c.add(chat2)
Already Tried
Vector itemsVector = new Vector(c); JList chats = new JList(itemsVector); //CHAT PANEL SCROLLABLE JScrollPane scrollChats = new JScrollPane(chats); scrollChats.setBounds(0,60,1100,600); main.add(scrollChats);
It didnt work got this output instead:
Advertisement
Answer
So, imagine a Chat class that holds a String that represents the chatter (or better, a well-defined User type object) as well text that this user is sending:
public class Chat { String name; String text; public Chat(String name, String text) { super(); this.name = name; this.text = text; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
Then you could create a list model and a JList that hold objects of this type:
private DefaultListModel<Chat> chatListModel = new DefaultListModel<Chat>(); private JList<Chat> chatList = new JList<Chat>(chatListModel);
Then you could create a renderer that displays the Chatter’s text as the default renderer text, and the chatter’s name in a titled border that goes around the individual renderer (the default renderer is a JLabel):
private class ChatRenderer extends DefaultListCellRenderer { @Override public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Chat chat = (Chat) value; String text = chat.getText(); String name = chat.getName(); JLabel rendererComponent = (JLabel) super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus); rendererComponent.setBorder(BorderFactory.createTitledBorder(name)); return rendererComponent; } }
The whole thing could look like:
import java.awt.BorderLayout; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.*; @SuppressWarnings("serial") public class FooGui01 extends JPanel { private DefaultListModel<Chat> chatListModel = new DefaultListModel<Chat>(); private JList<Chat> chatList = new JList<Chat>(chatListModel); private JTextField inputField = new JTextField(20); private JButton submitButton = new JButton("Submit"); public FooGui01() { chatList.setCellRenderer(new ChatRenderer()); chatList.setPrototypeCellValue(new Chat("John", "abc def ghi jkl abc def ghi jkl abc def ghi jkl abc def ghi jkl abc def ghi jkl abc def ghi jkl ")); chatList.setVisibleRowCount(10); JScrollPane scrollPane = new JScrollPane(chatList); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); ActionListener listener = new SubmitListener(); inputField.addActionListener(listener); submitButton.addActionListener(listener); submitButton.setMnemonic(KeyEvent.VK_S); JPanel submitPanel = new JPanel(); submitPanel.setLayout(new BoxLayout(submitPanel, BoxLayout.LINE_AXIS)); submitPanel.add(inputField); submitPanel.add(submitButton); setLayout(new BorderLayout()); add(scrollPane); add(submitPanel, BorderLayout.PAGE_END); } // public method called by outside code that is wanting to pass chat to you public void submitChat(Chat chat) { if (SwingUtilities.isEventDispatchThread()) { chatListModel.addElement(chat); } else { SwingUtilities.invokeLater(() -> chatListModel.addElement(chat)); } } private class SubmitListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String text = inputField.getText(); chatListModel.addElement(new Chat("Me", text)); inputField.selectAll(); inputField.requestFocusInWindow(); // here you would send the chat text or object to a socket's output stream } } private class ChatRenderer extends DefaultListCellRenderer { @Override public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Chat chat = (Chat) value; String text = chat.getText(); String name = chat.getName(); JLabel rendererComponent = (JLabel) super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus); rendererComponent.setBorder(BorderFactory.createTitledBorder(name)); return rendererComponent; } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Foo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FooGui01 gui = new FooGui01(); frame.add(gui); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }); } }