import javax.swing.*; //for the main JFrame design
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.DefaultEditorKit.CopyAction;
import java.awt.*; // for the GUI stuff
import java.awt.event.*; // for the even handling
import java.util.Scanner; // for reading from a file
import java.io.*; // for writing to a file
public class Notepad extends JFrame implements ActionListener {
private TextArea textArea = new TextArea("", 0, 0,
TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar(); // first create a MenuBar item
private Menu file = new Menu(); // our File menu
// what's going on in File? Let's see...
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();
private Menu actions = new Menu();
private MenuItem copy = new MenuItem();
private MenuItem paste = new MenuItem();
/*
* public void Notepad1(CopyAction copy){ copy = new
* DefaultEditorKit.CopyAction(); }
*/
// The constructor for Notepad
public Notepad() {
this.setSize(500, 300); // set the initial size of the window
this.setTitle("Java Notepad"); // set the title of the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close
// operation(exit when it gets closed)
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); // set
// a
// default font for the TextArea
this.getContentPane().setLayout(new BorderLayout()); // the
// BorderLayout bit makes it fill automatically
this.getContentPane().add(textArea);
// add out menu into the GUI
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file);
this.menuBar.add(this.actions);
this.file.setLabel("File");
this.actions.setLabel("Actions");
// add the open option
this.openFile.setLabel("Open"); // sets the label of the open option
this.openFile.addActionListener(this); // add action listener(so we know
// when its clicked)
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); // set
// a
// keyboard
// shortcut
this.file.add(this.openFile);
// and the save
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);
// and finally the close
this.close.setLabel("Close");
this.close.addActionListener(this);
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4));
this.file.add(this.close);
this.copy.setLabel("Copy");
this.copy.addActionListener(this);
this.copy.setShortcut(new MenuShortcut(KeyEvent.VK_C));
this.actions.add(this.copy);
this.paste.setLabel("Paste");
this.paste.addActionListener(this);
this.paste.setShortcut(new MenuShortcut(KeyEvent.VK_X));
this.actions.add(this.paste);
}
@Override
public void actionPerformed(ActionEvent e) {
// if the source of our code was the close option
if (e.getSource() == this.close)
this.dispose(); // dispose all the resources and close the
// application
/*
* else if (e.getSource() == this.copy) menuItem = new menuItem(new
* DefaultEditorKit.CopyAction());
*/
// if the source of our code was the open option
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser(); // open a file chooser
int option = open.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) { // get the option that
// the user selected
this.textArea.setText(""); // clear the text area before
// applying the file contents
try
// get a file scanner to read the path to the file
// (getSelectedFile().getPath() will get the path to the file)
{
Scanner scan = new Scanner(new FileReader(open
.getSelectedFile().getPath()));
while (scan.hasNext())
; // while there is something to read
this.textArea.append(scan.nextLine() + "/n"); // append the
// line to
// the text
// area
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
} else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser();
int option = save.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(save
.getSelectedFile().getPath()));
out.close(); // close the file stream
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}
public static void main(String args[]) {
Notepad app = new Notepad();
app.setVisible(true);
}
}
Question about a simple text editor
Page 1 of 13 Replies - 304 Views - Last Post: 26 June 2012 - 10:03 AM
#1
Question about a simple text editor
Posted 26 June 2012 - 07:18 AM
I have been following a tutorial here for creating a simple text editor. I am try to add some new features of my own such as copy & paste. Everything I have tried so far, does not work.
Replies To: Question about a simple text editor
#2
Re: Question about a simple text editor
Posted 26 June 2012 - 07:21 AM
Quote
Everything I have tried so far, does not work.
Would you elaborate on what this means?
#3
Re: Question about a simple text editor
Posted 26 June 2012 - 09:06 AM
#4
Re: Question about a simple text editor
Posted 26 June 2012 - 10:03 AM
Never mind. Every time I ask a question here, some guy has to post some bs. I figured it out myself.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|