There is an instanceof operator which you can use to check if cls is an
instanceof JButton or JLabel etc. and accordingly add listeners to the
JComponent by typecasting it to JButton or JLabel. One point which should be
taken care of is that the cls should be typecast to JButton only if cls
instanceof JButton returns true otherwise it is going to give
classcastException.
I hope this helps.
regards
arpna
> -----Original Message-----
> From: ravi kanth [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, February 22, 2001 4:16 PM
> To: [EMAIL PROTECTED]
> Subject: Attaching a listener to a JComponent created at runtime
>
>
> I am having a JDialog created when a menu item is selected in
> a Frame (Simple Scenario). One parameter in the constructor
> of the Dialog is a Class class parameter.
> Within the dialog constructor, I know only that the Class
> class parameter is a JComponent. So it can be a JButton,
> JComboBox, JRadioButton.... etc. etc.
>
> Now when I create the component inside the dialog's
> constructor, how would I determine if it a JButton,
> or JCombobox or JRadioButton.... etc... etc.. so that I can
> attach the proper listner.
>
> This is the general look of my JDialog....
>
> public class myDialog extends JDialog implements
> ActionListner, ItemListner (Other listners if required)
> {
>
> public myDialog(JFrame parent, Class cls)
> {
> //at this point I know that for sure (from outside)
> //that when the call for the costructor comes, Class
> //cls is going to be a JComponent
>
> .........
> //So then I create the JComponent like this...
> JComponent component = cls.newInstance();
> component.setVisible(true);
>
> ....
> //Well when I run the program (full scale completion)
> //I can see the component is created. I tested with a
> //JButton, and a JRadioButton. But the question is
> //I have to attach the proper listener. How would I
> //know inside here (constructor of the dialog) whether
> //it is a JButton or JRadio or JCheckbox.... etc..etc.
> //to add the proper listener? Further if it is a
> //component which requires a text string attached
> //it like in a button or in a RadioButton, I cannot
> //use the setText() unless if know for sure the
> //setText method is in the subclass. I tried to
> //downcast from JComponent. I am getting a
> //ClassCastException.
> }
> }
> Hope U would throw some ligth on this.
>
> _____________________________________________________
> Chat with your friends as soon as they come online. Get Rediff Bol at
> http://bol.rediff.com
>
>
>
>
> _______________________________________________
> Advanced-swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/advanced-swing
>
----------------------------------------------------------------------------
Visit us at http://www.canon-asia.com
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing