Sitemap
farook fazni

farook fazni

Associate AI/ML Engineer at Virtusa | 2 years in AI solutions | Skilled in Azure Synapse, PySpark, SQL | Exploring Generative AI, Transformers, and LLMs.

Java Chapter -12

3 min readJul 17, 2022

Hi guyz! In this article we are going talk about Java GUI(Graphical User Interface). Let’s dive in.

why we need a GUI. If we’re building applications that other people are going to use, we need a graphical interface. If you’re building programs for yourself, you want a graphical interface.

It’s all Starts with a window. A JFrame is a object which represents a window on the screen. It’s where we put all the interface things like buttons, checkboxes, text fields, and etc. It can have an honest-to-goodness menu bar with menu items. And it has all the little windowing icons for whatever platform we’re on, for minimizing, maximizing, and closing the window.

So How do we make a GUI. Making a GUI is easy,

Let’s create our first GUI button on the frame.

Press enter or click to view image in full size

If we run above we’ll get something like this.

(Whoa! That’s a Really Big Button…..)Is it works? yes it works but it doesn't display anything. When you press the button it shows that ‘pressed’ or ‘pushed in’ look, But the Million dollar question is “How do we get the button to do something specific when the user clicks it?” To make it work we need to do two things and they are,

We need to call a method when the user clicks the button.

And we need to find a way to know when the user clicks button.

So how we can make it work in real scenario. First Let’s make a method to do when the button is pressed. Don’t worry it’s a simple work.

Yes as the image says we just need to create a method. But how we know the button is clicked or not. That’s the big deal here. In here first we need tell the button that we care(It means that we need to know if anything happens to you….) and then button knows that we care them so when something happens to it it send us a message. Theoretically yes this is what we need to do, but How do we do it in practically.

To get this done we are goin to use a pre build java class named event.

Get farook fazni’s stories in your inbox

Join Medium for free to get updates from this writer.

you can go through the java documentation which has a huge amount of events in it. But in this article we are going to use a event name ActionListener() Interface. which is works like,

Press enter or click to view image in full size

So Let’s build the code for that so you can get a better idea.

I hope yo all got an idea of Events. You can check more details about Events in Head First Java book which is written by Kathy Sierra, Bert Bates. Don’t forget to read the book.

Reference: Head First Java, 2nd Edition [Book] (oreilly.com)

--

--

farook fazni
farook fazni

Written by farook fazni

Associate AI/ML Engineer at Virtusa | 2 years in AI solutions | Skilled in Azure Synapse, PySpark, SQL | Exploring Generative AI, Transformers, and LLMs.