Sidharth
Sidharth

Reputation: 3679

In swing GUI how to show a JPanel as maximized when the user clicks a button?

I want to maximize a JPanel inside a JFrame when the user clicks a button.What is the best way to achieve this.The view and the data model should be in sync in both the panels,that is the panel which in the JFrame and the maximized one.Please suggest me some solution.

my requirement is: i have a JFrame with 4 JPanels named as

So he need to maximize this JPanelD alone to see the contents of the JList clearly.For that he clicks "MAXIMIZE PANEL" button.After the click action ,the JPanelD in the JFrame remains there,also a new JPanel with the same JList data(ie.,the replica of the JPanelD say JPanelDMaximized)should be popped up.This is what i want to do ..

Upvotes: 1

Views: 11386

Answers (4)

Noel Ang
Noel Ang

Reputation: 5109

Follow-up to your clarification of the problem:

Take my code, and remove:

maximizedFrame.setUndecorated( true );

and size the frame bigger before you make it visible. That should satisfy the maximize-like behaviour you need.

Your other problem is that you cannot add JPanelD to the two JFrames. The pop-up frame must have its own unique JPanel object (let's call it JPanelE). So you need to:

  1. Initialize and lay out JPanelE like you do JPanelD. That means giving JPanelE its own JList (and JTree, and so on).
  2. Share the ListModel from JPanelD's JList with JPanelE's JList, and so on. The feasibility and details of executing this successfully depends on the specifics of your implementation, and is beyond the scope of your original problem.

Upvotes: 1

Noel Ang
Noel Ang

Reputation: 5109

  1. Create a JWindow (or an undecorated JFrame) with a JPanel. Leave the JWindow invisible, initially. (The wiring of this new JPanel to the same data model used by the original JPanel is left as an exercise.)

  2. When your maximize-panel button's ActionListener executes, it must:

    2.1. Update the (invisible) JWindow's location and size to match the (visible) JFrame's.

    2.2. Make your JFrame invisible.

    2.3. Make your JWindow visible.

  3. When your unmaximize-panel button's ActionListener executes, it must:

    3.1. Update the (invisible) JFrame's location and size to match the (visible) JWindow's.

    3.2. Make your JWindow invisible.

    3.3. Make your JFrame visible

Example:

package stackoverflow;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MaximizingPanelApp extends JFrame {

private JPanel framePanel;
private JPanel windowPanel;
private JFrame maximizedFrame;

public static void main(String[] args) {
    JFrame appFrame = new MaximizingPanelApp();
    appFrame.setVisible( true );
}

public MaximizingPanelApp() throws HeadlessException {
    super( "Application" );
    setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    initialize();
}

private void initialize() {
    // JFrame
    {
        Container container = getContentPane();
        container.setLayout( new BorderLayout() );

        framePanel = new JPanel();
        framePanel.setBackground( Color.ORANGE );
        container.add( framePanel, BorderLayout.CENTER );

        JButton button = new JButton( new MaximizeAction() );
        container.add( button, BorderLayout.SOUTH );
        setSize( 400, 300 );
    }

    // JWindow
    {
        maximizedFrame = new JFrame();
        Container container = maximizedFrame.getContentPane();
        container.setLayout( new BorderLayout() );

        windowPanel = new JPanel();
        windowPanel.setBackground( Color.ORANGE );
        container.add( windowPanel, BorderLayout.CENTER );

        JButton button = new JButton( new UnMaximizeAction() );
        container.add( button, BorderLayout.SOUTH );
        maximizedFrame.setSize( getSize() );
        maximizedFrame.setUndecorated( true );
    }

}

private class MaximizeAction extends AbstractAction {

    private MaximizeAction() {
        super( "Maximize" );
    }

    public void actionPerformed(ActionEvent e) {
        maximizedFrame.setSize( getSize() );
        maximizedFrame.setLocation( getLocation() );
        setVisible( false );
        maximizedFrame.setVisible( true );
    }
}

private class UnMaximizeAction extends AbstractAction {

    private UnMaximizeAction() {
        super( "Un-Maximize" );
    }

    public void actionPerformed(ActionEvent e) {
        setLocation( maximizedFrame.getLocation() );
        setSize( maximizedFrame.getSize() );
        maximizedFrame.setVisible( false );
        maximizedFrame.dispose();
        setVisible( true );
    }
}
}

Upvotes: 1

Yishai
Yishai

Reputation: 91931

Of course you could do this yourself, but you should really look at JInternalFrame and consider using that for your panel. It will save a bunch of headache.

Edit: Sun's tutorial should get you what you need.

Upvotes: 2

Steve McLeod
Steve McLeod

Reputation: 52478

This depends on the layout manager you use. If you add a JPanel to a JFrame using the default layout manager, and the JFrame only contains the JPanel and nothing else, you'll achieve what you describe.

Here's an example. The JPanel is green; notice how it resizes as you resize the JFrame.

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

public class ScratchSpace {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Stretchy panel demo");
        final JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(Color.GREEN);
        panel.setPreferredSize(new Dimension(600, 400));
        final JComponent contentPane = (JComponent) frame.getContentPane();
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Upvotes: 0

Related Questions