Â
Text Editor
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MBC
*/
public class TextEditor extends JApplet
{
private ImageIcon openImageIcon =
new ImageIcon(getClass().getResource("open.gif"));
private ImageIcon saveImageIcon =
new ImageIcon(getClass().getResource("save.gif"));
private JMenuItem jmiOpen = new JMenuItem("Open", openImageIcon);
private JMenuItem jmiSave = new JMenuItem("Save", saveImageIcon);
private JMenuItem jmiClear = new JMenuItem("Clear");
private JMenuItem jmiExit = new JMenuItem("Exit");
private JMenuItem jmiForeground = new JMenuItem("Foreground");
private JMenuItem jmiBackground = new JMenuItem("Background");
private JButton jbtOpen = new JButton(openImageIcon);
private JButton jbtSave = new JButton(saveImageIcon);
private JLabel jlblStatus = new JLabel();
private JFileChooser jFileChooser1 =
new JFileChooser(new File("."));
private JTextArea jta = new JTextArea();
public TextEditor()
{
JMenu jMenu1 = new JMenu("File");
jMenu1.add(jmiOpen);
jMenu1.add(jmiSave);
jMenu1.add(jmiClear);
jMenu1.addSeparator();
jMenu1.add(jmiExit);
JMenu jMenu2 = new JMenu("Edit");
jMenu2.add(jmiForeground);
jMenu2.add(jmiBackground);
JMenuBar jMenuBar1 = new JMenuBar();
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
JToolBar jToolBar1 = new JToolBar();
jToolBar1.add(jbtOpen);
jToolBar1.add(jbtSave);
jmiOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
open();
}
});
jmiSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
save();
}
});
jmiClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
jta.setText(null);
}
});
jmiExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
jmiForeground.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Color selectedColor =
JColorChooser.showDialog(
null, "Choose Foreground color", jta.getForeground());
if (selectedColor != null)
jta.setForeground(selectedColor);
}
});
jmiForeground.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Color selectedColor =
JColorChooser.showDialog(
null, "Choose Background color", jta.getBackground());
if (selectedColor != null)
jta.setBackground(selectedColor);
}
});
jbtOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
open();
}
});
jbtSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
save();
}
});
add(jToolBar1, BorderLayout.NORTH);
add(jlblStatus, BorderLayout.SOUTH);
add(new JScrollPane(jta), BorderLayout.CENTER);
}
private void open()
{
if (jFileChooser1.showOpenDialog(this) ==
JFileChooser.APPROVE_OPTION)
open(jFileChooser1.getSelectedFile());
}
private void open(File file)
{
try
{
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file));
byte[] b = new byte[in.available()];
in.read(b, 0, b.length);
jta.append(new String(b, 0, b.length));
in.close();
jlblStatus.setText(file.getName() + " Opened");
}
catch (IOException ex)
{
jlblStatus.setText("Error Opening " + file.getName());
}
}
private void save()
{
if (jFileChooser1.showSaveDialog(this) ==
JFileChooser.APPROVE_OPTION)
save(jFileChooser1.getSelectedFile());
}
private void save(File file)
{
try
{
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
byte[] b = (jta.getText()).getBytes();
out.write(b, 0, b.length);
out.close();
jlblStatus.setText(file.getName() + " Saved");
}
catch (IOException ex)
{
jlblStatus.setText("Error Saving " + file.getName());
}
}
}