java - Selecting radio buttons to make text appear -
using radio buttons displayed on panel, possible select radio button , display text on panel explaining user has selected?
so here list of radio buttons
public void radiobuttons() { btldap = new jradiobutton ("ldap"); btldap.setbounds(60,85,100,20); panelholder.add(btldap); btkerbegos = new jradiobutton ("kerbegos"); btkerbegos.setbounds(60,115,100,20); panelholder.add(btkerbegos); btspnego =new jradiobutton("spnego"); btspnego.setbounds(60,145,100,20); panelholder.add(btspnego); btsaml2 = new jradiobutton("saml2"); btsaml2.setbounds(60,175,100,20); panelholder.add(btsaml2); }
user selects btldap
btldap.setselected(true);
now how make text appear on panel not message box
if want display text when radio button selected use actionlistener
.
final jtextarea textarea = new jtextarea(); add(textarea); jradiobutton radiobutton = new jradiobutton(); add(radiobutton); radiobutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { textarea.settext("selected"); } }); jradiobutton radiobutton2 = new jradiobutton(); add(radiobutton2); radiobutton2.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { textarea.settext("selected 2"); } }); radiobutton.setselected(true);
when first selected change text of jtextarea the first selected!
, same second radio button the second selected
.
as said, radiobutton.setselected(true);
setselected
used select/deselect radio button.
in example used textarea, can use have method change text contains (an image too!)
official doc, here.
anyway, actionperformed
not called when setselected
used go method
private void updatetext(int index) { string text = null; switch (index) { case 0: text = "selected"; break; case 1: text = "selected 2"; break; } textarea.settext(text); }
and call updatetext(0 or 1 etc.)
when want select setselected
radio button , update text too.
all useful, if want show "what happens if press it" message, if want change text of area text of radio button, use
textarea.settext(e.getactioncommand());
Comments
Post a Comment