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
The 5 BEST Credit Cards For Beginners in 2021
What’s up you guys, it’s Graham here! So welcome to the year of 2021, where YouTubers like myself can finally make videos with 2021 in the title. But here on the channel, it’s become kind of like an annual tradition to break down the best credit cards for…
The Last Northern White Rhinos | Years of Living Dangerously
That’s right. It’s good we met the last three northern white rhinos in existence. They have three armed guards that follow them around and make sure the poachers don’t kill them, so they have to have 24-hour security. Yes. Do you think people will come i…
Reading tables 2
The table below shows the number of chickens sold by four friends from January to March. Except one entry is missing. What number should go in the empty cell? So, let’s see. This is the empty cell right over here they haven’t filled out. How many? Let’s …
Born Poor, Stay Poor: The Silent Caste System of America | C. Nicole Mason | Big Think
We’re not talking honestly about what it really takes to get from poverty to the middle class. So, for example, we know that only four percent of people who are born into poverty will ever make it to the upper middle class or to, you know, have middle cla…
Write your story, change history - Brad Meltzer
You know what the greatest secret of history is? It’s that history can be changed. And, yeah, yeah, I know, everyone says history can’t be changed, but it can. And today we’re going to talk about how history isn’t just something that goes backwards; histo…
15 Steps To Completely Disappear from Society
There’s a difference between putting your life on airplane mode and leaving your entire life behind in search of a new one. Some people simply can’t settle for a cookie-cutter life; to them, it’s either freedom or nothing. What if you could simply quit yo…