Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to make a JPanel to Full Size of it Outer JPanel?

sunnycodeJun 25 2007 — edited Jun 25 2007
Hello sir:

I met a problem I have following runnable code that Outer JPane jp1 has an inner JPanel jp2, I hope to click "Full" button, then I can get jp2 expanded to the full size of jp1, and when I click "Normal" Button, it shrinks back to original size.

I tried to add some code, not success, I also google this forum, not find proper posts.
Can somebody here give some advice??
Thanks
import java.awt.*;
import java.awt.event.*; 
import java.awt.geom.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.*;

public class ResizeFullJPanels extends JPanel
{
    protected JLabel label1;
    protected JLabel[] labels;
    protected JPanel[] panels;
    protected JPanel selectedJPanel;
    protected JButton btnFull	  	= new JButton("Full");
    protected JButton btn		  	= new JButton("move");
    protected JButton btnNormal  	= new JButton("Normal");

    int cx, cy;
    protected Vector order = new Vector();	  
	public static void main(String[] args)
    {
        JFrame f = new JFrame("Test to Full Size");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new ResizeFullJPanels().GroupJLabels());
        f.setSize(600,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }

	private MouseListener ml = new MouseAdapter() {   
		public void mousePressed(MouseEvent e) {    
			Point p = e.getPoint();       
			JPanel jp = new JPanel();
			jp.setLayout(null);
			Component[] c = ((JPanel)e.getSource()).getComponents();
			System.out.println("c.length = " + c.length);       
			for(int j = 0; j < c.length; j++) {         
				if(c[j].getBounds().contains(p)) {      
					if(selectedJPanel != null && selectedJPanel != (JPanel)c[j]) 
						selectedJPanel.setBorder(BorderFactory.createEtchedBorder());
					selectedJPanel = (JPanel)c[j];             
					selectedJPanel.setBorder(BorderFactory.createLineBorder(Color.green));
					break;              
					}   
				add(jp);
				revalidate();
				}      
			}   
		};  
public JPanel GroupJLabels ()
{
		setLayout(null);
        addLabels();
        label1.setBounds( 125,  150, 125, 25);
        //add(btn);
        btn.setBounds(10, 5, 100, 25);
        btnFull.setBounds(100, 5, 100, 25);
        btnNormal.setBounds(200, 5,100, 25);
       	add(btn);
       	add(btnFull);
       	add(btnNormal);
       	
       	
	    ActionListener lstFull = new ActionListener() {
	 		   public void actionPerformed(ActionEvent e) {
	 				setPreferredSize(new Dimension(1700,1000));
	 				revalidate();
	 				repaint();
		      }
	    }; 	 
       determineCenterOfComponents();
        ComponentMover mover = new ComponentMover();
 	    ActionListener lst = new ActionListener() {
 		   public void actionPerformed(ActionEvent e) {
 			  ComponentMover mover = new ComponentMover();
 			        addMouseListener(mover);
 			        addMouseMotionListener(mover);
 		      }
 		    };
	
 		btn.addActionListener(lst);
		btnFull.addActionListener(lstFull);
		addMouseListener(ml);  
		return this;
    }
 
    public void paintComponent(final Graphics g)
    {
    	super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    	Point[] p;
        g2.setStroke(new BasicStroke(4f));
        
       for(int i = 0 ; i < order.size()-1; i++) {
    	   JPanel l1 = (JPanel)order.elementAt(i);
    	   JPanel l2 = (JPanel)order.elementAt(i+1);
            p = getCenterPoints(l1, l2);
            g2.setColor(Color.black);

           // g2.draw(new Line2D.Double(p[0], p[1]));       	
       }
 }
    
    private Point[] getCenterPoints(Component c1, Component c2)
    {
        Point
            p1 = new Point(),
            p2 = new Point();
        Rectangle
            r1 = c1.getBounds(),
            r2 = c2.getBounds();

        	p1.x = r1.x + r1.width/2;
        	p1.y = r1.y + r1.height/2;
        	p2.x = r2.x + r2.width/2;
        	p2.y = r2.y + r2.height/2;

        return new Point[] {p1, p2};
    }


    private void determineCenterOfComponents()
    {
        int
            xMin = Integer.MAX_VALUE,
            yMin = Integer.MAX_VALUE,
            xMax = 0,
            yMax = 0; 
        for(int i = 0; i < labels.length; i++)
        {
            Rectangle r = labels.getBounds();
if(r.x < xMin)
xMin = r.x;
if(r.y < yMin)
yMin = r.y;
if(r.x + r.width > xMax)
xMax = r.x + r.width;
if(r.y + r.height > yMax)
yMax = r.y + r.height;
}
cx = xMin + (xMax - xMin)/2;
cy = yMin + (yMax - yMin)/2;
}

private class ComponentMover extends MouseInputAdapter
{
Point offsetP = new Point();
boolean dragging;

public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
for(int i = 0; i < panels.length; i++)
{
Rectangle r = panels[i].getBounds();
if(r.contains(p))
{
selectedJPanel = panels[i];
order.addElement(panels[i]);
offsetP.x = p.x - r.x;
offsetP.y = p.y - r.y;
dragging = true;
repaint(); //added
break;
}
}
}

public void mouseReleased(MouseEvent e)
{
dragging = false;
}

public void mouseDragged(MouseEvent e)
{
if(dragging)
{
Rectangle r = selectedJPanel.getBounds();
r.x = e.getX() - offsetP.x;
r.y = e.getY() - offsetP.y;
selectedJPanel.setBounds(r.x, r.y, r.width, r.height);
//determineCenterOfComponents();
repaint();
}
}
}

private void addLabels()
{
label1 = new JLabel("Label 1");

labels = new JLabel[] {
label1
};

JLabel jl = new JLabel("This is resizeable JPanel at Runtime");
jl.setBackground(Color.green);
jl.setOpaque(true);
jl.setFont(new Font("Helvetica", Font.BOLD, 18));
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
panels = new JPanel[]{jp};
jp.setBorder(new LineBorder(Color.black, 3, false));

jp.setPreferredSize(new Dimension(400,200));
jp.add(jl, BorderLayout.NORTH);
for(int i = 0; i < labels.length; i++)
{
labels[i].setHorizontalAlignment(SwingConstants.CENTER);
labels[i].setBorder(BorderFactory.createEtchedBorder());
jp.add(labels[i], BorderLayout.CENTER);
}

jp.setBounds(100, 100, 400,200);
add(jp);
}
}


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 23 2007
Added on Jun 25 2007
3 comments
180 views