Hi,
I have a java task to complete and I was hoping if some kind person can give me some help in completing it. I have been given a simple text editor program.
I need to add a search button to it, that will automatically run a method that searches a loaded textfile and finds the number of occurrences for the following words in an loaded text file:
The / the
of
program
components
GUI
I have made the search button but what code do I need to put into the search buttons method to make it search for the above words in any loaded text file.
The text file will contain the following text:
"Perhaps the most interesting feature of the editor program is how much of the behaviour is handled by the GUI components. The program code presented essentially creates the components and fits them together with some ?glue? code. Very little of the complicated activity of displaying and editing text needs to be programmed by the programmer, instead pre-built components are plugged together and a useful program quickly constructed."
I appreciate any help that can be offered.
Here is the code:
_________________________________________________________________________________________________
//Example Swing program to illustrate a very simple text editor
//Need to import classes from all these packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;
public class VerySimpleEditorTwo extends JFrame {
//Instance variables for the GUI components used.
//Declaring the variables here allows them to be
//used by any instance methods in the class.
JPanel topPanel;
JPanel buttonPanel;
JPanel editorPanel;
JButton loadButton;
JButton saveButton;
JButton cutButton;
JButton copyButton;
JButton pasteButton;
//new-------------------> New changes
JButton basicsearchButton;
JScrollPane scroller;
JTextArea editor;
//The constructor build the window using a utility
//method and displays it.
public VerySimpleEditorTwo() {
//Create and configure the various panels
topPanel = new JPanel ();
topPanel.setLayout (new BorderLayout ());
editorPanel = new JPanel ();
editorPanel.setLayout(new BorderLayout ());
editorPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
buttonPanel = new JPanel ();
buttonPanel.setBorder(BorderFactory.createEtchedBorder ());
//The panel containing the buttons uses a GridLayout of 1 column
//and 5 rows to position the buttons in a column.
GridLayout grid = new GridLayout ();
grid.setColumns(1);
grid.setHgap(10);
grid.setRows(5); //Set a small gap between the buttons
grid.setVgap(3); //to improve the appearance
buttonPanel.setLayout(grid);
scroller = new JScrollPane();
//The editor component handles all text display and editing. It
//is given a width of 40 characters.
editor = new JTextArea ();
editor.setColumns(40);
//Now create the buttons, giving each a listener to respond to
//the button being clicked. The listener methods delegate to
//private helper methods later in the class, to avoid the
//constructor method becoming too long and unwieldy.
//----------->Added Search Button
basicsearchButton = new JButton("Basic Search...");
basicsearchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
basicsearchFile();
}
});
/////////////////////////////////////////////////////
loadButton = new JButton("Load...");
loadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile();
}
});
saveButton = new JButton("Save...");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
cutButton = new JButton("Cut");
cutButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
});
copyButton = new JButton("Copy");
copyButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
});
pasteButton = new JButton("Paste");
pasteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
});
//Configure the top-level JFrame
setTitle("Very simple editor V2.0 by Ranatech Corporation TM");
getContentPane().add(topPanel, BorderLayout.CENTER);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Now assemble the components
topPanel.add(buttonPanel, BorderLayout.WEST);
buttonPanel.add(loadButton);
buttonPanel.add(basicsearchButton);
buttonPanel.add(saveButton);
buttonPanel.add(cutButton);
buttonPanel.add(copyButton);
buttonPanel.add(pasteButton);
topPanel.add(editorPanel, BorderLayout.CENTER);
editorPanel.add(scroller, BorderLayout.CENTER);
scroller.getViewport().add(editor);
pack();
setVisible(true);
}
//The following methods implement the button click behaviour.
//Display a file dialog and load a file.
private void loadFile() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//If a file was selected, try and load it.
File file = fc.getSelectedFile();
try {
editor.read(new FileReader(file), null);
}
catch (IOException exp) {}
}
}
//Display a save file dialog and save the current file.
private void saveFile() {
JFileChooser fc = new JFileChooser ();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
editor.write(new FileWriter(file));
}
catch (IOException exp) {}
}
}
//Searches an imported file for selected words
private void basicsearchFile() {
// WHAT CODE TO PUT HERE?
// A JOptionPane message box, displaying the words and the number of times they appear in the loaded text file would be great.
// The words the code must look for are: The / the , of , program , components, GUI
}
//Cut, copy and paste are implemented using methods provided by a
//superclass of the JTextArea class, and work with the system
//clipboard. Very useful and makes it very easy to implement
//cut/copy/paste!!
//
//The requestFocus method is used to make the JTextArea the active
//component after a button is clicked. The component with the focus
//receives the input events. If focus is not returned to the
//JTextArea it remains with the button, preventing text being
//entered into the text area until it is clicked on to regain
//focus.
private void copy() {
editor.copy();
editor.requestFocus();
}
private void paste() {
editor.paste();
editor.requestFocus();
}
private void cut() {
editor.cut();
editor.requestFocus();
}
public static void main(final String[] args) {
final VerySimpleEditorTwo editor = new VerySimpleEditorTwo ();
}
}
_____________________________________________
Thanks in advance
I have a java task to complete and I was hoping if some kind person can give me some help in completing it. I have been given a simple text editor program.
I need to add a search button to it, that will automatically run a method that searches a loaded textfile and finds the number of occurrences for the following words in an loaded text file:
The / the
of
program
components
GUI
I have made the search button but what code do I need to put into the search buttons method to make it search for the above words in any loaded text file.
The text file will contain the following text:
"Perhaps the most interesting feature of the editor program is how much of the behaviour is handled by the GUI components. The program code presented essentially creates the components and fits them together with some ?glue? code. Very little of the complicated activity of displaying and editing text needs to be programmed by the programmer, instead pre-built components are plugged together and a useful program quickly constructed."
I appreciate any help that can be offered.
Here is the code:
_________________________________________________________________________________________________
//Example Swing program to illustrate a very simple text editor
//Need to import classes from all these packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;
public class VerySimpleEditorTwo extends JFrame {
//Instance variables for the GUI components used.
//Declaring the variables here allows them to be
//used by any instance methods in the class.
JPanel topPanel;
JPanel buttonPanel;
JPanel editorPanel;
JButton loadButton;
JButton saveButton;
JButton cutButton;
JButton copyButton;
JButton pasteButton;
//new-------------------> New changes
JButton basicsearchButton;
JScrollPane scroller;
JTextArea editor;
//The constructor build the window using a utility
//method and displays it.
public VerySimpleEditorTwo() {
//Create and configure the various panels
topPanel = new JPanel ();
topPanel.setLayout (new BorderLayout ());
editorPanel = new JPanel ();
editorPanel.setLayout(new BorderLayout ());
editorPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
buttonPanel = new JPanel ();
buttonPanel.setBorder(BorderFactory.createEtchedBorder ());
//The panel containing the buttons uses a GridLayout of 1 column
//and 5 rows to position the buttons in a column.
GridLayout grid = new GridLayout ();
grid.setColumns(1);
grid.setHgap(10);
grid.setRows(5); //Set a small gap between the buttons
grid.setVgap(3); //to improve the appearance
buttonPanel.setLayout(grid);
scroller = new JScrollPane();
//The editor component handles all text display and editing. It
//is given a width of 40 characters.
editor = new JTextArea ();
editor.setColumns(40);
//Now create the buttons, giving each a listener to respond to
//the button being clicked. The listener methods delegate to
//private helper methods later in the class, to avoid the
//constructor method becoming too long and unwieldy.
//----------->Added Search Button
basicsearchButton = new JButton("Basic Search...");
basicsearchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
basicsearchFile();
}
});
/////////////////////////////////////////////////////
loadButton = new JButton("Load...");
loadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile();
}
});
saveButton = new JButton("Save...");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
cutButton = new JButton("Cut");
cutButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
});
copyButton = new JButton("Copy");
copyButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
});
pasteButton = new JButton("Paste");
pasteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
});
//Configure the top-level JFrame
setTitle("Very simple editor V2.0 by Ranatech Corporation TM");
getContentPane().add(topPanel, BorderLayout.CENTER);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Now assemble the components
topPanel.add(buttonPanel, BorderLayout.WEST);
buttonPanel.add(loadButton);
buttonPanel.add(basicsearchButton);
buttonPanel.add(saveButton);
buttonPanel.add(cutButton);
buttonPanel.add(copyButton);
buttonPanel.add(pasteButton);
topPanel.add(editorPanel, BorderLayout.CENTER);
editorPanel.add(scroller, BorderLayout.CENTER);
scroller.getViewport().add(editor);
pack();
setVisible(true);
}
//The following methods implement the button click behaviour.
//Display a file dialog and load a file.
private void loadFile() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//If a file was selected, try and load it.
File file = fc.getSelectedFile();
try {
editor.read(new FileReader(file), null);
}
catch (IOException exp) {}
}
}
//Display a save file dialog and save the current file.
private void saveFile() {
JFileChooser fc = new JFileChooser ();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
editor.write(new FileWriter(file));
}
catch (IOException exp) {}
}
}
//Searches an imported file for selected words
private void basicsearchFile() {
// WHAT CODE TO PUT HERE?
// A JOptionPane message box, displaying the words and the number of times they appear in the loaded text file would be great.
// The words the code must look for are: The / the , of , program , components, GUI
}
//Cut, copy and paste are implemented using methods provided by a
//superclass of the JTextArea class, and work with the system
//clipboard. Very useful and makes it very easy to implement
//cut/copy/paste!!
//
//The requestFocus method is used to make the JTextArea the active
//component after a button is clicked. The component with the focus
//receives the input events. If focus is not returned to the
//JTextArea it remains with the button, preventing text being
//entered into the text area until it is clicked on to regain
//focus.
private void copy() {
editor.copy();
editor.requestFocus();
}
private void paste() {
editor.paste();
editor.requestFocus();
}
private void cut() {
editor.cut();
editor.requestFocus();
}
public static void main(final String[] args) {
final VerySimpleEditorTwo editor = new VerySimpleEditorTwo ();
}
}
_____________________________________________
Thanks in advance