Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2003, 09:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default ItemListener problem.

I am pretty new to Java, so there is probably a simple answer to this one...

The thing is that I have made a class that extends the JPanel class, and I wanted to make it as broad as abstract as possible...
Code:
class JComponentPanel extends JPanel
{
    public JComponentPanel(JComponent[] coms, EventListener[] listener, int alignment, int[] mnemonic, int fill1, int fill2)
    {
        setLayout(new FlowLayout(alignment, fill1, fill2));
        for(int i = 0; i < coms.length; i++)
        {
            if(coms[i].getClass().toString().equals("class javax.swing.JCheckbox"))
            {
                ((JCheckBox)coms[i]).setMnemonic(mnemonic[i]);
                ((JCheckBox)coms[i]).addItemListener((ItemListener)listener[i]);
            }
            if(coms[i].getClass().toString().equals("class javax.swing.JButton"))
            {
                ((JButton)coms[i]).setMnemonic(mnemonic[i]);
                ((JButton)coms[i]).addActionListener((ActionListener)listener[i]);
            }
            add(coms[i]);
        }
    }
}
My first problem was to figure out which type of JComponent I had to process, and I fixed this using the getClass method, which seems kindof a hack. Is there another way to do this!?

Then my other problem is the assignement of Listeners. Since I am passing an EventListener array to the constructor, I have to typecast the listeners before adding them. This is actually working for the ActionListener, but not for the ItemListener...!? Why!?

The setMnemonic does not work for the JCheckBox either!

Hope you can help!? Thanks!

Jacob.


__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old February 10th, 2004, 01:43 AM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In fact it is simple. The listener is of type EventListener, EventListener is a superclass, or actually a super interface of ActionListener. But ItemListener is in a totally different package, and don't inherit from EventListener, so can't cast it. Maybe you can do it by giving the listener the type of Object, then maybe it is possible to downcast it.
Rune

 
Old March 20th, 2004, 06:00 PM
Registered User
 
Join Date: Mar 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

no the actionlistener and itemlistener are in same package and they are inherited from eventlistener
but i didnt find the problem in your code if you can specify how it not working,
is it compile time error or runtime error.

fo the first point you can use instrancof to know the type ofclass
coms[i] instanceof JCheckbox
or you can use getClass().getNamr()



GA









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.