Introduction to javax.swing Library

In this tutorial you will find a step by step guide about creating your first Java Application with Graphical User Interface using Eclipse IDE. Swing library is one of the most used libraries to create applications with Graphical User Interface (GUI). This library has a large set of different GUI components. We will start from the simplest application - "Hello World!" application.
  1. Open the Eclipse IDE. Choose "File" -> "New" -> "Project" from menu. New project
  2. Next step is to enter the project's name. We will name our project "HelloWorld". After entering project's name, press "Finish" to create project New Project
  3. Now we will add a class to our project. This class will have the main method. Right click on the project's name in Package explorer and choose "New" -> "class". add class
  4. Now, enter the name of class and choose option, which will create the main method automatically. class
  5. After creating the Hello_World class, Eclipse will generate the following code
    1. public class Hello_World {
    2.  
    3. public static void main(String[] args) {
    4. // TODO Auto-generated method stub
    5.  
    6. }
    7. }
    First of all, we have to know the fact that if we want to create a frame application, our class must be inherited from JFrame class. That's why we have to add next import import javax.swing.JFrame; and then class can extend JFrame class import javax.swing.JFrame; Now it looks like this code:
    1.  
    2. import javax.swing.JFrame;
    3.  
    4. public class Hello_World extends JFrame {
    5.  
    6. public static void main(String[] args) {
    7. // TODO Auto-generated method stub
    8.  
    9. }
    10. }
  6. The next step is to add GUI components to our frame. We want to have "Hello World!" message in the frame. Often, class, called JLabel it's used to show different text in frame. So, we have to add another import to the code:  import javax.swing.JLabel; Traditionally, all GUI elements are initialized in the constructor. We will act in the same way. To create an instance of JLabel, write the following code:
    1. JLabel helloLabel;
    2. helloLabel = new JLabel("Hello World");
    The constructor of JLabel class takes a string as an argument. This string will be displayed on the label. To find more information about JLabel class, read this
  7. Next step is to modify some properties of the text, displayed in the label. To change the color of the text call setForeground method of the JLabel object  helloLabel.setForeground(Color.RED); The parameter of this method is the color of the text. To use color constants, please, add import java.awt.Color; import. By default, text in the label is aligned to the left part. We will change the alignment to the center using setHorizontalAlignment method: helloLabel.setHorizontalAlignment(SwingConstants.CENTER); SwingConstants class it's a set collection of different constants used in Java. CENTER constant is used to align text to the center of component. Now we will change the font of the text, displayed on label. We wil call setFont method to change the font. It will look like this helloLabel.setFont(new Font("Arial", Font.PLAIN, 36)); where "Arial" is font's name, "Font.PLAIN" is font's style(It can be also Font.Bold and Font.Italic) and 36 - size of font. We have finished the process of initialization. Now, the constructor looks like this:
    1. Hello_World(){
    2. JLabel helloLabel;
    3. helloLabel = new JLabel("Hello World");
    4. helloLabel.setForeground(Color.RED);
    5. helloLabel.setHorizontalAlignment(SwingConstants.CENTER);
    6. helloLabel.setFont(new Font("Arial", Font.PLAIN, 36));
    7. add(helloLabel);
    8. }
    The last step is to add helloLabel to the frame. For this we will call  add method. in the line 7.
  8. Now, let's create an instance of our class in the main method:
    1. Hello_World HW = new Hello_World();
    Next, we have to set size of the frame. For this we call SetSize method: HW.setSize(320, 240); After this, we make the frame visible: HW.setVisible(true); The full project's code is:
    1. import java.awt.Color;
    2. import java.awt.Font;
    3.  
    4. import javax.swing.JFrame;
    5. import javax.swing.JLabel;
    6. import javax.swing.SwingConstants;
    7.  
    8. public class Hello_World extends JFrame {
    9. Hello_World(){
    10. JLabel helloLabel;
    11. helloLabel = new JLabel("Hello World");
    12. helloLabel.setForeground(Color.RED);
    13. helloLabel.setHorizontalAlignment(SwingConstants.CENTER);
    14. helloLabel.setFont(new Font("Arial", Font.PLAIN, 36));
    15. add(helloLabel);
    16. }
    17. public static void main(String[] args) {
    18. // TODO Auto-generated method stub
    19. Hello_World HW = new Hello_World();
    20. HW.setSize(320, 240);
    21. HW.setVisible(true);
    22. }
    23. }
    And that's all for our application. Let's run it! code
In the next article we will speak about other swing components and events handling model in Java.

Tags

Comments

Thank you user for ur javax swing Library tutorial.......

Add new comment