• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final and layout

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two questions:
1)why:
final int i=100;
byte b=i;
are correct? (if i is not final, compiling error)
2)what will happen if:
comp.setLayout(new FlowLayout());
add... add..
comp.setLayout(new BorderLayout());
Hope someone can help me. Thanks a lot!
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
show more of the code
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since i is final the compiler knows that i will always equal 100. Since 100 is small enough to fit in byte it allows the assignment without a cast.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UR SECOND QUESTION IS NOT CLEAR.
I DID NOT UNDERSTAND WHAT U R ASKING?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When you change the layout, it won't reflect until you call setVisible(true)


Run the program with and without /////, you'll see the diff.
LHS
[This message has been edited by Hungson Le (edited February 01, 2001).]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your second question, you cann't change the layout once you added the components to it. You just set the layout once, before adding the components to it.
 
Hungson Le
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
My code works fine with JBuilder4. Please try it and let me know what you see.
Thanks,
LHS
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quan,
when you reset the layout the components are also reset according to the specifications of the new layout.
deepak, a request. please test your "theory" with some code before making a statement you can't validate with any textual reference. i was pretty sure of my answer but the confidence of your statement made me do a lot of testing and coding, before i could reach the inevitable conclusion. thanks.
 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I tried the following code with JBuilder4:
public class Enter extends Frame {
public Enter(){
setLayout(new FlowLayout());
this.add(new Button("1"));
this.add(new Button("2"));
this.add(new Button("3"));
this.add(new Button("4"));
this.pack();
//whether comment it out or not
//make no differerce to the result
//setVisible(true);
}

public void method()
{
setLayout(new BorderLayout());
pack();
setVisible(true);
}
public static void main(String args )
{
Enter e=new Enter();
e.method();
}
}
what I have seen is
1) a frame with 4 buttons layouting as FlowLayout
2) after call method(), I only see the title bar of the Frame. After enlarge the frame, I see that 4 buttons layouting as FlowLayout.
?? the code shows no effect from the BorderLayout manager. Even if the BorderLayout is taking in charge, how does it figure out the constraints, i.e, NORTH, CENTER
Thank all your answers (also they are different :-)).

Originally posted by Anshuman Acharya:
[B]quan,
when you reset the layout the components are also reset according to the specifications of the new layout.
[B]


 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The frame is always updated with the latest layout. No matter whatever be the layout with which the components are set.

The following code illustrates this. Deepak, please be sure before making a statement. I had to now erase from my memory whatever u told and bring back whatever i thought. During preparation times, lot of ideas creep into our mind and its easy to get confused.
import java.awt.*;
class layoutchange {
public static void main(String[] args){
Frame f = new Frame();
f.setSize(400,400);

f.add(new Button("Button 1"), "South");

f.setVisible(true);

// f.setLayout(new GridLayout(1,2));
f.setLayout(new FlowLayout(FlowLayout.RIGHT));

f.add(new Button("Button 2"));
f.add(new Button("Button 3"));

f.setVisible(true);

}

}

thanks.

 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But see my code above, what if it is FlowLayout first and BorderLayout later.
I think if both of our code works, in different way, the issue may be more complexed and we can not use one program to study its behavior.

Originally posted by natchit:
The frame is always updated with the latest layout. No matter whatever be the layout with which the components are set.


 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I used ur same code and replaced BorderLayout with GridLayout and it does what i said, the latest layout is used.
Check out!!.
class Enter extends Frame {
public Enter(){
setLayout(new FlowLayout());
this.add(new Button("1"));
this.add(new Button("2"));
this.add(new Button("3"));
this.add(new Button("4"));
this.pack();
//whether comment it out or not
//make no differerce to the result
//setVisible(true);
}
public void method()
{
setLayout(new GridLayout(1,2));
pack();
setVisible(true);
}
public static void main(String[] args )
{
Enter e=new Enter();
e.method();
}
}

But!!, why does it behave strangely for BorderLayout. Also, u cannot say that borderlayout is totally ignored. Because, without border layout, just with the flow layout, u can see the four buttons aligned at the center of the top of the frame. But after you give a borderlayout, they shift to the left. So something is happening there!!!
thanks,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic