(classes are in java.awt package, unless otherwise specified) |
(classes are in com.sun.java.swing, unless otherwise specified) |
Button, Component, Dialog, Frame, Label, List, Menu, MenuBar, MenuItem, Panel, PopupMenu, ScrollPane, TextArea, TextField, Window | prefix with "J" |
Canvas | JPanel |
Checkbox, CheckboxMenuItem | JCheckBox, JCheckBoxMenuItem |
Choice | JComboBox |
FileDialog | com.sun.java.swing.preview.JFileChooser (Swing 1.0.x) |
Scrollbar | JScrollBar |
java.applet.Applet | com.sun.java.swing.JApplet |
frame.getContentPane().setLayout(new
BorderLayout()); // Swinging code
frame.getContentPane().add("Center",
new TextArea()); // Swinging code
public void paint(Graphics g) {
g.setColor(getForeground());
// swinging code
g.drawString("this", 10, 10);
transient Thread animation = null;
// serializable code
public void stop() {
animation.stop();
animation = null;
}
public void start() {
animation = new AnimationThread();
animation.start();
}
// replace with the following, serializable code:
transient Image image = Toolkit.getDefaultToolkit().getImage("anImage.gif");
//.... some more code
public void paint(Graphics g) {
if (image == null)
image = Toolkit.getDefaultToolkit().getImage("anImage.gif");
g.drawImage(image, 0, 0, null);
}
// Not serializable:
public void
paint(Graphics g) {
if (offImage == null) {
offImage = createImage(size().width, size().height);
offGraphics = offImage.getGraphics();
}
}
// Serializable
// ...
transient
Image offImage = null;
transient
Graphics offGraphics = null;
// ...
public void paint(Graphics
g) {
if
((offImage
== null) || (offGraphics == null)) {
offImage = createImage(size().width, size().height);
offGraphics = offImage.getGraphics();
}
}
public static Frame getParentFrame(Component comp) {
Component possibleParentFrame = comp;
while ( (possibleParentFrame != null) &&
!(possibleParentFrame
instanceof Frame)) {
possibleParentFrame
= possibleParentFrame.getParent();
}
return (Frame)possibleParentFrame;
}
JDialog dialog = new JDialog(parentFrame);
// remove the parentFrame variable, and just use this instead:
JDialog dialog = new JDialog(getParentFrame());
private void readObject(java.io.ObjectInputStream stream)
// re-add the observer
throws java.io.IOException, ClassNotFoundException
{
stream.defaultReadObject();
aObservable.addObserver(this);
}