Java Reference
In-Depth Information
The methods saveFile and exitEditor are implemented in a very simple
way; exercises to extend the implementation are given at the end of the chapter.
The saveFile method writes the current content of the editor area to a file with
the same name as the one it loaded from. The exitEditor method exits the
application.
public void saveFile(){
try {
FileWriter fw = new FileWriter(selectedFile);
fw.write(textDisplayPane.getText());
fw.close();
} catch (IOException e){
System.out.println("Problems writing "+selectedFile.getName());
}
textDisplayPane.setText("");
}
public void exitEditor(){
System.exit(0);
}
The implementation of the 'Search' feature is described in the next section.
12.3
User-defined dialogues
We shall now augment the editor with a search function. To keep things simple,
we search for a whole word only and report how often it occurs in the text. The
user can specify whether the search should be case-sensitive or not.
We will create our own dialogue class SearchDialog by extending the Swing-
class JDialog .A JDialog is much like a JFrame but it can be modal ; see Sec-
tion 12.2.1. A JDialog has a content pane into which further components are
embedded. We glue a panel with 4
2 grid layout into the content pane of the
dialogue. We then add components to the panel to get a layout as shown in Fig-
ure 12.2. The only new type of components here are the radio buttons which
select whether the search is case sensitive. They are explained below. From class
×
Figure 12.2 The search dialogue
Search WWH ::




Custom Search