tags:

views:

451

answers:

4

Hi,

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

  • JPanelA,JPanelB,JPanelC,JPanelD
  • Here the JPanelD contains a JList and a button below it say "MAXIMIZE PANEL" button . JList has a JTree with in it . Sometimes the JList may have huge set of data and it is not visible to the user clearly.

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 ..

+2  A: 

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.

Yishai
hi Yishai,can you give me code snippet to do this?
Sidharth
A: 

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);
    }
}
Steve McLeod
Hi Steve McLeod ,this is not my requirement .. i need to have the JPanel in the frame and also its should be displayed infront of the JFrame when i press "maximize this panel" button ..
Sidharth
Hi Steve McLeod ..i edited my requirement clearly in my question now ..please see there ..
Sidharth
+1  A: 
  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 );
    }
}
}
Noel Ang
If by "maximize" you meant stretch the panel to the bounds of the desktop/display-device, not of its JFrame, then prior to displaying the JWindow, resize it instead to the display device's dimensions.
Noel Ang
Hi Noel ,, thanks a lot for your very neat and clear coding ..Here i want some changes .. edited the question itself ..please see there ..
Sidharth
+1  A: 

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.
Noel Ang
s Noel now i can think of that ..i need to invoke a JWindow with the shared List
Sidharth