Sthatyc Soul. Sthatyc Soul. - 5 months ago 14
Java Question

Put a JTextArea in specific location

I want to know, how can I put my

JTextArea
, down of some buttons I have in my program... To show you how I want to order this, I made an example in a GUI.

Image example:

enter image description here

Basically, I want to put them down of the buttons, like in the example.
I try to put a JPanel as flowlayout, and add the JTextArea, but dont works.

Answer

Here is an example, as suggested a BorderLayoutis appropriate for the main panel.

To be the closer possible to your example, I added a margin (an empty border) to the north panel, as well as a horizontal strut between the buttons (with the use of a BoxLayoutfor the north panel).

JFrame frame = new JFrame();

JPanel contentPanel = new JPanel();

contentPanel.setLayout(new BorderLayout());

JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

JButton b1 = new JButton("B1");
JButton b2 = new JButton("B2");

buttonsPanel.add(b1);
buttonsPanel.add(Box.createHorizontalStrut(5));
buttonsPanel.add(b2);

contentPanel.add(buttonsPanel, BorderLayout.NORTH);
contentPanel.add(new JTextArea(), BorderLayout.CENTER);

frame.setContentPane(contentPanel);
frame.setSize(400, 300);
frame.setVisible(true);
Comments