1

I am trying to make a panel to have 4 items on top. these are a JLabel, JTextField, JLabel and JTextField.

In the center I need a JTextArea and to the left of it a JList that is scrollable.

On the bottom I need 3 buttons.

What would be the best layout manager for this and how should I go about it?

Would having just 3 columns be a good idea?

enter image description here

Heres what I have so far:

                    JPanel panel = new JPanel();

        JTextField IDLabel = new JLabel("ID: ");
        IDLabel.setBounds(10, 10, 80, 25);
        panel.add(IDLabel);

        JTextArea IDText = new JTextField(5);
        IDText.setBounds(100, 10, 160, 25);
        panel.add(IDText);

        JLabel TitleLabel = new JLabel("Title: ");
        TitleLabel.setBounds(10, 10, 80, 25);
        panel.add(TitleLabel);

        JTextField TitleText = new JTextField(10);
        TitleText.setBounds(100, 10, 160, 25);
        panel.add(TitleText);


        JList list = new JList(new String[]{"test1", "test22"});

                list.setFixedCellWidth(150);
                list.setFixedCellHeight(50);
                list.setFont(new Font("Serif",Font.BOLD,16));
                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        panel.add(list);

        JTextArea BodyArea = new JTextArea();
        BodyArea.setSize(200, 200);
        BodyArea.setText("Test area");
        panel.add(BodyArea);
1
  • +1 for being the one righteous person to post a mock-up GUI of the intended result & code of "what I have done so far". Commented May 5, 2014 at 4:17

2 Answers 2

2

You will mostly likely need to use a combination of layouts (AKA compound layouts), for example

North Panel

Create a JPanel and assign it a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.

Add the JLabel, JTextField, JLabel, JTextField to it.

Center Panel

Create a JPanel with BorderLayout. Add the JTextArea to the CENTER position and the JList to the WEST position

South Panel

Craete a JPanel with a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.

Add the buttons to it.

Putting it together

Create a JPanel with a BorderLayout, add the "north" panel to the NORTH position, the "center" panel to the CENTER position and the "south" panel to the SOUTH position

You could use a single container and a GridBagLayout, but that's a lot of work.

Take a look at Laying Out Components Within a Container for more details

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe start with a BorderLayout for the main layout. Then you can add components to the PAGE_START (NORTH), LINE_START (WEST) and CENTER and PAGE_END (SOUTH). Check out the section from the Swing tutorial on Using Layout Manager, for more information and examples.

Of course you would also use panels when you want to display multiple components in a single area. So your buttons would first be added to panels.

Also, follow standard naming conventions. Variable names should NOT start with an upper case character( ie. BodyArea, TitleText). You got variable like (panel, list) correct so be consistent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.