kusnendi
kusnendi

Reputation: 3

How to resize JFrame and show JPanel and/or its controls with a button?

I have made simple form that looks like this:

How to make the Show Details button display an additional panel and form like this:

And when the button is clicked again, it hides the control and the panel.

Upvotes: 0

Views: 1149

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Let's assume the controls from Filter down through Show Details are in a single (permanently viewable - PV) panel, and the controls to be show/hidden are in a second (hide or show - HS hideOrShowPanel) panel. Put the PV panel in the CENTER of a BorderLayout and the HS panel in the LINE_END.

On button toggle, call..

private void changePanelVisibility(Boolean visible) {
    hideOrShowPanel.setVisible(visible);
    topLevelContainer.pack(); // revalidate layout and size the GUI to fit the controls
}

The topLevelContainer might be a JFrame, but this GUI seems better suited to being displayed in a JDialog. Especially so since this would look best in a fixed size (non user resizable) top level container, which the dialog is by default.

Upvotes: 1

mr mcwolf
mr mcwolf

Reputation: 2859

After display / hide the details panel, pack() must be called to resize the frame. And setLocationRelativeTo() respectively to position itself correctly.

Here is an example

Main.java

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new MainFrame("Test Frame").setVisible(true));
    }
}

MainFrame.java

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

public class MainFrame extends JFrame {

    private DetailsPanel detailsPanel;

    public MainFrame(String title) throws HeadlessException {
        super(title);
        createGUI();
    }

    private void createGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        detailsPanel = new DetailsPanel();

        add(new MainPanel(this), BorderLayout.CENTER);
        add(detailsPanel, BorderLayout.LINE_END);

        pack();
        setLocationRelativeTo(null);
    }

    public void showDetails(boolean show) {
        detailsPanel.setVisible(show);

        pack();
        setLocationRelativeTo(null);
    }
}

MainPanel.java

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

public class MainPanel extends JPanel {

    private MainFrame owner;
    private JButton showDetailsButton;

    private boolean details = false;

    public MainPanel(MainFrame owner) {
        super();
        this.owner = owner;

        createGUI();
    }

    private void createGUI() {
        showDetailsButton = new JButton("Details >>>");
        showDetailsButton.addActionListener(this::showDetais);

        JPanel contentPanel = new JPanel();
        contentPanel.setPreferredSize(new Dimension(400, 400));
        contentPanel.setBackground(Color.CYAN);

        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonsPanel.add(showDetailsButton);

        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(400, 400));
        setMinimumSize(getPreferredSize());


        add(contentPanel, BorderLayout.CENTER);
        add(buttonsPanel, BorderLayout.PAGE_END);
    }

    private void showDetais(ActionEvent event) {
        showDetailsButton.setText(details ? "Details >>>" : "<<< Details");

        details = !details;
        owner.showDetails(details);
    }
}

and DetailsPanel.java

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

public class DetailsPanel extends JPanel {

    public DetailsPanel() {
        super();
        createGUI();
    }

    private void createGUI() {
        setBackground(Color.BLUE);
        setPreferredSize(new Dimension(200, 800));
        setVisible(false);
    }
}

Upvotes: 1

cello
cello

Reputation: 5486

It might depend a bit on the LayoutManager that you use. But in general, have the Details-panel always added to the Dialog, but set it invisible at start. When pressing the "Show Details" button, toggle the visibility of the Details panel and resize the dialog to accommodate for the additional space it requires.

Upvotes: 0

Related Questions