• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write Actionlistener for dynamically created radio buttons

 
krishna venkata
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys..
i am new to swing.
I have to add Jradio buttons dynamically (i.e at runtime)
private JPanel setDynamicPanel(){
//System.out.println("Enterd into setDynamic panel");
String treebank=findTreebankname();
int noofpos=0;
if(treebank.equalsIgnoreCase("export")){
manager=new TranslationXmlManager(getNegraXmlPath());
noofpos=manager.getRuleCount();
descmap=manager.getDescriptionMap();
}else if(treebank.equalsIgnoreCase("mrg")){
manager=new TranslationXmlManager(getPennXmlPath());
noofpos=manager.getRuleCount();
descmap=manager.getDescriptionMap();
}else if(treebank.equalsIgnoreCase("export")){
manager=new TranslationXmlManager(getPdtXmlPath());
noofpos=manager.getRuleCount();
descmap=manager.getDescriptionMap();
}


JPanel rulepanel=new JPanel();
rulepanel.setLayout(new BoxLayout(rulepanel,BoxLayout.Y_AXIS));
//JRadioButton [] rbutton = new JRadioButton[noofpos];
rbutton = new JRadioButton[noofpos];
ButtonGroup ruleGroup = new ButtonGroup();

for(int i=0;i<rbutton.length;i++){

rbutton[i] =new JRadioButton();
//System.out.println(rbutton[i]);
rbutton[i].setText("Rule"+" "+(i+1));
rbutton[i].setSelected(false);
rbutton[i].addItemListener(this);
//rbutton[i].addActionListener(this);
ruleGroup.add(rbutton[i]);
rulepanel.add(rbutton[i]);

}
rulepanel.setVisible(true);
return rulepanel;
//adding tab for the setting rules
//tabbedPane.addTab("Rules",null,rulepanel,"Setting rule for standardization");
}

from the above code i have added radio buttons dynamically
If i use action listener how to capture which radio button i the user pressed.

The important thing is that the radio button is an array and i cannot hard code saying like
if(e.getactioncommand().equalsIgnoreCase("Rule 1"))..
since i dont know how many radio buttons will be created dynamically
so i cannot explicitly give the above statement.

so my problem is
how to write action listeners for radio buttons which are creted dynamically.

I require this very urgently...
any help from u guys..
venkata
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




With setActionCommand and getActionCommand you could more easily parse the returned string as a number and do something with that.
[ June 11, 2004: Message edited by: Eddie Vanda ]
 
krishna venkata
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanks a lot...
it worked..
venkat
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!