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

Java GUI Lesson 4 | JButton and ActionListener


4m read
·Nov 3, 2024

This is Maad's 101. My name is Jake, and this is going to be your fourth Java GUI tutorial. Today, I'm going to be talking about the action listener, which is going to give our GUI some functionality.

Basically, the program we're going to be making is, there's a text field, and you enter your name in there. Then, you click the button, and a little window pops up, which says "Hello" plus the name you entered. That window is called a JOptionPane. It's not like a JFrame; it's a different kind of window that's just a popup, so we don't have to code all the stuff that we had to code for the JFrame.

We’re going to make this extend JFrame, and that's not at all how you spell extends; there's no "Y" in extends. We import java.awt.event, and that allows us to use some event handling, which is going to be, in this case, the action listener and implements action listener. Now we're getting an error here because this interface, the action listener, if you remember from the polymorphism tutorial about overriding methods, well, there's a method in here that has to be overwritten, and that's called actionPerformed.

That's going to allow us to do something when an event happens. If this is confusing you now, you'll see once I start building the program. So first, I'm going to make a private JLabel, enter name, because that's what it's going to say. We're telling the user to enter their name in the text field below. Private JTextField name, because that's what's going to be in there. Private JButton click, which is the button we're going to do it to, and private String storeName that will equal nothing right now.

That's just going to store the name; it's going to store the value of whatever was in this text field. So yeah, so public Second make the constructor, set layout null because we're going to use absolute positioning. Set size; I'll make this 300 by 250. Set default close operation so it can close when we press the "X." Yeah, and I'm not going to set a title.

Now, enter name equals new JLabel, and I'll make it say "Enter your name." Button equals new JButton; I'll make it just say "Click," and I don't know why I called it "click." Name equals new JTextField, and I'm not going to give that any default text. All right, so now we set the location. We're going to use that setBounds method I went over in the last tutorial.

SetBounds, and I will make that 60 and 30 down, 120 large, and 30 tall. Name.setBounds, and that'll be 80 in 60 down, 130 big, and 30 tall. The button click setBounds; it'll be 190 down, 60 by 30.

Now, here's what I have to do: click.addActionListener, and this is going to give it that action listener so that we can do something when the button is clicked. I'm just going to write this, which means this action listener, the one that we implemented. It's not actually going to have an action listener until I override that method, but it's kind of hard to explain until you really see the whole thing.

Now we have to add all our components: add click, add name, and add enterName. Right, so that's all for the constructor. The name of that method we have to override is public void actionPerformed, and it takes the parameter of ActionEvent e. Okay, and that's the name of the method.

See, we don't have the error there anymore. Here's where we're going to make an if statement to basically mean if the button's clicked. So if e.getSource means the location of the event equals click. If the location of the event is that click button, in other words, if the button's clicked, do the following.

So if the button clicks, storeName equals name.getText. What name.getText is, is it's a method that returns the value of whatever is in that JTextField. So storeName will then have the value of whatever is in that JTextField.

And that JOptionPane I was talking about, do so JOptionPane.showMessageDialog. This is really simple; it's just like a popup thing. Parent component? Don't worry about that right now; just put null, and the message is "Hello" plus what was the name of that storeName.

Here, I'm going to put a System.exit(0), which means close the program. So it's going to close the program after I click "OK" on this JOptionPane.

Okay, so now just Second s equals new Second(). SetVisible(true). What we did is we got our components here: the label to tell you to enter your name, the text field where you enter your name, and the button where you click. That string just stores the value; it is going to store the value of whatever is in the text field.

The addActionListener(this), which means this action listener that we're implementing, and this is the method for it. When I add the action listener to this, it means that it's waiting for an event to happen. If the event happens, it goes to this method and it means, "All right, so an event happens."

Did that event happen at the button? Okay, it did; so now do this, which is storeName equals whatever was in the text field. A little window pops up, says "Hello" whatever your name was, and then exits the program afterward.

So now I'm going to run this. Here it is: "Enter your name." I'll write "Jake" and press "Click." "Hello Jake," that's the JOptionPane, and you can customize this message part and customize this image.

I might talk about that later; I'll see, but click "OK," and then the program exits. And if I got rid of System.exit(0), the program would not exit when I pressed it; I would have to exit out.

So, I'll say "John." "Hello John." See, but I can exit out.

Thank you for watching Maad's 101. Subscribe, and goodbye.

More Articles

View All
Khan Stories: Claudia
My name’s Claudia and I’m currently a freshman at MIT. I’m from South Florida and now my journey continues here. My family is from the Azores Islands, which are in the middle of the Atlantic. Just knowing that where my family comes from and the lack of e…
Nelly - Ride Wit Me (Official Music Video) ft. St. Lunatics
[MURPHY LEE] I CAN’T JUST DRIVE THE HUMMER? - [KYJUAN] HOLD ON HOLD ON HOLD ON HOLD ON - [MURPHY LEE] I DON’T WANNA GET MY RIMS DIRTY ♪ OH WHY DO I LIVE THIS WAY? ♪ ♪ OH IT MUST BE THE MONEY ♪ ♪ IF YOU WANNA TAKE A RIDE WITH ME ♪ - OH! ♪ THREE WHEELING IN…
How To Make Colour With Holes
I want to show you how to color a transparent piece of plastic without adding anything to it, no dyes, paint, nothing except holes. But first we have to talk about light. Most people know that it is a form of electromagnetic radiation. But have you ever s…
Electron configurations of ions | Atomic structure and properties | AP Chemistry | Khan Academy
In many videos, we have already talked about electron configuration, and now in this video, we’re going to extend that understanding by thinking about the electron configuration of ions. So these are going to be charged atoms. So let’s just start looking…
TIL: There's Probably a Raccoon Living on Every City Block in North America | Today I Learned
Every city block probably has a raccoon living on it, and people very rarely see them or even know that they’re there. These animals have adapted to urban living in a way that makes them common and present in almost every major urban complex throughout th…
Assignment: Inspiration Winner | National Geographic
[Applause] After three uplifting photographic quests, our assignment inspiration finalists pitched us their photos, hoping to be the ones chosen to go on assignment with National Geographic Travel. We judges had an incredibly tough decision to make. Each …