Get Selected Radio Button Value Example | Swing

java

Yesterday there was someone in Facebook Java Group asked how to get selected value from RadioButton in Java Swing. Actually this problem is very easy to be solved, but since in the past i didn’t learn Swing in depth and never use RadioButton except in web based projects which are on different side so i took a search on google and read some books 😀

We need to locate RadioButton inside ButtonGroup so that RadioButton works as group not individually because RadioButton’s behavior is can be selected just one in one group. Moreover we have to set the action command for RadioButton, this is like the value of it. So here’s my solution

Suppose you design the Frame using Netbeans GUI Builder

form design

When jButton is clicked, jLabel1 will show whether “male” or “female” depends on your choice.

As i mentioned above, we set the action command and place RadioButton inside ButtonGroup. To get the value of your choice simply using buttonGroup object.

here’s my result
run

Hope this simple tutorial help you
thanks for coming on my blog
happy programming 😀

Facebook Comments
 

Agung Setiawan

Agung Setiawan adalah software engineer di BukaLapak.com, penulis sekaligus pecinta sastra, dan pembaca buku

 
Halo, perkenalkan saya Agung Setiawan.
Saya Software Engineer di BukaLapak.
Simak pemikian saya soal dunia Software Engineering via Twitter di @agungsetiawanmu dan facebook
Blog ini saya update seminggu sekali jadi sering-sering saja mampir
Mau belajar Vim bareng saya?
Belajar ngoding dari nol menggunakan PHP

2 thoughts on “Get Selected Radio Button Value Example | Swing

Leave a Reply

Your email address will not be published. Required fields are marked *