Reputation: 316
Can you please tell me why the scroll doesn't work. It is visible, but it doesn't work, here you can view that piece of code. What could be the missing part?
// GUI elements
private JTextField textSend = new JTextField(20);
private JTextArea textArea = new JTextArea(5, 20);
private JScrollPane scroll = new JScrollPane(textArea);
private JButton buttonConnect = new JButton("Connect");
private JButton buttonSend = new JButton("Send");
private JButton buttonDisconnect = new JButton("Disconnect");
private JButton buttonQuit = new JButton("Quit");
private JPanel leftPanel = new JPanel();
private JPanel rightPanel = new JPanel();
private JLabel empty = new JLabel("");
ChessHeroChatClient() {
setTitle("ChessHero Chat Client");
setLocationRelativeTo(null);
setSize(500, 500);
setResizable(false);
leftPanel.setLayout(new BorderLayout());
rightPanel.setLayout(new GridLayout(6, 1));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
leftPanel.add(textSend, BorderLayout.NORTH);
leftPanel.add(textArea, BorderLayout.WEST);
leftPanel.add(scroll, BorderLayout.EAST);
add(leftPanel, BorderLayout.CENTER);
add(rightPanel, BorderLayout.EAST);
textArea.setEditable(false);
Upvotes: 0
Views: 189
Reputation: 324197
private JTextArea textArea = new JTextArea(5, 20);
private JScrollPane scroll = new JScrollPane(textArea);
You create the scrollPane with the textArea which is good.
//leftPanel.add(textArea, BorderLayout.WEST); // this is wrong
leftPanel.add(scroll, BorderLayout.EAST);
But then you add the textArea to the WEST and the scroll to the EAST, which is wrong. A Swing component can only have a single parent, so just leave the textArea alone and add the scrollPane to the EAST or WEST.
Upvotes: 4