Mazzy
Mazzy

Reputation: 14227

The app does not resize its component

I have this app, but, when I resize the window, the element JTextArea inside, it doesn't resize with the window. Why?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ExampleGUI {

private JTextArea text_area;
private JScrollPane scroll_bar;

private JFrame frame;
private JPanel panel;

public ExampleGUI(){

    frame = new JFrame("Example GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    text_area = new JTextArea();
    scroll_bar = new JScrollPane(text_area);

    panel = new JPanel();
    panel.add(scroll_bar);

    frame.add(panel);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){public void run(){new ExampleGUI();}});
}
}

Upvotes: 1

Views: 493

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168845

This takes into account the advice of Hovercraft Full Of Eels & Ted Hopp with a few other tweaks.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.Collection;

public class AziendaGUI implements ActionListener {

private JButton view_list;
private JButton save_list;
private JTextArea text_area;
private JScrollPane scrollpane;
private JPanel pane;

private JFrame frame;
private GridBagLayout grid;

public AziendaGUI() {

    frame = new JFrame("Immobiliari s.p.a");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new BorderLayout());

    view_list = new JButton("View Property");
    view_list.setActionCommand("view_list");
    view_list.addActionListener(this);

    save_list = new JButton("Save List");
    save_list.setActionCommand("save_list");
    save_list.addActionListener(this);

    text_area = new JTextArea(10,22);
    text_area.setEditable(false);
    scrollpane = new JScrollPane(text_area);

    grid = new GridBagLayout();
    pane = new JPanel();
    pane.setLayout(grid);

    /* Set Constraints view_list button */
    grid.setConstraints(view_list, new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(view_list);

    /* Set Constraints save_list button */
    grid.setConstraints(save_list,new GridBagConstraints(1,0,1,1,0.1,0.1,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(save_list);

    frame.add(scrollpane);

    frame.add(pane, BorderLayout.NORTH);

    frame.pack();
    frame.setVisible(true);
}

private void store(){

    String file_name = JOptionPane.showInputDialog("Inserisci il nome del file");

}

@Override
public void actionPerformed(ActionEvent e){

    String s = e.getActionCommand();

    if(s.equals("view_list")){
    }
    if(s.equals("save_list")){

        store();
    }
}


public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){@Override
                                              public void run(){new AziendaGUI();}});
}
}

Upvotes: 3

Ted Hopp
Ted Hopp

Reputation: 234857

Your frame layout is a FlowLayout. This does not resize children. From the docs:

A flow layout lets each component assume its natural (preferred) size.

You will be better off using a BorderLayout and putting the pane in the CENTER.

Replace this:

frame.setLayout(new FlowLayout());
frame.add(pane);

with this:

frame.setLayout(new BorderLayout());
frame.add(pane, BorderLayout.CENTER);

Also, as Hovercraft points out, if you need the individual components to resize when the pane resizes, then you need to have non-zero weights in the GridBagConstraints.

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

You need to set your GridBagConstraint x and y weights (weightx and weighty -- the 5th and 6th parameters in the GridBagConstraint constructor) to a positive value other than 0.0. You should read tutorials on GridBagLayout if you're going to use it as it is fairly complex. Some have had great success nesting simpler layouts or using 3rd party layouts such as MigLayout.

Upvotes: 3

Related Questions