• home
  • forum
  • my
  • kt
  • download
  • How-to make a JAVA GUI Application

    Author: 2007-08-03 13:34:54 From:

    Before you fire up Eclipse, or whatever editor you use. Let's first brush up your mind by learning what GUI really is and how it is designed to work in Java. A GUI is a short term for Graphical User Interface. By making a GUI program, you can make your software have buttons, menus, text fields, textareas, file browser and much much more which isn't possible by a normal console driven Java Program. GUIs are mainly designed to work with complex programs. Such as, you could make your own Word Processor, Calculator and bunch of other cool shit. Since it is always a good idea to learn while you are still interested, I think you should definitely spend more time learning GUI because in future you will be using it nonetheless less.

    GUI Hello Program

    Ok, if you've read my another article called Hello Java, Then I am sure the output wasn't really that good. Seriously no matter how hardcore (ah yes) your program is, if it is just plain text no one will be interested in it. But if you have something colorful, with style, attitude or a little bit sex thrown in... then it will look very interesting. Our program will work like this.
    1. You run the program.
    2. In the window you see two or three buttons.
    3. When clicked, each shows different types of alert messages.

    Sounds very simple doesn't it? But trust me, this baby is dirtier than it looks. I will have to go over some of the stuff like a nerd or a geek (no offense geek n nerds), so just bear with me.

    In JAVA making a GUI program is very easy and doesn't require any extra files for your clients to download or for you to download. It all comes as a pack in the JRE, called Swing.

    And throughout this tutorial, I will be using Swing Class library to make our super cool GUI Program (at least its better than Hello Java).

    A JAVA GUI Program should be designed like the diagram below (at least it's what I think):

    - main()
    - Frame
    -- Content Panel
    --- Buttons
    --- Text Areas
    --- Menus
    --- and so on

    Basically you should store all your components like buttons, text areas, menus and so on in a Panel called Content Panel. And add the Content Panel in a Frame (window), and simply display that Frame under main(). So whatever shit is inside the Frame is automatically included in the program (including the children like Content Panel -> Text Areas etc...)

    Ok, before you start farting we will jump into coding. Go ahead, fire up Eclipse since I will be using it, and it will be easier for you to follow. If you don't know how to use Eclipse follow the link below to learn:

    Starting an Eclipse Project

    I know the codes given earlier didn't make much sense, especially if you have never dealt with objects and classes. So I will go over the syntax of Java, before we get coding.

    Java is 100% object oriented, and it needs a method called main(). A method is just a function under a particular class. From the above examples, some methods would be, doSleep, eatFood and so on. By default when a program is compiled and run, it automatically executes the main() method to start the program.

    I want you to, download Eclipse SDK from here: http://www.eclipse.org/downloads/. You don't need to install or anything, simply download, extract in a folder and run the eclipse.exe file.

    The first thing you will see when you run eclipse is:

    First Run

    I want you to click the X next to welcome, to get rid of this splash. The small icons you see, does nothing much. Some links to the readme file, downloads, and samples.

    If eclipse asks you to choose a workspace, just ignore and click ok and continue accordingly.

    Go to File - New - Project



    Now eclipse will ask you which type of application you want to create. Choose Java Project from Java.

    Eclipse Project 2

    Click Next, and type the name of your project. Leave all other settings as it is for now, and click Finish.

    Eclipse Project 3

    Congratulations! Your HelloWorld Java Project is created, and you should see HelloJava folder appearing under the Package explorer:

    Eclipse Project 3

    It's time for you to start typing your source codes. After all, without source codes the HelloJava project will look very naked.

    I want you to click the Plus sign next to HelloJava to expand it. And then right click HelloJava, go to New and then Class.

    Eclipse Project 5

    Type the name of the class, for example Hello, put a tick next to "public static void main(String[] args)", and click Finish.

    Eclipse Project 6

    Done it? Good, now the real fun begins. Head over to the next section to learn how to add some methods in order to make your Hello program.
    The Source Codes

    Every time you start a Class, you will have to use the convention, public class ClassName. You can also substitute public for private, but beware it will make your classes non accessible. Don't worry, in some other article I will go over the public, private and another keyword known as protected (applies for methods).

    public class Hello

    In the next line, you start the curly brace to show, that from now on, whatever you enter belongs to the Hello class. Then you write the methods that are related to your Class.

    { //it is the curly brace indicating, that after this everything belongs to Hello class
    public static void main(String[] args) //a method or function, taking in some arguments
    { //Starts the main() method's code block

    }//Ends the main() method's code block
    } //Closes the curly brace of the Hello class, indicating whatever belongs to Hello class is completed.

    The coding structure of a method is similar to a Class , but it has a space surrounded by parenthesis. It indicates that, you can use that space to pass in some values from other party. Each method also has their own curly brace chunks. Make sure you close the curly braces or else Java will never know up to which line, a particular code belongs to a particular method.

    After the methods or the functions, you also need to close the curly brace started when you've created Hello class.

    Ok I believe you by now know you how a Class and its methods should be written. Under the main method, after the first curly brace I want you to enter this code: System.out.println("Hello Baby!");

    public class Hello
    {
    public static void main(String[] args)
    {
    System.out.println("Hello Baby!");
    }
    }

    Now go to Run - Run..., click the small plus sign next to Java Application, choose Hello and click Run.

    Eclipse Project 7

    After doing this, you should see this under the Console tab:

    Project 8

    That's it! Your Hello Java program is completed. Hello Baby! text that appeared on the console window, really is the output of your program. And you've programmed it say Hello Baby!, by entering System.out.println("Hello Baby"); under the main method.

    So when Java compiled the source codes, it looked into Hello Class, made sure there's no errors, all the curly braces are closed, searched and finally ran whatever was inside the main() method. System.out.println(), itself is a function which belongs to Java itself. Basically we use that to printout contents. System.out.println() takes in the argument that has the type String. A String can be anything from letters or numbers to different characters. Since it is a String type, the contents need to be surrounded by double quotes. System.out.println("Hello Baby!");

    If you wish to put a line break, then you can use another System.out.println(); line just after System.out.println("Hello Baby!");. Or you can add \n code at the end of Hello Baby! text. So if I wanted to print out the following lines:

    Rooseveltrp.com totally rocks!

    You should visit the web site more often and,

    Give suggestions!

    My source codes would be:

    public class Hello
    {
    public static void main(String[] args)
    {
    System.out.println("Rooseveltrp.com totally rocks!\n\nYou should visit the web site more often and,\n\nGive suggestions!");
    }
    }

    or I can also use the long way:

    public class Hello
    {
    public static void main(String[] args)
    {
    System.out.println("Rooseveltrp.com totally rocks!");
    System.out.println();
    System.out.println("You should visit the web site more often and,");
    System.out.println();
    System.out.println("Give suggestions!");
    }
    }

    That is all for now in this basic program. Hold on, don't go yet. Here is some exercises for you.

    Print out the following lines of texts:

    Java looks bit daunting at first. But I believe as I regularly practice,
    visit Rooseveltrp.com and
    contact them with questions.
    I could get better.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Applet Building (2)
      Application Building (3)
      Communication (1)
      Database Related (8)
      Development (12)
      EJB (14)
      Game Programming (2)
      General Java (38)
      Javabeans (4)
      JSP and Servlets (8)
      Miscellaneous (23)
      Networking (1)
      Security (2)
      Swing (13)
      WAP and WML (1)
      XML and Java (0)

    New

    Hot