Search code examples
javaswingjcheckbox

how to assign value to a checked JChechbox?


I am making a simple currency convertor which takes the initial value in a JTextField then the user clicks the JCheckBox corresponding to their desired currency, the converted value will then be displayed in another JTextField. Basically i would like to know is there any way of assigning a value to a checked JCheckBoxi have looked around and cannot find a clear answer to this, any help would be appreciated.

For example if the current conversion rate from Sterling to euro is 1.12244 this value would be assigned when the JCheckBox is checked, so the the original value would be multiplied by the conversion rate.


Solution

  • Think it'd be easier if you assign an action-listener to your JCheckBox and make the conversion on trigger of this event. To check is a JCheckBox is checked or not you can use the isSelected() method

    EDIT

    Actually i think you need to use JRadioButton's in a ButtonGroup for this, as if you are using a checkbox then there is a chance that more than one is selected. Here is an example of how to do use ButtonGroup and trigger action on the radio button