And that's what I needed to add today - just a small transparent gif on a non-opaque JPanel in the corner of my applet, which when clicked on would push up a browser window with some help content.
Here's just a snipped of how it's done:
public class MyApplet extends JApplet implements ActionListener,Runnable,MouseListener {
Image helpImage;
...
public void start() {
...
Canvas c = new Canvas(this); // don't worry about this. I'm just setting up a background
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
// set up container on which to place components
Container b = getContentPane();
b.setLayout(new GridBagLayout());
GridBagConstraints canvasConstraints = new GridBagConstraints();
canvasConstraints.fill = GridBagConstraints.BOTH;
canvasConstraints.weighty=0.1;
canvasConstraints.weightx=0.1;
b.add(c,canvasConstraints);
...
/* inner class for ImageIcon on a JPanel implementing MouseListener */
class HelpPanel extends JPanel implements MouseListener{
public HelpPanel(){
super.setOpaque(false);
super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
helpImage=getImage(getCodeBase(), "help.png");
ImageIcon ii = new ImageIcon(helpImage);
JLabel iconLabel = new JLabel(ii);
iconLabel.setOpaque(false);
addMouseListener(this);
add(iconLabel,BorderLayout.NORTH);
setSize(new Dimension(300,40));
setPreferredSize(new Dimension(300,40));
setAlignmentX(Component.LEFT_ALIGNMENT);
}
public void mousePressed(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
// launch help secreen
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"instructions.html"),"_blank");
}
catch (Exception ex) {}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
HelpPanel helpPanel = new HelpPanel();
c.add(helpPanel);
....
// rest of applet start() method
}
}
So you can see that I have included all the required methods for the MouseListner interface - this is required. I have also used the 'super' keyword to access parent class (in the inheritance heirarchy) methods - in this case to call setOpaque(boolean option) and setLayout(Layout l) on the JPanel. Very simple, nicely contained and the inner class takes only 20 or so lines of code.
enjoy,
christo