Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Layout mock question

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-----------------------------------------------
one general question: what do methods validate() and pack() do ? even if they are not used i don't see any change in the GUI.
-----------------------------------------------
in this question, any unallotted space is taken or given to CENTER. is that correct ?
now question is as follows:
A frame uses BorderLayout Management and has components added to all the regions. On resizing the Frame some space becomes available. The space is alloted to the regions, in which Order of preference?

North , South, West, East and then Center.
North , West, South, Center and then Center.
Center, East, West, South and then North.
West, Center, South, North and then East.
------------------------------------------------
in this question the layout is set to be Flow and then components are added. and then layout is changed to Border. but BorderLayout needs that components be specifically allotted to their 5 respective positions, which if not, nothing is visible. Hence nothing is visible. is the reasoning correct ?
import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
------------------------------------------------
[ February 02, 2002: Message edited by: mark stone ]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just posting again. no responses yet....

Originally posted by mark stone:
-----------------------------------------------
one general question: what do methods validate() and pack() do ? even if they are not used i don't see any change in the GUI.
-----------------------------------------------
in this question, any unallotted space is taken or given to CENTER. is that correct ?
now question is as follows:
A frame uses BorderLayout Management and has components added to all the regions. On resizing the Frame some space becomes available. The space is alloted to the regions, in which Order of preference?

North , South, West, East and then Center.
North , West, South, Center and then Center.
Center, East, West, South and then North.
West, Center, South, North and then East.
------------------------------------------------
in this question the layout is set to be Flow and then components are added. and then layout is changed to Border. but BorderLayout needs that components be specifically allotted to their 5 respective positions, which if not, nothing is visible. Hence nothing is visible. is the reasoning correct ?
import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
------------------------------------------------
[ February 02, 2002: Message edited by: mark stone ]

 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by mark stone:
-----------------------------------------------
one general question: what do methods validate() and pack() do ? even if they are not used i don't see any change in the GUI.
[ February 02, 2002: Message edited by: mark stone ]


well, the two methods have almost the same idea behind them.
after you add components to a container u need to tell the layout manager to verufy that they "are there" - meaning to make sure he got al those components u added and to rearrange them in his layout manner.
putting validate() or pack() just means to make sure all the added components are really "validated" in the container u put them into. (usually in the first time u constuct a frame that doesnt really matter as it happens anyway BUT if during your program u add additional components to the container u need to use validate to force the layout manager to put them in the container) - if u dont u will have to resize the window in order to see them.
pack() does the same as validate() but adds something more:
pack() also set teh bounds of the window accroding to the prefered sizes of all the components inside. thus if u use pack, u cant use the methods setBounds() or setSize() as the program ignores them.
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pack() method is used for adjusting the container
size as the preffered size of the components in it.
so no need to call setSize() method for making components visible
got it?
changing layout questien is sound.i also need answer ?? AWT geniuses are invited.
have a nice day mark..
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi basha. there is an answer for these questions
here
and here
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u ami
 
Hot dog! An advertiser loves us THIS much:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic