GridBagLayout not showing JTextArea, and showing panel in center

In the following code, when I add the JTextArea to the main panel, it doesn't show up. When I add the controlPanel, it shows up in the center, not the edge. I'm new with GridBagLayout, so I'm assuming I'm missing something simple.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        JFrame frame = new JFrame();

        JPanel mainPanel = new JPanel();
        JPanel controlPanel = new JPanel();

        mainPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        controlPanel.add(new JButton("Play"));
        controlPanel.add(new JButton("Pause"));
        controlPanel.add(new JSpinner());
        JTextArea textArea = new JTextArea();

        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 3;
        c.gridwidth = 3;
        mainPanel.add(textArea, c);
        // mainPanel.add(controlPanel, c);
        frame.add(mainPanel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 700);
        frame.setLocation(250, 100);
        frame.setVisible(true);
    }
}

EDIT: This is how the constraints look after your suggestions. The textArea still does not show up.

        c.gridx = 0;
        c.gridy = 0;
//      c.gridheight = 3;
//      c.gridwidth = 3;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.NORTHWEST;
        mainPanel.add(textArea, c);
        // mainPanel.add(controlPanel, c);
        frame.add(mainPanel);

Answers


Don't forget weights and anchors:

    c.weightx = 1.0;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.NORTHWEST;

Edit: Example of adding row and column values to the JTextArea:

    controlPanel.add(new JButton("Play"));
    controlPanel.add(new JButton("Pause"));
    controlPanel.add(new JSpinner());

    JTextArea textArea = new JTextArea(20, 40);

    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 3;
    c.gridwidth = 3;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.NORTHWEST;
    mainPanel.add(new JScrollPane(textArea), c);

My totally serious answer would be to not use GridBagLayout. Leave GridBagLayout for the GUI form builders.

If you want to build GUI's by hand (I recommend building them by hand and avoiding form builders, BTW) then generally BorderLayout with nested panels using BoxLayouts are really your best bet.


Need Your Help

How to post questions in sencha forum?

extjs sencha-touch forum

i like to post some questions in sencha forum. I created account in sencha touch.Could anyone guide me How to post questions in Sencha forum ? Because i'm new to sencha forum. Don't mistake me frie...

Error : expected body of lambda expression .. (what is Lambda ???)

c++ c++11 lambda

Just start learning CS106B , Error messages telling me something wrong with these 3 lines.