aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes From JONH hunts mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "From JONH hunts mock exam " Watch "From JONH hunts mock exam " New topic
Author

From JONH hunts mock exam

sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:
1 paint()
2 repaint()
3 paint(Graphics)
4 update(Graphics)
5 None � you should never cause paint(Graphics) to execute
the ans was 2 , but i guess shouldnt be 1.

//code 2
import java.awt.*;

public class FrameTest extends Frame {
public FrameTest() {
add (new Button("First"));
add (new Button("Second"));
add (new Button("Third"));
pack();
setVisible(true);
}
public static void main(String args []) {
new FrameTest();
}
}

IN the above code there is no change if i remove pack. what is pack meant for. and also it prints "third" which occupies the frame size why?
plz help thanks in advance
------------------
"Winners don't do different things
They do things differently"


"Winners don't do different things<br /> They do things differently"
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Hi sunil,
Regarding your first question, I would like to say that the correct answer is 2. In most of the cases we want to clear the drawing area and draw afresh. The repaint() will call update() which will sets the background color of drawing region(clears the drawing area) and calls paint().
regarding the second question,
pack() is to resize the frame so that it occupies the appropriate size in which the components can be fit in.
The default layout of Frame is BorderLayout.
And in this layout policy, adding components to the Frame
without specifying the orientation results in the components added at the center. In your code you are adding three buttons without specifying the orientation, so all the buttons are added at center and third button is visible since it replaces all the previously added buttons in the center.
Hope this clears your doubt.
Regards
------
vadiraj.
------------------
*************************
There's a lot of I in J.
*************************


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
Thanks vadiraj
But in second case even if i dont specify pack it eould adjust 2 the specified size since it is a frame uses border layout as default. can u tell me some example where can i look at the use od pack() command?
thanks,
sunil.s
------------------
"Winners don't do different things
They do things differently"
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Hi sunilkumar,
As BorderLayout doesn't consider the component's preferred size, pack()(and other methods like- setBounds(), setSize()) will not have any effect.
Change the layout of a Frame to FlowLayout which will take
the component's preferred size into account.
The following code demonstrates the use of pack().
The method pack() is useful in cases you want to set the container size exactly required by the components.
Try commenting/de-commenting the lines 1 and 2 and run the program.
You'll notice the difference.
Code
--------------
<PRE>

import java.awt.*;
public class PackDemo extends Frame
{
Label lUName,lPwd;
TextField tUName,tPwd;
Button bOk,bCancel;
public PackDemo()
{
lUName = new Label("Username:");
lPwd = new Label("Password:");
tUName = new TextField(20);
tPwd = new TextField(20);
bOk = new Button("OK");
bCancel = new Button("Cancel");
setLayout(new FlowLayout()); // change layout to FlowLayout.

//add all the components to Frame.
add(lUName);add(tUName);add(lPwd);add(tPwd);add(bOk);add(bCancel);
}

public static void main(String a[])
{
PackDemo p = new PackDemo();
//p.setSize(500,500); // line 1.
p.pack(); // line 2.
p.setVisible(true);
}
}

</PRE>
---------------
Hope this clears your doubt.
Regards
--------
vadiraj

------------------
*************************
There's a lot of I in J.
*************************
[This message has been edited by vadiraj vd (edited December 21, 2000).]
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
thanks vadiraj that made me clear where pack has 2 be used
------------------
"Winners don't do different things
They do things differently"
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
To go back and elaborate on your first question,
repaint() is the preferred way to call paint() because repaint() calls update which schedules the time to call paint() to the screen. Calling paint() directly or calling udpate() directly is more inefficient and since the question said what is the APPROPRIATE way, repaint() is the correct answer.
Bill
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
Vadiraj,

Thanks for a very nice example. Can u please tell me which all layout managers are affected when the pack() method is used. ???
Does pack() only work where the layoutmanagers "honors the preferred size of its components". ?
Thanks,
Aruna
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: From JONH hunts mock exam
 
Similar Threads
pack()
Frame
Layout
actual GUI question on a job technical interview
awt - what does pack( ) do here?