(Apple Bug ID# 3653709)
Adding a component on top of other components is treated differently in Java 1.4 on the Macintosh than it is in other environments. In the applet above, the following is seen:
The crucial part of the code is the "add(Component, 0)" method, used in many coding examples to add a component on top of all existing components. However, this behavior is not clearly mandated in the Java documentation I've seen.
Is the behavior in Java 1.4 on the Macintosh a bug or is the use of "add(Component, 0)" venturing into gray areas in which the Macintosh Java Virtual Machine 1.4 should be free to be different from all other implementations, including all previous Macintosh implementations up to 1.3.x? Are there other better ways to place a component on top of other components?
The button is meant to represent components one might want to add on top of others, such as ToolTip/BubbleHelp or a custom Choice-like drop-down component.
If you have any insights, workarounds or comments about this test page please contact Mickey Segal. A listing of many Java resources is at this link.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AddOrder2 extends Applet {
public void init()
{
setBackground(new Color(225,225, 255));
try
{
FrameForApplet frameForApplet = new
FrameForApplet("A frame");
}
catch (Throwable e)
{
System.out.println("Can't use Frame:
" + e);
return;
}
}
public void paint(Graphics g)
{
g.drawString("A Frame appears with some components", 10 ,
25);
}
} // END OF Class AddOrder2
final class FrameForApplet extends Frame implements WindowListener {
DashHolder dashHolder;
static Frame frame;
FrameForApplet(String s)
{
super(s);
setLayout(new FlowLayout());
frame = this;
setBounds(400, 100, 400, 150);
dashHolder = new DashHolder(100, 40);
add(dashHolder);
show();
dashHolder.addTopComponent();
addWindowListener(this);
}
public void windowOpened(WindowEvent we){}
public void windowClosed(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
public void windowActivated(WindowEvent we){}
public void windowDeactivated(WindowEvent we){}
public void windowClosing(WindowEvent we)
{
if (we.getSource() == this)
{
setVisible(false);
dispose();
}
}
} // END OF Class FrameForApplet
class DashHolder extends Panel {
Button button;
int height = 0;
int width = 0;
DashHolder(int width, int height)
{
this.width = width;
this.height = height;
setBackground(Color.yellow);
}
final void addTopComponent()
{
button = new Button("Button");
FrameForApplet.frame.add(button, 0);
button.setSize(button.getPreferredSize());
button.setLocation(230, 35);
}
public final Dimension getMinimumSize()
{
return(new Dimension(width, height));
}
public final Dimension getPreferredSize()
{
return(getMinimumSize());
}
} // END OF Class DashHolder