I have this JTextArea and it keeps expanding as text is entered. I know this looks like a repeat question but I\'ve followed the answers and they haven\'t worked for me. I also
Your problem occurs here...
consolePanel.add(console);
consolePanel.add(scrollBar);
Basically a component can only belong to a single parent, by adding the console
to the consolePanel
you are removing it from the scroll pane.
The console
is already contained within a container, so you simply only need to add the parent container (the scroll pane) to consolePane
...
//consolePanel.add(console);
consolePanel.add(scrollBar);
On a side note, you should be careful of Tookit#getScreenSize
is it returns the "whole" screen and not the viewable screen size (that area which the application can safely use). So instead of ...
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
//sets the size of the GUI
this.setSize(600, 400);
//centers the GUI
int xPos = (dim.width / 2) - (this.getWidth() /2);
int yPos = (dim.height / 2) - (this.getHeight() /2);
this.setLocation(xPos, yPos);
You could use setLocationRelativeTo(null)
and achieve a better result
I would also suggest sizing the component after you have added the content and before you make it visible, for example...
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
This ensures that the window takes into consideration in difference between different platforms more accurately...