简体   繁体   中英

How to set JPanel height to auto

I create layout manually not using GUI editor, I need to set height to auto so the entire form visible to user. I wrap GridLayout into FlowLayout so input is not stretch. What's the best practices to create layout like this. Could someone modify it so the entire form visible or using GridLayout but width and height of input in the form can be set ?

package helpdesk;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Joko Wandiro
 */
public class Layout extends Base {

    JPanel panel_form_edit = new JPanel();
    FlowLayout layout_form_edit_outer = new FlowLayout(FlowLayout.LEFT);
    JPanel panel_form_edit_outer = new JPanel();
    // Form - Edit
    JLabel label_name_update = new JLabel("Name", JLabel.LEFT);
    JTextField input_id_update = new JTextField();
    JTextField input_name_update = new JTextField();
    JButton btn_update = new JButton("Update");

    public Layout() {
        setLookAndFeel();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Main Layout
        GridLayout layout = new GridLayout(5, 1, 10, 10);
        setLayout(layout);
        // Form - Edit
        label_name_update.setPreferredSize(new Dimension(400, 20));
        input_name_update.setPreferredSize(new Dimension(400, 20));
        input_id_update.setVisible(false);
        GridLayout layout_form_edit = new GridLayout(4, 1, 10, 10);
        panel_form_edit.setLayout(layout_form_edit);
        panel_form_edit.add(input_id_update);
        panel_form_edit.add(label_name_update);
        panel_form_edit.add(input_name_update);
        panel_form_edit.add(btn_update);
        panel_form_edit_outer.setLayout(layout_form_edit_outer);
        panel_form_edit_outer.add(panel_form_edit);
        add(panel_form_edit_outer);
        panel_form_edit.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        panel_form_edit_outer.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Layout app = new Layout();
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            // ignore error
        }
    }

}

Without set preferred size with pack method

不使用包装方法设置首选尺寸

Set preferred size with pack method

使用包装方法设置首选尺寸

Set extended state to maximize but update button is not fully visible. How to set height of JPanel so form is fully visible ?

设置扩展状态以最大化

I want JFrame display in maximize screen.

Thanks a ton.

I need to set height to auto so the entire form visible to user.

You use:

pack();
setVisible(true);

The pack() method will make the frame the preferred size of all the components added to the frame.

label_name_update.setPreferredSize(new Dimension(400, 20));
input_name_update.setPreferredSize(new Dimension(400, 20));

Don't use setPreferredSize(...). Each Swing component will determine its own preferred size based on the text, font etc.

Edit:

GridLayout layout_form_edit = new GridLayout(5, 1, 10, 10);

The above line appears to cause the problem. You are specifying 5 rows but I guess there is not enough space on the frame for 5 rows.

Try:

GridLayout layout_form_edit = new GridLayout(0, 1, 10, 10);

I use JScrollPane to make JFrame scrollable so the entire form is visible.

package helpdesk;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Joko Wandiro
 */
public class Layout extends Base {

    JPanel panel_form_edit = new JPanel();
    FlowLayout layout_form_edit_outer = new FlowLayout(FlowLayout.LEFT);
    JPanel panel_form_edit_outer = new JPanel();
    // Form - Edit
    JLabel label_name_update = new JLabel("Name", JLabel.LEFT);
    JTextField input_id_update = new JTextField();
    JTextField input_name_update = new JTextField();
    JButton btn_update = new JButton("Update");

    public Layout() {
        setLookAndFeel();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Main Layout
        GridLayout layout = new GridLayout(5, 1, 10, 10);
        setLayout(layout);
        // Form - Edit
        label_name_update.setPreferredSize(new Dimension(400, 20));
        input_name_update.setPreferredSize(new Dimension(400, 20));
        input_id_update.setVisible(false);
        GridLayout layout_form_edit = new GridLayout(4, 1, 10, 10);
        panel_form_edit.setLayout(layout_form_edit);
        panel_form_edit.add(input_id_update);
        panel_form_edit.add(label_name_update);
        panel_form_edit.add(input_name_update);
        panel_form_edit.add(btn_update);
        panel_form_edit_outer.setLayout(layout_form_edit_outer);
        panel_form_edit_outer.add(panel_form_edit);
        add(panel_form_edit_outer);
        // Set window or jframe scrollable
        Container container = getContentPane();
        JScrollPane scroll = new JScrollPane(container);
        setContentPane(scroll);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Layout app = new Layout();
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            // ignore error
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM