Introduction to javax.swing Library. A Simple Notepad Application. Part 2
Submitted by pavel7_7_7 on Wednesday, February 5, 2014 - 09:42.
This is the second part of the A Simple Notepad Application tutorial. In this article you will see, how all the functions of the text editor are implemented.
So, we continue our work on the text editor. We have the graphical user interface.
First step to implement the functionality is to add shortcut keys to every menu item.
We will ise for this KeyAdapter. When you will press any of the common shortcut keys, programm will generate the click event for corresponding button
In the first part we addedlisteners to the all menu items. When our class implements ActionListener interface you'll need to override
- open.doClick();
- save.doClick();
- save_as.doClick();
- find.doClick();
- replace.doClick();
- }
- });
public void actionPerformed(ActionEvent e)
method.
From the event object e
you can get the source of the event. That's why the structure of the actionPerformed method is like this
- if(e.getSource() == object1){
- //handling object1 pressed event
- }
- if(e.getSource() == object2){
- //handling object2 pressed event
- }
- Open a file.
We will open only .txt files. That's why we use the FileNameExtensionFilter- if(e.getSource() == open){
- FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt");
- openFile.setFileFilter(filter);//set .txt filter to dialog
- openFile(openFile.getSelectedFile(),openFile.getSelectedFile().getName()); }
So, if you pressed "Open File" from menu or pressed "ctrl+o" an open file dialog will appear and you will be able to choose a file. After this, openFile method will load the file into text area The work of openFile is described here:
Every line of the text file is appended as a new line to text Area. Reading data from file can throw some exceptions. That's why try/catch block is used.User will be informed about the error and he will be able to continue the work. In the end of work we set the name of file as the title of frame.- textArea.setText("");//text area is empty
- filePath = fullPath;//path of file
- CurrentFile = openFile;
- String scan;
- try {
- while((scan = br.readLine()) != null)
- textArea.append(scan + "\n");
- br.close();
- }
- setTitle(filePath+" Cool Reader v1.0");
- }
- }
-
Now, let's take a look on the "save" and "save as" options.
If you press "save" the saveFile method is called for the current file. If you call "save as" option, you will be asked to choose the place where file will be saved and the name of the file:
- if(e.getSource() == save &&CurrentFile!=null ){
- saveFile(CurrentFile);
- }
- if(e.getSource() == save_as){
- FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt");
- saveFile.setFileFilter(filter);
- CurrentFile = saveFile.getSelectedFile();
- saveFile(CurrentFile);
- }
- }
As you see, the process of creating open file dialog and save file dialog is very similar. The save function code is next:
First of all, this code makes a check, if the file is saved in txt format. If you don't enter the ".txt" extension, a new text file will be created. Next step is to write all the data from text area to file JTextArea write method.- try {
- if(!f.toString().endsWith(".txt"))
- textArea.write(fw);
- }
- }
-
Now, I'll show you an example of using confirmation dialog in the program. When user will press exit menu item, a confirmation dialog will be showed thank's to this code:
- if(e.getSource()==exit){
- dispose();
- else
- save_as.doClick();
- dispose();
- }
In this case user asked, if he wants to save file. If it's no need to save a file - application is closed with
dispose()
method. In case of "Yes" answer, save_as item from menu is automatically clicked usingdoClick()
method -
Handling the events from the popup menu is very simple thanks to build-in methods of JTextArea. The implementation of cut,copy,paste and delete options looks in the next way:
To show the popup menu we need to listen for the mouse events. That's why in the class the MouseListener interface is implemented. From this interface we will implement only 1 method :
- //---------------POPUPMENU HANDLERS-------------------------------------
- if(e.getSource()==mCut||e.getSource()==cut){
- textArea.cut();
- }
- if(e.getSource() == mCopy||e.getSource()==copy){
- textArea.copy();
- }
- if(e.getSource() == mPaste||e.getSource()==paste){
- textArea.paste();
- }
- if(e.getSource() == mDelete||e.getSource()==delete){ textArea.setText(textArea.getText().replace(textArea.getSelectedText(),""));
- }
This will show popup menun, when you right-click (it's 3 button on mouse) on text Area.- if(e.getButton()==3){
- mouseMenu.show(e.getComponent(), e.getX(), e.getY());
- }
- }
-
Another type of used dialog in this aplication is message dialog. An Example of the message dialog is:
- if(e.getSource()==help){
- "ctrl+s - save file\n" +
- "ctrl+alt+s - save file as\n" +
- "ctrl+f - find\n" +
- }
So, when you use a message dialog you have to write the message, the title of the dialog and the type of the message(JOptionPane.PLAIN_MESSAGE). There are several types of messages, like error or informational message.
-
Also, you can show a essage dialog of the specified construction. You can construct you own dialog message, for example, to ask user to input data. This is an example of creating your own dialog:
- if(e.getSource()==replace){
- JComponent[] replaceField = new JComponent[]{new JLabel("Find"),searchWord,new JLabel("Replace with"),replaceWord};
- textArea.setText(textArea.getText().replaceAll(findString, replaceString));
- }
Add new comment
- 49 views