How do you open a popup window from JApplet?
I can't seem to get this to work, and I got the code directly from here: http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html#sizeplace
The JApplet window starts fine, and draws the test pattern correctly, so I know that part of the code is fine. But I can't get the same test pattern to appear in a separate popup window. (FYI my real app needs a separate window because it's size is too big to fit in the webpage...)
publicclass DrawTestPatternextends JApplet{
private JPanel jContentPane1 =null;
publicvoid init(){
this.setSize(new Dimension(TestPattern.size*TestPattern.cols, TestPattern.size*TestPattern.channelRows*4));
this.setContentPane(getJContentPane1());
//1. Create the popup.
JFrame popup =new JFrame("popupDemo");
//JDialog popup = new JDialog();
//2. Optional: What happens when the popup closes?
popup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the popup.
//...create emptyLabel...
popup.getContentPane().add(new JLabel("Hello World"), BorderLayout.CENTER);
//4. Size the popup.
//popup.pack();
popup.setSize(new Dimension(TestPattern.size*TestPattern.cols, TestPattern.size*TestPattern.channelRows*4));
popup.setContentPane(getJContentPane1());
popup.setLocation(100,100);
popup.setAlwaysOnTop(true);
popup.setBackground(Color.GREEN);
//5. Show it.
popup.setVisible(true);
}
[2319 byte] By [
mixersofta] at [2007-11-27 8:12:46]

// <applet code="DTP" width="100" height="100"></applet>
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class DTP extends JApplet {
BufferedImage image;
public void init() {
createImage();
JPanel panel = getContent();
setSize(panel.getPreferredSize());
setContentPane(panel);
showDialog();
}
private void showDialog() {
JDialog popup = new JDialog(new Frame());
popup.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
popup.getContentPane().setBackground(Color.GREEN);
popup.getContentPane().add(new JLabel("Hello World"), "First");
popup.getContentPane().add(getContent());
popup.setLocation(100,100);
// java.security.AccessControlException:
//access denied (java.awt.AWTPermission setWindowAlwaysOnTop)
//popup.setAlwaysOnTop(true);
popup.pack();
popup.setVisible(true);
}
private JPanel getContent() {
return new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - image.getWidth())/2;
int y = (getHeight() - image.getHeight())/2;
g.drawImage(image, x, y, this);
}
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
};
}
private void createImage() {
int w = 200, h = 100;
int type = BufferedImage.TYPE_INT_RGB;
image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setPaint(new GradientPaint(0, 0, Color.red,
w, h, Color.green.darker()));
g2.fillRect(0,0,w,h);
g2.dispose();
}
}