JScrollPane is not making my JTextArea scrollable....
I'm making a little chat client for school and I'm having trouble getting my GUI to work right. I'm basically having two problems:
1. When I put my JTextArea's in JScrollPane's, they aren't becoming scrollable. When I type on it the text just goes off the JTextArea and doesn't scroll. I can't understand why.
2. When I don't specify a preferred size for my JTextArea's they shrink up to as small as possible and are unusable. When I do specify one they don't stretch if I increase or decrease the size of the window. I basically want them to use up as much space as possible.
Here's my code:
package main;
import javax.swing.*;
import java.awt.*;
publicclass Main{
private JFrame frame;
private JPanel panel,output,input;
private JTextArea area,field;
private JButton button;
public Main(){
frame =new JFrame("Chat Window");
panel =new JPanel();
output =new JPanel();
input =new JPanel();
area =new JTextArea();
field =new JTextArea();
button =new JButton("Send");
button.setSize(50,20);
//area.setPreferredSize(new Dimension(300,300));
//field.setPreferredSize(new Dimension(240,100));
output.add(new JScrollPane(area),"Center");
input.add(new JScrollPane(field),"West");
input.add(button,"East");
panel.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,output,input));
frame.add(panel);
frame.setSize(300,450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
}
publicstaticvoid main(String...a){
Main main =new Main();
}
}
# 2
import javax.swing.*;
import java.awt.*;
class Main {
private JFrame frame;
private JTextArea area,field;
private JButton button;
public Main(){
frame = new JFrame("Chat Window");
area = new JTextArea();
field = new JTextArea();
button = new JButton("Send");
frame.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,new JScrollPane(area),new JScrollPane(field)));
frame.add(button,"South");
frame.setSize(300,450);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args){
Main main = new Main();
}
}
# 3
How about something like this?:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class Main
{
private JFrame frame3;
private JPanel outputPnl, inputPnl;
private JTextArea outputTxt, inputTxt;
private JButton btn3;
private void wtf3()
{
frame3 = new JFrame();
outputTxt = new JTextArea();
inputTxt = new JTextArea();
btn3 = new JButton("Send");
outputPnl = new JPanel();
inputPnl = new JPanel();
outputPnl.setLayout(new GridLayout());
outputPnl.add(new JScrollPane(outputTxt));
inputPnl.setLayout(new BorderLayout());
inputPnl.add(new JScrollPane(inputTxt), BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
btnPanel.add(btn3, BorderLayout.CENTER);
btnPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
inputPnl.add(btnPanel, BorderLayout.EAST);
JSplitPane splitP3 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, outputPnl, inputPnl);
frame3.getContentPane().add(splitP3);
frame3.setSize(600, 450);
frame3.setLocationRelativeTo(null);
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.setVisible(true);
splitP3.setDividerLocation(0.8);
}
public Main()
{
wtf3();
}
private static void createAndShow()
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Main mn = new Main();
}
});
}
public static void main(String[] args)
{
createAndShow();
}
}