I asked a question before, on how to make a JTextField insert its text from beneath. Now I got some problem with the code. I edited a little bit, just for testing. Here is the code I'm using right now:
public class BaseTextAreaDemo {
private static JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("Base JTextArea App");
final JPanel panel = new JPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
JPanel textAreaPanel = getBaseTextArea();
JScrollPane scrollPane = new JScrollPane(textAreaPanel) {
public Dimension getPreferredSize() {
return new Dimension(300, 230);
}
};
panel.add(scrollPane, BorderLayout.SOUTH);
JTextField textField = new JTextField();
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
textArea.append(arg0.getActionCommand() + "\n");
}
});
frame.add(panel, BorderLayout.CENTER);
frame.add(textField, BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getBaseTextArea() {
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.append("bla bla bla\n");
textArea.append("new text here");
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(textArea.getBackground());
panel.setBorder(textArea.getBorder());
textArea.setBorder(null);
panel.add(textArea, BorderLayout.SOUTH);
return panel;
}
});
}
I just added a textField, because I also need one in the program I need this in, and it's useful to add more lines this way.
Now the problem is, when I run this program and I add lines until a vertical scrollbar appears, a horizontal comes out too. I already figured that you can just turn that off, but then some text falls out of the screen. Also, when you make the frame wider, and than return it to it's normal size, the horizontal scrollbar stays at the size from the widened window.
Another problem is that the speed of scrolling is very low in the JTextField.