Results 1 to 5 of 5
Thread: resizeable panels
- 07-18-2009, 11:28 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
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
- is this that the layout managers will only start adjusting sizes when components ( ie buttons, labels etc) are added....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...
- or ?
-
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 01:23 PM.
- 07-18-2009, 05:42 PM #3
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)); } }; }
- 07-28-2009, 01:00 PM #4
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
- 07-29-2009, 01:45 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
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
-
Spliting the Panel to three panels
By suraw in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:05 PM -
images, panels and applets
By jamesfrize in forum Java AppletsReplies: 3Last Post: 03-20-2008, 04:35 PM -
Buttons to show new panels
By Lehane_9 in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:22 PM -
Working with Labels on Panels.
By vargihate in forum AWT / SwingReplies: 2Last Post: 01-04-2008, 04:09 AM -
Need a tutorial for Studying about Panels
By ramachandran in forum New To JavaReplies: 1Last Post: 10-25-2007, 09:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks