Java radio buttons tutorial JRadioButton #51


Welcome back. It’s your bro here. And in this video, I’m going to show you how we can make J radio buttons in Java Let’s get started If you’re new here Make sure to subscribe so you can always return to this channel in the future and not miss any additional content for this topic I’ll be discussing how J radio buttons work Radio buttons usually belong in a set of at least two by selecting one you unselect any others So we are forced to make a choice between one of a few different options. So let’s start to make some J radio buttons, so Just to give you some context here’s my program. I have two classes a class name test on another name domain I have in my main method an instance of the class test and Then I have a frame setup and a button Feel free to pause the video if you’d like to copy these down or you can just watch doesn’t matter I don’t care regardless all right now to make a J radio button outside of the constructor. We’re going to type J radio button We’ll need a name. How about radio button? We’re going to make a few so I’ll make radio button one Then we’ll make radio button two and three and I’ll just copy and paste Radio button three Alright, let’s start to finish declaring these and instantiating them so radio Button one and we’re gonna do this within our constructor equals new J radio button Parenthesis semicolon and then whatever we put within the parentheses. This will appear next to the radio button itself So for our example, let’s say that we want a user to select a form of payment like a check a credit card Whatever. We’ll have them check of like one of these options So for our first option, we could say a check like they’re paying by check All right. Now we just need to add the radio button and Then we’ll add a few more to So we’ll just add Radio button one for now just for this example, then we’ll add the others later on Oh I have to set the bounds. Okay, so I have a No lay out. It’s set to null. So we need to set the bounds for our radio button So radio button 1 equals, nope set bounds My bed set bounds. Okay, the there’s 4 fields The first two are the x-coordinate then the y-coordinate the width and the height so let’s set this to where X is 50 and Y is 50 I’m just making stuff up. We’ll make this 200 units long. I think it’s pixels and then 50 for the height All right now this should show up now There we go, it says check all right, let’s have the others then So we have radio button – and then radio button 3 Will make the second payment option Credit like a credit card and the third. How about Bitcoin All right, then we need to set the bounds for our radio buttons as well So I’m just gonna copy and paste what we have for Radio button 1 so we have set bounds for 2 and 3 now. We’re just gonna change the Position of where they start so they don’t overlap So 100 and 150 Then we just need to add these radio buttons So we’re adding these to the frame Alright, let’s see what this looks like all Right, yeah, we have three total radio buttons now one thing we’d like to change is that if we select one we should be able to Deselect the others but here we can obviously select all three and that’s going to be a problem what we need to do here is add these all To a button group. So then if you select one it’s going to deselect the others so outside of your constructor type in button group Then we can name this button group Honestly, we can just fully instantiate this outside the constructor new button group All right, and then we need to add these buttons these radio buttons to our Button group so we know that they’re all within the same group So then button group Dot add Then we’re going to add radio button 1 then we’ll just copy and paste this for the others 2 & 3 All right now let’s rerun this Alright so then when you select one it deselects the others like we intended So you have to make sure you have them all in a related button group What else can we do? Um We could change the font and the font color You’ve probably already seen me do this for a few other examples, but if you’re new here We just run through it like real quick. So and I’ll just copy and paste this for the other buttons So radio button 1 dot set font New font Select whatever style of font you want my favorite is mV bolli The second field separated with a comma is the style. I think I’ll pick bold and The third is the font size and I’ll go with 15 use whatever you want Then let’s copy and paste this for all three of these radio buttons Alright, let’s see what this looks like. Yeah. We you can see the style changed now, let’s change the font color Radio button 1 dot set foreground color Let’s try dark-gray I’m not gonna mess with RGB values never now And we’ll copy and paste this Now you can’t really tell let’s try something else maybe I haven’t read green yet. Let’s try green Okay, that’s pretty terrible let’s go back go back go back go back All right, we’ll keep it at dark red, that’s fine Now let’s change it. So when you select each option there’s this annoying line It’s annoying box that pops up around each of these labels we can get rid of that So we need to set focus of all false. So radio button one dot set focusable False And we’ll just copy and paste this for the rest of the buttons two and three Okay, that is much better Alright now we want these radio buttons to do something when we hit the submit button So now whatever class you’re using make sure it implements the action listener interface and if you’re using an interface Specifically the action listener interface you’re required to have this method public void actionperformed now This is a necessary. But for my example, I think would be helpful if we tied these radio buttons to our current button So we can like submit payment or something so, how about If you have one of these radio button selected It’ll just pop up with a message that says you are paying with either a check credit card or Bitcoin But use whatever you want for your example. I’m just trying to pick something that’s simple and easy to follow So this is what I’ll do if Radio button one dot is selected Then I’m just gonna print out If you’re familiar with out messages I log boxes works That would actually be an appropriate thing to use for something like this. You can just have a box that pops up and says you are paying with check or whatever, but I’ll go over dialog boxes in a Lesson coming up fairly soon. All right. So if radio button one is selected. That was our check Will just display you are paying by check All right, then I’m just gonna copy this and make a few changes for radio buttons two and three Two three you are paying by credit And you are paying by Bitcoin, all right, let’s try this and I’ll Make this console window a little bit bigger. All right, so let’s pay with Bitcoin Between you and me. I’ve no idea how Bitcoin works. I just know what school you are paying by Bitcoin you are paying by credit and You are paying by check All right. Now we want to disable like everything once we hit submit payment, so within our method Edye gets source equals button will say radio button one dot Set enable false honestly, we should disable the button – All right So you are paying by check, we can’t select any more options and actually I’ll disable that button – Button set enabled pulse Honestly if you’re submitting a payment you should be brought to like another window or another screen but Maybe we’ll save that for another day All right, you are paying with credit submit and it says we are paying by credit We can’t select anything and the buttons disabled like we intended so to summarize radio buttons are grouped in a group size of two or more and when you select one it should either unselect or deselect the others because we only want our users to be able to pick from one of a few options and They’re good to just give users choices when they’re only allowed to have one and that is how a radio buttons work in Java Ladies and gentlemen that will conclude this episode of bro code you did great by making it to the end Keep up the good work if this video helped you out You can help me out by smashing that like button and leave a comment behind subscribe if you’d like to become a fellow, bro

Leave a Reply

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