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
Warren Buffett: Why Buying a House is a LOUSY Investment
Uh, I decided to buy a house when it was about when the down payment was about 10% or so of my net worth because I really felt I wanted to use the capital for other purposes. But that was a way different environment. Buying a house is usually a lousy inve…
15 Ways to Accelerate Your Journey to $1,000,000
You know, Alexa, accelerating isn’t just about putting your foot on the gas and pushing full steam ahead. In fact, that’s the last step. Before you can accelerate, you have to optimize your machine to run smoothly and efficiently and remove as many obstac…
Naming a cycloalkane | Organic chemistry | Khan Academy
Let’s see if we can name this guy right over here. And so, like always, we always want to look for the longest carbon chain or the longest carbon cycle. I think it’s pretty obvious from this picture that we have a very long carbon cycle here that we can s…
Will future robots & AI take over? | How Sci-Fi Inspired Science
Let’s face it, one of the worst things about adulting is having to clean. If we can get out of it in any way, we’ll do it. And since machines are made to make our lives easier, it makes sense we want a machine made to clean. But in sci-fi, we want to go o…
Calculating the equation of a regression line | AP Statistics | Khan Academy
In previous videos, we took this by variant data and we calculated the correlation coefficient. Just as a bit of a review, we have the formula here, and it looks a bit intimidating. But in that video, we saw all it is, is an average of the product of the …
15 Mistakes Smart People Don't Make Twice
Look, nobody’s had a perfect run through life, right? Mistakes are bound to happen multiple times. But smart people will make sure to never make these mistakes twice. Welcome to ALUX. First, stop misjudging character. Smart people are keen observers, alw…