Text Editor. Stage 1/4

A window to editing

Report a typo

Description

When writing a code, you use a text editor. However did you know you can do a lot more with a text editor than just programming? You can make notes in the text files and save important or temporary information. In this project, you will write a functional text editor that allows you to do just that. We will use Swing to create a graphical user interface (GUI).

In Swing, the class that represents an empty window is JFrame. Actually, all of the classes that represent graphic elements in Swing start with the letter 'J.' Don't confuse these classes with classes that represent graphic elements of the AWT library (for example, the Button class in the AWT library versus the JButton class in the Swing library).

To improve the basic window, you should extend this class and write your own logic for the window. Let's see this code below in the constructor of TextEditor class that extends JFrame:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setSize(300, 300);
setVisible(true); 
setLayout(null);

Without the first line, if you close the program, your program would still be running, and after that, you can only kill it using the task manager or the IDE (if you are running it under an IDE).

The second line sets the size of the window. Without it, the program would be 0 pixels in width and height. Of course, nothing can fit in this window size. And if you run the program, you would see nothing because the window is invisible by default. The third line fixes this. The fourth line means that there won't be any strategy for placing the elements on the screen. We will discuss different layouts later. For now, you can place components on the window using absolute coordinates and bounds.

You can place any component anywhere using component.setLocation(int x , int y) and component.setSize(int width, int height). If you want to change both the size and the location of the component, you may want to use component.setBounds(int x, int y, int width, int height). To add a Swing component to the window, you need to invoke the add method and pass the component as a parameter. For example: add(component).

Also, you can use the setTitle method to set the name of the window.

You can find the list of components at "A Visual Guide to Swing Components". In this stage, you need to write the most suitable component for our needs; JTextArea. For now, you can only edit text in the program, not save it or load it to the hard drive.

For the testing reasons, you need to set the name of each component using the component.setName(String name) method.
For now, there is only one component JTextArea and you should name it "TextArea."

Example

Below is an example of what your basic text editor might look like:

java text editor

Write a program
IDE integration
Checking the IDE status
___

Create a free account to access the full topic