Can I catch the panel name from ActionListener
?
Here is some code:
void models (int paneNum, int tabNum, String eQuery, String str, String title) throws SQLException {
intoModels[paneNum] = new JPanel();
intoModels[paneNum].setBackground(c);
intoModels[paneNum].setLayout(new GridLayout(6, 2));
ResultSet rs = DataBase.setConnection().executeQuery(eQuery);
ButtonGroup modelRadioGroup = new ButtonGroup();
while (rs.next()) {
JRadioButton radio = new JRadioButton(rs.getString(str));
radio.addActionListener(new radioButtonActionPerformed());
modelRadioGroup.add(radio);
intoModels[paneNum].add(radio);
}
intoModels[paneNum].setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
panelsTab[tabNum].add(intoModels[paneNum]);
}
And a listener code:
public class radioButtonActionPerformed implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Selected radio: " + e.getActionCommand());
selectedModel = e.getActionCommand();
}
}
The interface:
I can get selected radio, but I can't register from which panel. So I need to register the panel and save selected to the array on indexes 0 and 1 for the first and second panel.
Have you tried component.getParent()
method to get the parent.
public void actionPerformed(ActionEvent e) {
Object src=e.getSource();
if(src instanceof JRadioButton){
Container parent=((JRadioButton)src).getParent();
if(parent instanceof JPanel){
System.out.println(parent.getName());
}
}
}