Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextArea that needs to grow with parent panel

I want to make my JTextArea field as big as it can be in current JPanel. How to do that?

Now it is like this:

    JPanel statusBar = new StatusBar(project);
    JTextArea outputBox = new JTextArea(1, 50); 
    outputBox.setEditable(true);
    statusBar.add(outputBox);
like image 732
Adrian Adamczyk Avatar asked Feb 02 '26 22:02

Adrian Adamczyk


2 Answers

The default layout manager of JPanel is FlowLayout, which wouldn't let the text area fill the entire available space in the panel.

Using BorderLayout should work well:

statusBar.setLayout( new BorderLayout() );
JTextArea outputBox = new JTextArea(1, 50); 
outputBox.setEditable(true);
statusBar.add(outputBox, BorderLayout.CENTER);

You need a layout manager on the JPanel. If its just the JTextArea contained within it and you need to maximise it you can use a simple GridLayout:

   statusBar.setLayout(new GridLayout(1,1));
like image 28
AntonyM Avatar answered Feb 04 '26 12:02

AntonyM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!