• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

GUI with two panels, one of them a GridBagLayout

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I want to have a GUI with two panels: northPanel and southPanel.
And I want the northPanle to be a GridBagLayout. But, I don't know how to add the components to it; only when it's the Frame itself that is set to GridBagLayout.
Anyone has any similar example?
Thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just create a JPanel "northPanel", call
northPanel.setLayout(new GridBagLayout());
and then call
northPanel.add(new Whatever())
using a GridBagConstraints object as needed. When you're done, you can just use
myFrame.getContentPane().add(northPanel, BorderLayout.NORTH);
Does this answer your question?
[ September 01, 2003: Message edited by: Ernest Friedman-Hill ]
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if I understand you, this is what I was trying more or less:

GridBagConstraints constraints = new GridBagConstraints() ;
GridBagLayout gbLayout = new GridBagLayout() ;
Panel northPanel = new Panel() ;
..........
..........
northPanel.setLayout(gbLayout) ;
..........
..........
addComponent( browseButton, 0,0,1,1 ) ;
..........
..........
..........
..........
private void addComponent( Component component, int row, int column,
int width, int height )
{
// set gridx and gridy
constraints.gridx = column ;
constraints.gridy = row ;
// set gridwidth and gridheight
constraints.gridwidth = width ;
constraints.gridheight = height ;
layout.setConstraints( component, constraints ) ;
add( component ) ;
}
***************************************
In your example, how would you add browseButton to the northPanel?
Thanks
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I think I understood you now.
I changed the
private void addComponent(....)
to
private Component addComponent( ... )
and then
northFrame.add(addComponent( browseButton, 0,0,1,1 )) ;
Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your addComponent() method implicitly calls add() on (what I imagine is) your JFrame object -- i.e., an object of the class that all this code is defined in. You could change addComponent() to take another argument, the container to add the component to; then instead of calling just add(), you'd call container.add() -- i.e.,
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Your addComponent() method implicitly calls add() on (what I imagine is) your JFrame object -- i.e., an object of the class that all this code is defined in. You could change addComponent() to take another argument, the container to add the component to; then instead of calling just add(), you'd call container.add() -- i.e.,


Thanks a lot, much better than my solution
 
Tell me how it all turns out. Here is a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic