Results 1 to 5 of 5
  1. #1
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default resizeable panels

    Hi i am trying to do this,( sorry for crappy diagram :) )

    __________________________
    |-------------------|--------|
    |-------------------|--------|
    |----graph-panel----|-button-|
    |-------------------|-panel--|
    |-------------------|--------|
    |-------------------|--------|
    |-------------------|--------|
    __________________________

    and have the graph panel take 75% of the parent JFrame and the button panel
    take up 25 %

    i am attempting to use a gridBagLayout with the code below

    Java Code:
    public class DynamicGraphFrame extends JFrame implements ActionListener,WindowListener{
    	
    	
    	private Container ContentPane;
    	private Point FrameCoOrdinates;
    	private Dimension FrameDimension;
    	private String FrameName;
    	private JFrame ref = this;
    	private GridBagLayout GBL;
    	
    
    
    	public DynamicGraphFrame(String FrameName,Point FrameCoOrdinates,Dimension frameDimension)
    	{
    		this.FrameCoOrdinates = FrameCoOrdinates;
    		this.FrameDimension = frameDimension;
    		this.FrameName = FrameName;
    		this.ContentPane = this.getContentPane();
    		// set the layout
    		//ContentPane.setLayout(new FlowLayout());
    		
    		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    		this.addWindowListener(this);
    		// set up default frame position and coordinates
    		setFrameBounds();
    		this.setFrameName(FrameName);
    		//set the pack
    		GBL = new GridBagLayout();
    		ContentPane.setLayout(GBL);
    		GridBagConstraints gbc = new GridBagConstraints();
    	
    
    		
    		
    		this.addFrameSizeListener();
    		gbc.fill = GridBagConstraints.PAGE_START;
    
    		gbc.gridx = 0;
    		gbc.gridy = 0;
    		gbc.ipady = 100;
    		gbc.ipadx = 100;
    		gbc.gridwidth = 2;
    		gbc.gridheight =1;
    
    
    	        ContentPane.add(this.GraphPanel(),gbc);
                    
                    // set the constraints
    	        gbc.fill = GridBagConstraints.PAGE_END;
    		gbc.gridx = 0;
    		gbc.gridy = 1;
    		gbc.gridwidth = 1;
    		gbc.gridheight =1;
    	       
                    ContentPane.add(this.buttonPanel(),gbc);
    	
    		//this.pack();
    		this.setVisible(true);
    		
    		
    			
    	}
    	
    	
    	// set up default frame position and coordinates
    	
    	
    	public JPanel GraphPanel()
    	{
    		JPanel graphPanel = new JPanel();
    		graphPanel.setBackground(Color.black);
    		//graphPanel.setPreferredSize(new Dimension (50,50));
    		graphPanel.setBounds(20,50,100,100);
    		
    			
    		return graphPanel;
    	}
    	
    	
    	public JPanel buttonPanel()
    	{
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setBackground(Color.red);
    		buttonPanel.setBounds(100,20,100,100);
    		
    			
    		return buttonPanel;
    	}
    	
    	
    	etc...
    - is this that the layout managers will only start adjusting sizes when components ( ie buttons, labels etc) are added....

    - or ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please read up on the different layouts at the Sun layout tutorial. setBounds has no place when using layout managers. Myself, I avoid GridBagLayout unless absolutely necessary. Better here would be perhaps to use a BorderLayout with the graph panel BorderLayout.CENTER and the button panel BorderLayout.EAST.

    Does the Button Panel have to be exactly 1/4 the width of the Graph panel, or can they be in that position, but sized as they naturally prefer? What is your ultimate goal for this layout and why?
    Last edited by Fubarable; 07-18-2009 at 02:23 PM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DGF extends JFrame {
        JPanel left;
        JPanel right;
    
        public DGF(String name, Point coords, Dimension size)
        {
            super(name);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            // set frame position and size
            setSize(size);
            this.setLocation(coords);
    
            Container contentPane = getContentPane();
            contentPane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            gbc.weighty = 1.0;
    
            gbc.fill = //GridBagConstraints.PAGE_START;
                       GridBagConstraints.BOTH;
    
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 0.5;
    //        gbc.ipady = 100;
    //        gbc.ipadx = 100;
    //        gbc.gridwidth = 2;
    //        gbc.gridheight = 1;
    
            contentPane.add(left = getGraphPanel(), gbc);
    
            // set the constraints
    //        gbc.fill = GridBagConstraints.PAGE_END;
            gbc.gridx = //0;
                        1;
            gbc.gridy = //1;
                        0;
            gbc.weightx = 0.15;
    //        gbc.gridwidth = 1;
    //        gbc.gridheight = 1;
    
            contentPane.add(right = getButtonPanel(), gbc);
    
            //this.pack();
            this.setVisible(true);
        }
    
        public JPanel getGraphPanel()
        {
            JPanel graphPanel = new JPanel();
            graphPanel.setBackground(Color.blue);
    //        graphPanel.setPreferredSize(new Dimension(50,50));
    //        graphPanel.setBounds(20,50,100,100);
            return graphPanel;
        }
    
        public JPanel getButtonPanel()
        {
            JPanel buttonPanel = new JPanel();
            buttonPanel.setBackground(Color.red);
    //        buttonPanel.setBounds(100,20,100,100);
    //        buttonPanel.setPreferredSize(new Dimension(100, 100));
            return buttonPanel;
        }
    
        public static void main(String[] args) {
            DGF test = new DGF("Layout Test", new Point(100, 100),
                                new Dimension(400, 400));
            test.addComponentListener(test.cl);
        }
    
        private ComponentAdapter cl = new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                int leftWidth = left.getWidth();
                int rightWidth = right.getWidth();
                double total = leftWidth + rightWidth;
                System.out.printf("leftWidth = %d  %.1f%%  " +
                                  "rightWidth = %d  %.1f%%%n",
                                   leftWidth, 100*(leftWidth/total),
                                   rightWidth, 100*(rightWidth/total));
    
            }
        };
    }

  4. #4
    josemoncas's Avatar
    josemoncas is offline Member
    Join Date
    Jul 2009
    Location
    Madrid (Spain)
    Posts
    11
    Rep Power
    0

    Default Use BorderLayout

    For the layout your are showing us, the best layout manager to use is BorderLayout:

    Use BorderLayout.CENTER for the graph panel and BorderLayout.EAST for the button panel.

    GridBagLayout is a very powerful layout but I don't see its usefulness in this simple case.

    Hope it helps.
    The web that offers Java utilities (classes) which make programming Swing applications an easier task: JMC Java utilities

  5. #5
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    thanks very much to all the contributors

    and just quietly and embarrassingly, i only post when i get stuck, i have read
    the sun tutorials a number of times :o but clearly need to read again :D what tends to happen is that you
    try something, gain a little context but then somehow in the iterations you misunderstand something ... and viola :confused: ... and i guess i was trying
    to just master one layout manager for all occasions...

    The idea here was to have a graph pane which displays a graph on adjustable axis, so that when you adjust the size of the parent pane, the axis adjust ...
    done ...
    on the right hand side i have a panel of buttons so that the user can
    set things like colour, max value for axis etc... done ...

    thanks very much to hardwired for the code help :)

    i wasn't using the weighty aspects of the grdibagconstraints at all and after
    revisiting the reading ... i think i've got a better handle on things

    i guess the conceptual part i wasn't getting was that your'e only really setting the preffered size for the smallest components ... textbox, label etc..


    thanks again to all who contributed
    very helpful and appreciated :)

Similar Threads

  1. Spliting the Panel to three panels
    By suraw in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 07:05 PM
  2. images, panels and applets
    By jamesfrize in forum Java Applets
    Replies: 3
    Last Post: 03-20-2008, 05:35 PM
  3. Buttons to show new panels
    By Lehane_9 in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 05:22 PM
  4. Working with Labels on Panels.
    By vargihate in forum AWT / Swing
    Replies: 2
    Last Post: 01-04-2008, 05:09 AM
  5. Need a tutorial for Studying about Panels
    By ramachandran in forum New To Java
    Replies: 1
    Last Post: 10-25-2007, 10:05 AM

Posting Permissions

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