Using a Radio Button Component in Java

Опубликовано: 14 Январь 2018
на канале: yoel_programming_school
38
0

github:

https://github.com/yoel123/yoel-java-...

transcript:

in this java tutorial i will show you how to use the radio btn component.
----------------
i will show you how the end product will look like:

.---------------------


the frame and panel class is the same as previous tutorials.

------------------------

this one is a bit more complicated. i tend to turn everything into a framework.
or at least simplify complex tasks with oop.
-----------------------------

this time i had to create multiple components JRadioButtons, group them and add all of them to the panel.
---------------

i could of done a simple example and be done with ,it but i wanted to give a solution for a little more complex.
tasks like two groups of radio buttons, and alot of them at the same time.
creating them one by one will be a waste of time so i created functions that will generate an array of radio btns and put them in
a group, the radio btns will be generated from a string array. i also created a function that will add all of them to the panel with one line.
---------------------
1
first i will show you the arrays and groups that will hold the radio btns.
------------
2
at the top rs attribute will be an array of radio btns of the first group.
rs_s will be the string text next to each radio btn.
-----------------------
3
r_group will be the group that will hold all the radio btns
---------
4
the next group of atts is for the ,male female chackboxes practically the same.

--------------------------
5
now i will show you how to use the methods i created and then go into depth on how
they work.

first we initialize the ButtonGroup object.

then we create the radio btn arry rs using create_radio_btns method.
its first argument is the string array, for each string it will create a radio btn.
second argument is r_group which is ButtonGroup object that we will add all the
radio btns to.

------------------------
6
then thers a separator i created and add to the panel to visually
separate the two radio btn groups. i wont go into it
becuse the tut is not about it, but its supposed to be
straight foreword to understand.

the male female radio btns is practically the same code as the first group
------------------------------------
7
now lets go into create_radio_btns method.

it returns an JRadioButton array.
its first argument is a string array.
the second argument is ButtonGroup

-------------------
8
inside the method
first we create an int called i to count how meany time the for loop have looped.
then we create JRadioButton array with the same length of the string array
we get from the argument.

and then we loop the string array

-------------------------------
9
inside the loop we create a RadioButton and put
in its argument the current text that is looped.
we put that RadioButton inside the RadioButton array we
return at the same position of the string array using the i
int we created


then we add the radio btn to the btn group.
and add one to i using ++ operator.

---------------
10
after the loop we set the first radio btn as selected
and return the RadioButton array.

---------------------
11
add_radio_btns just loops the RadioButton array we give it
and adds each RadioButton in the array to the panel using a for loop


------------
12
in main we use rframe the same way we used the other frame class
in previous tutorials.

-------------

if anything is unclear contact me.


На этой странице сайта вы можете посмотреть видео онлайн Using a Radio Button Component in Java длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь yoel_programming_school 14 Январь 2018, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 38 раз и оно понравилось 0 зрителям. Приятного просмотра!