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
What Is Video ??
Hey, Vsauce. Michael here, and I just got back from VidCon. The entire convention was incredible, but the whole time I was there I thought a lot about video. We all watch video and many of us work with video, but what is it? I mean, what really is video? …
Khan Academy's Content
This video will review the available content on Khan Academy and discuss how our courses are designed to help students master the content they’re learning. When learners interact with Khan Academy, they will experience videos, articles, practice questions…
How I make $13,800 PER MONTH on YouTube (How much YouTubers make)
So I definitely don’t want to give anyone the idea that the only reason I’m doing this is for money because that couldn’t be further from the truth, and I would be doing this regardless of how much money I make. But I have a feeling this video might inspi…
The Universe is Hostile to Computers
A plane plummets out of the sky, a speed runner inexplicably jumps to a higher platform. What the? What the?! And an election recount is triggered. All because of the same invisible phenomenon that permeates the universe. On May 18th, 2003, voters in Bel…
Suing Robinhood - Again
What’s down, you guys? It’s Graham here, and this is not a video that I was planning to make today. In fact, I was never planning to make a video like this ever. But given the recent circumstances and allegations, I think this is worth diving into further…
Deep Thoughts with Neil deGrasse Tyson | StarTalk
We’ve known as educators that astrophysics can be a gateway science to other sciences. So I submit to you whether or not you embrace the universe because you’re enchanted by it. I can say that in a free capitalist democracy, innovations in science, techn…