Text Editor Application using Java Swing

               Do you want to create a graphical user interface using java???

If your answer is yes, then this is this concept is for you. This is “Java Swing”.

What is Swing?

              Swing is a java-based application which develops Graphical User Interface. It mainly interacts with the system. It is compatible with Windows OS.

Where to write the Swing Code??

              You may include in constructor, main () method or any method.

Swing Component has text fields, password fields, buttons, menus, labels and so on.

The features are listed as follows.

  • Swing application can be used in any Java enabled platform.
  • Swing holds lightweight components.
  • This provides configurable applications.
  • A pre-built layout manager is used to organize components.

Swing methods:

 Swing has variety of methods listed below.

 ‘setSize()’, ‘setLayout()’,’setVisible()’ and ‘add()’.

Let us create a simple Swing application in java. Make it a ‘Text Editor’ in java using Swing application.

Program implementation:

It is a text editor application which edits the text. This can be implemented as follows.

  • ·       First,include the built in package javax.swing.
  • ·       Create a public class with main() function.
  • ·       An object is created for JFrame.
  • ·       To set the text area,use JTextArea  object with length and breadth.
  • ·       In the frame, add() function is used to add the ScrollPane in the frame.
  • ·       Set the Default close Operation.
  • ·       Make the frame visible by setVisible(true).

Program:

// Text editor application using Swing in java

import javax.swing.*;

public class TextEditorEg {

    public static void main(String[] args) {

       //Frame object, text area object creation

        JFrame f1 = new JFrame("TXT EDITOR");

      // text area dimension setting

        JTextArea txtArea = new JTextArea(30,90);

     // add the scroll pane to text area

        f1.add(new JScrollPane(txtArea));

        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f1.pack();

        f1.setVisible(true);

    }

}

Output:

Just compile and run the program to get the output.

C:\raji\blog>javac TextEditorEg.java

C:\raji\blog>java TextEditorEg

Hope this text editor application using Swing in java is useful to you. Keep coding.

No comments:

Post a Comment