I'm trying to create a window with a scrollable JTextArea
and a JTextField
below it. I want the frame to look like a chat window; one, large scrollable text area and a single lined text frame. I've tried variations but I can't get the text area scrollable without making the entire window scrollable. It's incredibly annoying. My current iteration only draws one panel to the screen:
private void buildGUI() {
Container chatClientContainer = getContentPane();
chatClientContainer.setLayout(new BorderLayout());
JPanel messagesReceivedPanel = new JPanel();
messagesReceivedPanel.setLayout(new GridLayout(1, 1, 5, 5));
JTextArea messagesReceived = new JTextArea("area");
messagesReceivedPanel.add(messagesReceived);
JPanel draftPanel = new JPanel();
draftPanel.setLayout(new GridLayout(1, 1, 5, 5));
JTextField draftMessage = new JTextField("field");
draftPanel.add(draftMessage);
chatClientContainer.add(new JScrollPane(messagesReceivedPanel));
chatClientContainer.add(draftPanel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int windowWidth = 400;
int windowHeight = 600;
int posX = ((int) screenSize.getWidth())/2 - windowWidth/2;
int posY = (int) screenSize.getHeight()/2 - windowHeight/2;
setBounds(posX, posY, windowWidth, windowHeight);
setResizable(true);
setVisible(true);
}
How can I position this the way I want?