-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.2.0
-
sparc
-
solaris_2.5.1
Name: akC57697 Date: 06/22/98
The JDialog.setLocationRelativeTo() works unstable under appletviewer.
The doc says:
"
public void setLocationRelativeTo(Component c)
Sets the location of the dialog relative to the specified
component. If the component is not currently showing, the
dialog is centered on the screen.
@param c the component in relation to which the dialog's location
is determined
"
The example:
------------------------------8-<--------------------------------------------
import com.sun.java.swing.JFrame;
import com.sun.java.swing.JDialog;
import com.sun.java.swing.JPanel;
import java.applet.Applet;
import java.awt.Container;
import java.awt.Point;
import java.awt.Toolkit;
public class appletTest extends Applet{
public void start() {
int testsX[]={0
,100
,200};
int testsY[]={0
,100
,200};
Point results[]={new Point(100,100)
,new Point(200,200)
,new Point(300,300)
};
JFrame f=null;
JPanel p=null;
JDialog c=null;
for (int nRun=0; nRun<=100; nRun++) { // Repeat 100 times
f= new JFrame();
p=new JPanel();
int xpos=0;
int ypos=0;
f.getContentPane().add(p);
f.setSize(300,300);
f.setLocation(0,0);
c=new JDialog(f); // Create JDialog object
c.setSize(100,100);
try {
// test for invisible component
c.setLocationRelativeTo(p); // set location
if(!( c.getLocation().x==
(Toolkit.getDefaultToolkit().getScreenSize().width/2 - 50)
&& c.getLocation().y==
(Toolkit.getDefaultToolkit().getScreenSize().height/2 - 50))) // verify results
System.out.println("Method set incorrect location");
// test for visible component
f.setVisible(true);
c.setVisible(true);
Container parent=c.getParent();
while (!c.isShowing() || !parent.isShowing()) {}; // Wait for visible
for(int i=0;i<testsX.length;i++) {
f.setLocation(testsX[i],testsY[i]);
while (!c.isShowing() || !parent.isShowing()) {}; // Wait
c.setLocationRelativeTo(p); // set location
xpos=c.getLocation().x;
ypos=c.getLocation().y;
if(!(Math.abs(xpos-results[i].x)<100
&& Math.abs(ypos-results[i].y)<100)) {// verify results
System.out.println("Location is "+c.getLocation()+
". Must be "+results[i]);
System.out.println("Bounds:"+c.getBounds());
System.out.println("Size:"+p.getSize());
System.out.println("Location:"+p.getLocationOnScreen());
f.dispose();
System.out.println("Method sets incorrect location");
System.exit(0);
}
}
} catch(Throwable e) { // Test no exceptions thrown
c.setVisible(false);
f.setVisible(false);
System.out.println( "Unexpected "+e+" thrown" );
System.exit(0);
}
System.out.println("The loop N "+nRun+" passed.");
f.dispose();
} // for nRun
f.dispose();
System.out.println( "OKAY" );
System.exit(0);
}
}
appletTest.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.X//EN">
<!--SCCS: 1.7 96/08/06-->
<html>
<head>
<title>JDialog setLocationRelativeTo() test</title>
</head>
<body>
<h1>appletTest</h1>
<p> <hr>
<applet
code=appletTest
codebase="."
width=300 height=300
>
<param name=paramFile value="">
[[You need a Java-aware browser to see the JavaTest applet.]]
</applet>
<hr>
<!-- Created: Sun Jul 14 12:53:46 PDT 1996 -->
<!-- hhmts start -->
Last modified: Thu Aug 1 14:39:48 PDT
<!-- hhmts end -->
</body>
</html>
---------------------------->-8----------------------------------------------
Output:
java -ms8m -mx25m -Xverify:all sun.applet.AppletViewer http://spb:8080/~kuzm/appletTest.html
Swing: checked access to system event queue.
The loop N 0 passed.
The loop N 1 passed.
The loop N 2 passed.
The loop N 3 passed.
The loop N 4 passed.
The loop N 5 passed.
Location is java.awt.Point[x=200,y=228]. Must be java.awt.Point[x=300,y=300]
Bounds:java.awt.Rectangle[x=200,y=228,width=100,height=83]
Size:java.awt.Dimension[width=290,height=250]
Location:java.awt.Point[x=205,y=245]
Method sets incorrect location
java full version "JDK-1.2beta4-J"
======================================================================
- duplicates
-
JDK-4153446 setLocationRelativeTo can fail if invoked soon after setLocation()
-
- Resolved
-