Upload
ashutoshvashist
View
4
Download
0
Embed Size (px)
DESCRIPTION
Text Ed java
Citation preview
package editor;//Packages we will needimport java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.swing.text.*;//The classpublic class TextEditor extends JFrame{
//Basic Variables//Our JTextArea will be the area where we can write documents and etc...private JTextArea area = new JTextArea(20,120);private JFileChooser dialog = new JFileChooser(System.getProperty("user.
dir"));//JFileChooser is a component which provides a simple mechanism for the
user to choose a file.private String currentFile = "Untitled";private boolean changed = false; private JMenuBar menuBar;
private JMenu fontMenu;// The checkbox menu items private JCheckBoxMenuItem italicItem; private JCheckBoxMenuItem boldItem;// The radio button menu items private JRadioButtonMenuItem monoItem; private JRadioButtonMenuItem serifItem; private JRadioButtonMenuItem sansSerifItem; private JTextArea editorText; //Constructor Part #1public TextEditor() {
area.setFont(new Font("Monospaced",Font.PLAIN,12));JScrollPane scroll = new JScrollPane(area,JScrollPane.VERTICAL_S
CROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);add(scroll,BorderLayout.CENTER);
JMenuBar JMB = new JMenuBar();setJMenuBar(JMB);JMenu file = new JMenu("File");JMenu edit = new JMenu("Edit");JMB.add(file); JMB.add(edit);//Constructor Part #2file.add(New);file.add(Open);file.add(Save);file.add(Quit);file.add(SaveAs);file.addSeparator();
for(int i=0; i<4; i++)file.getItem(i).setIcon(null);
edit.add(Cut);edit.add(Copy);edit.add(Paste);
edit.getItem(0).setText("Cut out");edit.getItem(1).setText("Copy");edit.getItem(2).setText("Paste");//Constructor Part #3JToolBar tool = new JToolBar();
add(tool,BorderLayout.NORTH);tool.add(New);tool.add(Open);tool.add(Save);tool.addSeparator();
JButton cut = tool.add(Cut), cop = tool.add(Copy),pas = tool.add(Paste);
cut.setText(null); cut.setIcon(new ImageIcon("C:\\Users\\BARMER\\Desktop\\cut.gif"));
cop.setText(null); cop.setIcon(new ImageIcon("C:\\Users\\BARMER\\Desktop\\copy.gif"));
pas.setText(null); pas.setIcon(new ImageIcon("C:\\Users\\BARMER\\Desktop\\paste.gif"));
Save.setEnabled(false);SaveAs.setEnabled(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);pack();area.addKeyListener(k1);
setTitle(currentFile);
setVisible(true);} private void buildMenuBar() { // Build the file and font menus. // buildFileMenu(); buildFontMenu();
// Create the menu bar. menuBar = new JMenuBar();
// add font menus to the menu bar. menuBar.add(fontMenu);
// Set the menu bar for this frame. setJMenuBar(menuBar); }private void buildFontMenu() { // Create the Monospaced menu item. monoItem = new JRadioButtonMenuItem("Monospaced"); monoItem.addActionListener(new FontListener());
// Create the Serif menu item. serifItem = new JRadioButtonMenuItem("Serif"); serifItem.addActionListener(new FontListener());
// Create the SansSerif menu item. sansSerifItem = new JRadioButtonMenuItem("SansSerif", true); sansSerifItem.addActionListener(new FontListener());
// Group the radio button menu items. ButtonGroup group = new ButtonGroup();
group.add(monoItem); group.add(serifItem); group.add(sansSerifItem);
// Create the Italic menu item. italicItem = new JCheckBoxMenuItem("Italic"); italicItem.addActionListener(new FontListener());
// Create the Bold menu item. boldItem = new JCheckBoxMenuItem("Bold"); boldItem.addActionListener(new FontListener());
// Create a menu for the items we just created. fontMenu = new JMenu("Font"); fontMenu.setMnemonic(KeyEvent.VK_T);
// Add the items and some separator bars to the menu. fontMenu.add(monoItem); fontMenu.add(serifItem); fontMenu.add(sansSerifItem); fontMenu.addSeparator();// Separator bar fontMenu.add(italicItem); fontMenu.add(boldItem); }
//Listenerprivate KeyListener k1 = new KeyAdapter() {
public void keyPressed(KeyEvent e) {changed = true;Save.setEnabled(true);SaveAs.setEnabled(true);
}};//Action -1Action New = new AbstractAction("New", new ImageIcon("C:\\Users\
\BARMER\\Desktop\\new.gif")) {public void actionPerformed(ActionEvent e) {
}};//Action 2Action Open = new AbstractAction("Open", new ImageIcon("C:\\User
s\\BARMER\\Desktop\\open.gif")) {public void actionPerformed(ActionEvent e) {
saveOld();if(dialog.showOpenDialog(null)==JFileChooser.APP
ROVE_OPTION) {readInFile(dialog.getSelectedFile().getA
bsolutePath());}SaveAs.setEnabled(true);
}};//Action 3Action Save = new AbstractAction("Save", new ImageIcon("C:\\User
s\\BARMER\\Desktop\\save.gif")) {public void actionPerformed(ActionEvent e) {
if(!currentFile.equals("Untitled"))saveFile(currentFile);
elsesaveFileAs();
}};//Action 4Action SaveAs = new AbstractAction("Save as...") {
public void actionPerformed(ActionEvent e) {saveFileAs();
}};//Action 5Action Quit = new AbstractAction("Quit") {
public void actionPerformed(ActionEvent e) {saveOld();System.exit(0);
}};//Action mappingActionMap m = area.getActionMap();Action Cut = m.get(DefaultEditorKit.cutAction);Action Copy = m.get(DefaultEditorKit.copyAction);Action Paste = m.get(DefaultEditorKit.pasteAction);//Method 1private void saveFileAs() {
if(dialog.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
saveFile(dialog.getSelectedFile().getAbsolutePath());
}//Method 2private void saveOld() {
if(changed) {if(JOptionPane.showConfirmDialog(this, "Would yo
u like to save "+ currentFile +" ?","Save",JOptionPane.YES_NO_OPTION)== JOptionPane.YES_OPTION)
saveFile(currentFile);}
}//Method 3private void readInFile(String fileName) {
try {FileReader r = new FileReader(fileName);area.read(r,null);r.close();currentFile = fileName;setTitle(currentFile);changed = false;
}catch(IOException e) {
Toolkit.getDefaultToolkit().beep();JOptionPane.showMessageDialog(this,"Editor can't
find the file called "+fileName);}
}//Method 4private void saveFile(String fileName) {
try {FileWriter w = new FileWriter(fileName);area.write(w);w.close();currentFile = fileName;
setTitle(currentFile);changed = false;Save.setEnabled(false);
}catch(IOException e) {}
}private class FontListener implements ActionListener
{ public void actionPerformed(ActionEvent e) { // Get the current font. Font textFont = editorText.getFont();
// Retrieve the font name and size. String fontName = textFont.getName(); int fontSize = textFont.getSize();
// Start with plain style. int fontStyle = Font.PLAIN;
// Determine which font is selected. if (monoItem.isSelected()) fontName = "Monospaced"; else if (serifItem.isSelected()) fontName = "Serif"; else if (sansSerifItem.isSelected()) fontName = "SansSerif";
// Determine whether italic is selected. if (italicItem.isSelected()) fontStyle += Font.ITALIC;
// Determine whether bold is selected. if (boldItem.isSelected()) fontStyle += Font.BOLD;
// Set the font as selected. editorText.setFont(new Font(fontName, fontStyle, fontSize)); } }//the mainpublic static void main(String[] arg) {
new TextEditor();
}
}