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
Naming ionic compound with polyvalent ion | Atoms, compounds, and ions | Chemistry | Khan Academy
So we have the formula for an ionic compound here, and the goal of this video is: what do we call this thing? It clearly involves some cobalt and some sulfur, but how would we name it? Well, the convention is that the first element to be listed is going …
The Evergrande Crisis Continues...
Alright guys, welcome back! It’s time for an update video on Evergrande. I told you it would be a crazy week, and it certainly was. However, Evergrande is still standing, at least for now. So let’s get up to speed on exactly where Evergrande is at with th…
Watches Under $1,000 For Your Collection With Teddy Baldassarre (10+ Watches Featured)
Th000. M yes, stop them! Madness! Steady 400 bucks; you can’t go wrong. This is crazy good, I’m telling you right now, this is going to be one of my top three. Hi, Mr. Wonderful here in Cleveland, Ohio, for the first time at Teddy’s new retail store. Very…
What China's Ban of Crypto Means For Investors | Meet Kevin
I want to get started right away. So, uh, I want to start with cryptocurrencies. Obviously, Bitcoin has been running. We’ve crossed that 60,000 psychological threshold. NFTs are all the rage right now. Crypto Punks, we’ve got many other NFTs as well. Uh,…
Revolutions 101 | National Geographic
[Narrator] Politics are a powerful and dynamic human creation, a truth most evident in revolutions around the world. A revolution, in a political sense, is a sudden and seismic shift from one form of government to another. While revolutions come in many…
Strategic | Vocabulary | Khan Academy
I love it when a plan comes together, word Smiths, because the word I’m featuring in this video is strategic. Strategic, it’s an adjective, and it means related to a plan. It’s the adjective form of strategy, which is a way of thinking about making effect…