0

I've create a JFrame named homeWindowFrame and set its size to (600, 500) and then I added a JPanel named mainContainerPanel to the JFrame. I set a new size to JPanel but it's not working. JPanel size is remaining same as that of JFrame instead of updating. How can I set size to JPanels in a JFrame.Thanks in advance. Here my code:

/** * Main window construction */

    JFrame homeWindowFrame = new JFrame("Home - Crime File Management System");

    if (isInvalidLogin) {
        homeWindowFrame.setSize(600, 500);

    } else {
        homeWindowFrame.setSize(600, 400);
    }

    homeWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    homeWindowFrame.setLocation((screenSize.width / 2) - (homeWindowFrame.getWidth() / 2), (screenSize.height / 2) - (homeWindowFrame.getHeight() / 2));


    /**
     * Main panel construction
     */
    JPanel mainContainerPanel;

    if (isInvalidLogin) {
        mainContainerPanel = new JPanel(new GridLayout(4, 2));

    } else {
        mainContainerPanel = new JPanel(new GridLayout(3, 2));
    }

    homeWindowFrame.add(mainContainerPanel);
  • 1
    1) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 2) `homeWindowFrame.setLocation(..` can be replaced by `setLocationRelativeTo(null)`, but putting something at screen center makes it look like a splash screen. Better to `setLocationByPlatform(true)` until something better is implemented. For details see [this answer](https://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis/7143398#7143398) – Andrew Thompson Sep 03 '17 at 15:52
  • Please see edit to answer (code) – Hovercraft Full Of Eels Sep 03 '17 at 16:24

1 Answers1

1

Your code appears to be ignoring how Java Swing layout managers work. When you add a JPanel to a JFrame, the default lay out is BorderLayout, and this will center the panel in the frame and size it to fill the frame. If you wish to have a panel at different size, and its preferredSize needs to be set somehow, and the container that holds the JPanel will need to use a different layout manager. A GridBagLayout used in a "default" way (adding one component, no GridBagConstraints) for instance would center the JPanel if this is what you desire. If these suggestions do not help, then you should create a posterior own MCVE (please read the link).

For example, MY MCVE:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class DiffSizedPanel extends JPanel {
    private static final int PANEL_W = 400;
    private static final int PANEL_H = 300;
    private static final int FRAME_W = 600;
    private static final int FRAME_H = 500;
    private static final Color BG_COLOR = Color.PINK;

    public DiffSizedPanel() {
        setBackground(BG_COLOR);

        // set the JPanel the preferred size desired
        setPreferredSize(new Dimension(PANEL_W, PANEL_H));
        setBorder(BorderFactory.createTitledBorder("This is the JPanel"));
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Different Sized Panel");

        // set the JFrame the preferred size desired
        frame.setPreferredSize(new Dimension(FRAME_W, FRAME_H));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // change the content pane's layout from default BorderLayout to GridBagLayout
        frame.getContentPane().setLayout(new GridBagLayout());
        frame.getContentPane().add(new DiffSizedPanel());  // add the JPanel
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373