Description
In this stage, you will write a program that can read files from the hard drive, edit them, and save them to the same or any other file.
Notice that if you expand the window, the other elements stay in the same place. However, in most applications, you can see that some components get bigger or smaller when you change the size of the window. To do this, you should apply another layout instead of the null layout that was shown in the first stage.
In Java, there are a lot of different layouts, and the default layout is named BorderLayout. This layout has 5 places for components: top, bottom, left, right, and center. If you execute the code below you can see where these places are. Do not forget to remove the setLayout(null) line and add nothing, as BorderLayout is a standard layout for JFrame. Here you can see a visual representation of different layouts.
add(new JButton("North"), BorderLayout.NORTH);
add(new JButton("South"), BorderLayout.SOUTH);
add(new JButton("West"), BorderLayout.WEST);
add(new JButton("East"), BorderLayout.EAST);
add(new JButton("Center"), BorderLayout.CENTER);So this layout can contain only 5 components, but is this enough to create more complex programs? Yes, and actually this layout is used widely when developing programs on Swing. The component JPanel can contain other components. The default layout for this is FlowLayout; it puts components on the screen from left to right and from top to bottom. Just add a JPanel component to the frame with BorderLayout and then add several components to this JPanel with FlowLayout! You can find out more about BorderLayout here. Check out this link to see how to use FlowLayout.
Did you notice that if you type more text than the size of the component (in our case, in JTextArea), then the text is just not visible? This is the problem that can happen with any component. To solve it, this component should be wrapped by another component - JScrollPane. This class takes the initial component as a parameter and should be added to the JFrame instead of it. Also, all manipulations with size and location should be done with this wrapper. As a result, the component can be scrolled horizontally as well as vertically. Here you can see how to use JScrollPane.
Now, let's add working with files. To load a file from a hard drive, the user should specify the name of the existing file. If you run this from an IDE, this file should be in the same folder as the "src" folder. Then, after the user presses the "Load" button, the content of the file should be displayed in the text field. After editing, you should be able to save the file. For that you need to specify a name for the file and then press the button "Save".
To create a one-line input field for the name of the file you need the JTextField component. To get the input text from that field, you need the getText method.
To create buttons, you need JButton. Now, you need to add interaction with buttons. Some of the code should execute when pressing the button. To perform this action, you need ActionListener. Since ActionListener is an interface, you need to implement the actionPerformed method in a class that implements this interface. This would be your code when the user presses the button. Note, that for different buttons you need different logic, so you need different classes that implement the ActionListener interface. After that, you need to bind each class with each button. The method button.addActionListener(ActionListener listener) makes this possible. This method takes an object of a class that implements ActionListener. After that, everything should be fine. Pressing the button triggers the actionPerformed method of that object.
If you are familiar with lambdas, you can use them instead of creating classes that will be used only once. You can implement them as shown in the following template:
JButton button = new JButton();
button.addActionListener(actionEvent -> {
/* write your code here */
});During the programming of this stage, you may want to slightly tweak the sizes of the components. You can do it in two ways: first is to create an invisible border around it (this link will help), and the second is to force the component to be a certain size (this link).
Due to testing reasons, you need to set names to some components.
Set the names to these components:
JTextArea component to "TextArea"
A field which contains filename to "FilenameField"
A button that saves the file to "SaveButton"
A button that loads a file to "LoadButton"
ScrollPane to "ScrollPane"
Example
Below is an example of what your text editor might look like: