Question

I have a simple problem but I found no good solution for it. I have a JFrame with components at SOUTH, NORTH, EAST, and CENTER. On CENTER is my problem. On center I have a JPanel with a Borderlayout and 2 JTextAreas (one on NORTH, one int the CENTER).

I want that the first panel begins always on top of the panel and stretch maximum (if needed) to the middle of the panel, not more. The second area should begin at the end of the first area. If one of both text areas is to big, there should appear a JScrollPane.

What is the best Way to realize that? Should I use a other layout for the panel?

Here is my little execute example for it:

public class myGUI {

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

    public myGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);

    //Add Content at South, West and North....
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());
        centerPanel.add(new JScrollPane(new JTextArea("AREA")), BorderLayout.NORTH);
        centerPanel.add(new JScrollPane(new JTextArea("AREA2")), BorderLayout.CENTER);
        frame.add(centerPanel);
        frame.setVisible(true);
    }
}

Possible scenarios of the center panel:

enter image description here

SOLUTION with help from @Hovercraft Full Of Eels

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

private static GridBagConstraints constraint = new GridBagConstraints();

public MyGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);

    frame.add(new JLabel("NORTH!"), BorderLayout.NORTH);
    frame.add(new JLabel("EAST!"), BorderLayout.EAST);
    frame.add(new JLabel("SOUTH!"), BorderLayout.SOUTH);

    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new GridBagLayout());
    changeConstraint(0,0);

    JTextArea text1 = createTextArea("11111111111111");
    centerPanel.add(text1, constraint);
    changeConstraint(0,1);

    JTextArea text2 = createTextArea("2222222222222");
    centerPanel.add(text2, constraint); 
    JScrollPane scroll = new JScrollPane(centerPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    frame.add(scroll, BorderLayout.CENTER);
    frame.setVisible(true);
}

/**
 * Create a Gridbag Constraint
 * @param text
 * @return
 */
private void changeConstraint(int gridx, int gridy){
    constraint.anchor = GridBagConstraints.FIRST_LINE_START;
    constraint.fill = GridBagConstraints.HORIZONTAL;
    constraint.weighty = 0;
    constraint.weightx = 1.0;
    constraint.gridx = gridx;
    constraint.gridy = gridy;
}

/**
 * Create a Textarea
 * @param text
 * @return
 */
private JTextArea createTextArea(String text){
    JTextArea textarea = new JTextArea(text);
    textarea.setWrapStyleWord(true);
    textarea.setLineWrap(true);
    return textarea;

}

}

Was it helpful?

Solution

One possibility: Consider creating a JPanel that uses GridBagLayout, and adding both JTextAreas to this JPanel, making sure to set their weightx to 0 and weighty to a non-zero value, say 1.0. Then place that JPanel into a JScrollPane and the JScrollPane into your GUI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top