yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java GUI Lesson 7 | JRadioButtons


3m read
·Nov 3, 2024

Hey, this is Mac Heads 101. My name is Jake. Welcome to your seventh Java tutorial, and today we're talking about JRadioButtons.

So, if you remember in the last tutorial, we used file menus and we had radio button menu items so that only one can be selected at a time. Well, these radio buttons are kind of like checkboxes, and only one can be selected at a time. So, we're actually going to be making the same program I made in the last tutorial, just with radio buttons instead of a file menu, which is where when you select one, it changes the background or whatever you set.

I'm actually going to have more colors in this tutorial, just because it's more fun that way. So, I may just implement ActionListener just so we can actually, you know, do something.

So, private JRadioButton red. I'm going to copy and paste this a couple of times, so I have five colors: red, blue, yellow, green, and magenta because that's a fun one. And I have, no, it has no magenta.

Alright, so now public second constructor. I'll set layout as just new FlowLayout because I don't feel like coding a custom layout right now. Set visible true. Set default close operation. Set size, and they get 400 by 250.

Alright, so now red equals new JRadioButton("Red"). So you know what each button is going to be doing. And I'll just copy and paste this four more times to do for each other button: blue, yellow, green, magenta. And, of course, I'm going to change the label of it to all that, so red, blue, yellow, green, magenta.

Right now, I'm going to make that a button group, and what that means is if I add things to a group, only one of them can be selected at a time. Since I only want that because the background can only be one color at a time, which is why these are radio buttons in the first place. A radio button means it can only be one thing at a time.

So do that: ButtonGroup colorGroup equals new ButtonGroup(). And I just have to add these onto the group. So group.add(red). And I'm going to copy and paste this because I'm lazy: blue, yellow, and magenta.

Alright, so I think I would also have to do, oh yeah, add the ActionListener to them. So red.addActionListener(this) so that adds this ActionListener, the one that's implemented. And yeah, put it for blue, yellow, green, and magenta.

Now I just have to add all those components to the window; otherwise, they're just not going to show up, and that would be bad. So yeah, add red, blue, yellow, green, magenta.

Alright, so now the last thing to do is build that ActionListener, also the whole point of our program. You know, there's no point because we're just trying to change the colors.

So, in this method, we're going to change the color based on what's selected. So public void actionPerformed(ActionEvent e) {

Alright, so now if (e.getSource() == red), so basically if red is selected, it's the location of the event. If red is selected, setBackground(Color.red). And that basically means if red is selected, set the background color to red.

I'm just going to do that for blue, yellow, green, and magenta. So blue, setBackground(Color.blue). Yellow, setBackground(Color.yellow). Green, setBackground(Color.green). And magenta, setBackground(new Color(128, 0, 128)).

Okay, so that's up on ActionListener. And in here, I just have to do this: Second s = new Second(); That's all. And I'm not doing s.setVisible(this time because I set visible in here.

So now I'm just going to run this, and you'll see exactly what it does. So here's our five buttons. Let me just resize this because I feel like it. So red, blue, green, yellow, magenta.

So when I click red, the background becomes red, and when I click blue, it's blue, green, yellow, magenta.

Okay, so basically, the group is what made it so you can't select more than one at a time, and radio buttons only one can be selected at a time. So, as you can see, that one's selected, and then I try to click blue, and I can't select it because of the radio buttons.

So, thank you for watching Mac Heads 101. Subscribe, goodbye!

More Articles

View All
How to sell a $20,000,000 private jet.
You have a budget in mind, probably 10 to 20 million. That would be a number that’s not really an issue. What is the Gulf Stream G450? I guess you have two T450s on here; they’re going. One of them was also sold. We have another one that’s 95 million. The…
High Tide Trash Talk | Wicked Tuna: Outer Banks
Yeah, you’re there, Tyler? Yeah, yeah, yeah, yeah, yeah. What up over there? We need to get on to beat here. We’re on. Well, got him on. All right, good luck. Yeah, baby, airing it out! Yaaaah! What a chump! It’s really no one’s business if I’m hooked up…
Surviving Y2K: What did we learn from the biggest tech scare in history?
Many of our younger folks really don’t realize that the year 2000 or Y2K digital transformation was perhaps the biggest digital transformation to date across the world. So here’s a little bit of context. Y2K as a programming problem was caused because tw…
Finding specific antiderivatives: exponential function | AP Calculus AB | Khan Academy
We’re told that F of 7 is equal to 40 + 5 e 7th power, and f prime of X is equal to 5 e to the X. What is F of 0? So, to evaluate F of 0, let’s take the anti-derivative of f prime of X, and then we’re going to have a constant of integration there. So we …
How POV affects readers | Reading | Khan Academy
Hello readers! I want to talk to you today about point of view in literature and how it can shape what we as readers take away from a story. Now, we’ve talked about this in more basic terms before: Is a story in first, second, or third person? But I would…
How Do We Manage Loneliness?
There are no fixed environmental criteria for loneliness to occur. It can happen anywhere, even in social settings, family gatherings, and in the presence of a spouse. We can feel alone in large groups of people, but the experience of loneliness can be to…