Results 1 to 4 of 4
  1. #1
    babis is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default JFrame resize control

    Hi
    I want to have a control over the user resize in a JFrame. For example i want width will always 300 pixel higher than height. I try this code :

    Java Code:
    public class Test extends JFrame
    {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private static int con = 300;
    
    	/**
    	 * This is the default constructor
    	 */
    	public Test()
    	{
    		super();
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize()
    	{
    		this.setSize(500, 200);
    		this.setContentPane(getJContentPane());
    		this.setTitle("Test");
    		
    		this.addComponentListener(new ComponentAdapter()
    		{
    			public void componentResized(ComponentEvent e)
    			{
    				resizeControl();
    			}
    		});
    
    		this.setVisible(true);
    	}
    
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane()
    	{
    		if (jContentPane == null)
    		{
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new BorderLayout());
    		}
    		return jContentPane;
    	}
    
    	
    	protected void resizeControl()
    	{
    		int w = this.getWidth();
    		int h = this.getHeight();
    		if(w > h+con)
    		{
    			this.setSize(h+300, h);
    		}else
    		{
    			this.setSize(w, w-con);
    		}
    	}
    }
    This code is not doing resize until the user did some other action, like moving the JFrame. Is any solution to this?

    Thank you in advance
    Charalampos Alexopoulos

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    What happens when you override the setBounds( ... ) methods and call the super implementation with (optionally) adjusted values?

    kind regards,

    Jos

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    This code is not doing resize until the user did some other action, like moving the JFrame. Is any solution to this?
    Your code works fine for me. It does not allow increasing teh size of the JFrame except by dragging the corner (ie increasing width and height simultaneously). When reducing either width or height, the other gets reduced correspondingly.

    db

  4. #4
    Bulelakes is offline Member
    Join Date
    Jul 2010
    Posts
    32
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. how to resize an image
    By marodia in forum AWT / Swing
    Replies: 2
    Last Post: 08-21-2009, 04:10 AM
  3. Image Resize
    By jithan in forum New To Java
    Replies: 4
    Last Post: 11-07-2008, 09:26 AM
  4. control app width based on certain control
    By thebillybobjr in forum SWT / JFace
    Replies: 0
    Last Post: 05-15-2008, 04:52 PM
  5. Resize frame
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 11:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •