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.