An inner class example - extending JPanel and impl

chris (2004-05-28 13:22:57)
1809 views
0 replies
I am finding inner classes to be more and more useful these days - they do have their dangers, most notably that it's easy to accidently modify the parent class' memebers instead of the inner class. However this does come with the counter-benefit of easy access to the parent memebers, which can speed up development and make sources easier to read. I would not recommend using inner classes for anything which you intend to immitated elsewhere in an application. External classes are the obvious solution for anything that might be repeated later on. So let's consider the inner class as a sneaky means of potentially accelerating development and opening up protected members without having to pass around references to the parent class all the time, or having to write loads of getter and setter methods just for the sake of a small feature, say in a user interface.

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
comment