• 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

About GridLayout

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends
there is another question from Mock Exam
24)
Assuming the code below is part of a Frame subclass, which are true?

1.public LayoutFrame() {
2. setLayout(new GridLayout(1, 3));
3. Button b = new Button("What is b location?");
4. Button a = new Button("What is a location?");
5. Panel p1 = new Panel();
6. p1.setLayout(new GridLayout(2, 1));
7. p1.add(a);
8. p1.add(b);
9. add(p1);
10. }
a) There is a compile error since the superclass constructor is not called.

b) Two push buttons are added, each just larger than the size of the label and located in the center of the Frame.

c) Two push buttons are added. Each is half the width of the Frame and the entire length vertically, with a above b.

d) Two push buttons are added. Each is half the size of the entire Frame vertically and the entire width, with a above b.

e) There is a compile error since there is no receiving object specified
It s correct answer is d
but if i comment line 2 then it gives sam output and when i comment line 6 then it shows same two buttons not occupying the entire frame.Can any one explain this strange behaviour
i.e. what is the use of line 2
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


but if i comment line 2 then it gives sam output and when i comment line 6 then it shows same two buttons not occupying the entire frame.Can any one explain this strange behaviour
i.e. what is the use of line 2


In the original code the layout is redefined in line 6 as GridLayout(2,1). It means a Gridlayout with 2 rows and 1 column. The system always treats the latest layout defined as the current one and hence U see an output defined in D. So line 2 has no effect on the code ans is as good as commenting it out.
If U comment out line 6, the Gridlayout defined in line 2 with 1 row and 3 columns takes effect and the result is obviously different because of different rows and columns. The third column is unused and hence the two buttons do not fill out the entire frame.
Remember the latest Layout definition is the one that is used prior to adding of the components.
regds
Ajay Kumar
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if u comment line 6
the panel will have its own default layout -flowlayout
the buttons will appear horizontally with size of just of label.
the panel layed in gridlayout of frame.
but if u un comment line 6 the panel will have the grid layout instead of default layout. in the panel two buttons will layed as gridlayout.so the buttons will be larger.
panel itself layed in the frame again gridlayut.
i hope it is clear.\
suresh
 
I'm full of tinier men! And 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