Corei7
Corei7

Reputation: 43

Java GUI layout for fixed position of components and scrollable window

I want to make a program such that the components behave as shown in the pictures below

I would also want TextArea to be "scrollable" when it's not big enough to show all the text at once.

Any tips? :)

Window before and after expanding

Upvotes: 1

Views: 455

Answers (1)

camickr
camickr

Reputation: 324118

The secret of layout management is to nest panels with different layout managers to achieve your desired layout.

So you start with the default BorderLayout of the frame.

Then you create a panel for your buttons and add the panel to the frame:

JPanel buttonPanel = new JPanel( new FlowLayout(...) );
buttonPanel.add( button1 );
..
frame.add(buttonPanel, BorderLayout.PAGE_START);

Next you add your text area to the frame:

JTextArea textArea = new JTextArea(10, 30);
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);

Now the two things happen with the text area:

  1. it will resize to fill the space of the frame
  2. scrollbars will appear when required.

Read the section from the Swing tutorial on Layout Managers for more information and working examples.

Upvotes: 1

Related Questions