Copy this first bit of code and save it as "EditorMain.java"
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class EditorMain implements ActionListener
{
// declare all the items that are needed for the gui
// or for proper execution of the program
private JFrame mainFrame = new JFrame("Useless Text Editor");
private JTabbedPane tabbedMain = new JTabbedPane();
private JMenuBar menu = new JMenuBar();
private JButton copyToUseless = new JButton("Copy to Useless Tab");
private JButton openButton = new JButton("Open");
private JMenu file = new JMenu("File");
private JMenu edit = new JMenu("Edit");
private JMenu about = new JMenu("About");
private JMenuItem newAction = new JMenuItem("New");
private JMenuItem openAction = new JMenuItem("Open");
private JMenuItem saveAction = new JMenuItem("Save");
private JMenuItem saveAsAction = new JMenuItem("Save As..");
private JMenuItem exitAction = new JMenuItem("Exit");
private JMenuItem cutAction = new JMenuItem("Cut");
private JMenuItem copyAction = new JMenuItem("Copy");
private JMenuItem pasteAction = new JMenuItem("Paste");
private JMenuItem aboutAction = new JMenuItem("About");
private JTextArea tab1 = new JTextArea();
private JTextArea tab2 = new JTextArea();
private JScrollPane scrollPane1 = new JScrollPane(tab1);
private JScrollPane scrollPane2 = new JScrollPane(tab2);
int width = 500;
int height = 500;
boolean running = true;
public String fName = null;
// create an astetically pleasing gui, that is dynamic
// to allow for the resizing of the screen
public void createGui()
{
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
mainFrame.setSize(width,height);
mainFrame.setLayout(null);
file.add(newAction);
file.add(openAction);
file.add(saveAction);
file.add(saveAsAction);
file.add(exitAction);
edit.add(cutAction);
edit.add(copyAction);
edit.add(pasteAction);
about.add(aboutAction);
menu.add(file);
menu.add(edit);
menu.add(about);
menu.setBounds(1,1, width, height/20);
setGuiBounds();
tab1.setEditable(true);
tab2.setEditable(false);
tabbedMain.setAutoscrolls(true);
tab1.setLineWrap(true);
tab2.setLineWrap(true);
tab1.setVisible(true);
tab2.setVisible(true);
tabbedMain.add("Useful Tab",scrollPane1);
tabbedMain.add("Useless Tab",scrollPane2);
copyToUseless.setBounds(200,height/20 + 2,170,21);
openButton.setBounds(380,height/20+2,100,21);
mainFrame.add(openButton);
mainFrame.add(copyToUseless);
mainFrame.add(tabbedMain);
mainFrame.add(menu);
}
// Adjust the gui to the screen everytime they move the edges
public void setGuiBounds()
{
width = mainFrame.getWidth();
height = mainFrame.getHeight();
menu.setBounds(1,1,width,height/20);
scrollPane1.setBounds(1,1,width-5,height-25);
scrollPane2.setBounds(1,1,width-5,height-25);
tabbedMain.setBounds(1,height/20,width-5,height-height/20 - 25);
copyToUseless.setBounds(200,height/20 + 2,170,21);
openButton.setBounds(380,height/20+2,100,21);
}
// main execution of the program. Open a thread
// for the gui, handle events etc.
public EditorMain()
{
Thread gui = new Thread(guiThread);
gui.start();
handleEvents();
}
// a seperate thread for the gui, so that it
// doesnt slow down the operation of the rest of the program
Runnable guiThread = new Runnable()
{
public void run()
{
createGui();
while(running)
{
long time = System.currentTimeMillis();
update();
try
{
long delay = Math.max(0, 62-(System.currentTimeMillis()-time));
Thread.sleep(delay);
}
catch(InterruptedException e)
{
System.out.println(e.toString());
}
}
}
};
public void update()
{
if(fName != null)
saveAction.setEnabled(true);
else
saveAction.setEnabled(false);
mainFrame.setTitle("Useless Text Editor: " + fName);
if(mainFrame.getWidth() != width || mainFrame.getHeight() != height)
{
width = mainFrame.getWidth();
height = mainFrame.getHeight();
setGuiBounds();
mainFrame.validate();
}
}
public void newFile()
{
final JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("New File");
chooser.setFileFilter(new fileFilter());
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
fName = new File(chooser.getSelectedFile(),"").toString();
tab1.setText("");
saveFile();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.toString());
}
}
}
public void openFile()
{
final JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Open a File");
chooser.setFileFilter(new fileFilter());
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
fName = new File(chooser.getSelectedFile(),"").toString();
Scanner fin = new Scanner(new File(chooser.getSelectedFile(),""));
String container = new String();
container = fin.nextLine();
while(fin.hasNextLine())
{
container = container + "\n" + fin.nextLine();
}
tab1.setText(container);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.toString());
}
}
}
public void cutText()
{
copyText();
tab1.replaceSelection("");
}
public void copyText()
{
StringSelection info = new StringSelection(tab1.getSelectedText());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(info, info);
}
public void pasteText(JTextArea areaToPutItIn)
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable clipInfo = clipboard.getContents(clipboard);
if (clipInfo != null) {
try
{
if (clipInfo.isDataFlavorSupported(DataFlavor.stringFlavor))
{
String clipString = (String)
(clipInfo.getTransferData(DataFlavor.stringFlavor));
areaToPutItIn.replaceSelection(clipString);
}
}
catch (UnsupportedFlavorException e)
{
System.err.println("Not Text!");
}
catch (IOException e)
{
System.err.println("Nothing in the clipbard!");
}
}
}
public void handleEvents()
{
newAction.addActionListener(this);
newAction.setActionCommand("new");
openAction.addActionListener(this);
openAction.setActionCommand("open");
openButton.addActionListener(this);
openButton.setActionCommand("open");
saveAction.addActionListener(this);
saveAction.setActionCommand("save");
saveAsAction.addActionListener(this);
saveAsAction.setActionCommand("saveAs");
cutAction.addActionListener(this);
cutAction.setActionCommand("cut");
copyAction.addActionListener(this);
copyAction.setActionCommand("copy");
pasteAction.addActionListener(this);
pasteAction.setActionCommand("paste");
aboutAction.addActionListener(this);
aboutAction.setActionCommand("about");
copyToUseless.addActionListener(this);
copyToUseless.setActionCommand("pasteToUseless");
}
public void saveFile()
{
try
{
FileWriter outStream;
if(!fName.endsWith("txt"))
outStream = new FileWriter(fName +".txt");
else
outStream = new FileWriter(fName);
BufferedWriter textOut = new BufferedWriter(outStream);
textOut.write(tab1.getText());
textOut.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "SHIIIIIITS BROKE",
"OOOH NOE",JOptionPane.ERROR_MESSAGE);
}
}
public void saveAsFile()
{
final JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Save As...");
chooser.setFileFilter(new fileFilter());
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
fName = new File(chooser.getSelectedFile(),"").toString();
saveFile();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if ("new".equals(e.getActionCommand()))
{
newFile();
}
if ("open".equals(e.getActionCommand()))
{
openFile();
}
if ("save".equals(e.getActionCommand()))
{
saveFile();
}
if ("saveAs".equals(e.getActionCommand()))
{
saveAsFile();
}
if ("cut".equals(e.getActionCommand()))
{
cutText();
}
if ("copy".equals(e.getActionCommand()))
{
copyText();
}
if ("paste".equals(e.getActionCommand()))
{
pasteText(tab1);
}
if ("pasteToUseless".equals(e.getActionCommand()))
{
tab2.setText("");
copyText();
pasteText(tab2);
}
if ("about".equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(null, "Useless Text Editor \nBeta Version 1.0" +
"\nCreator: JAIMES \nInstigator: Douglass",
"About",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args)
{
new EditorMain();
}
}
And this second one as "fileFilter.java"
import java.io.File;
import javax.swing.filechooser.*;
public class fileFilter extends FileFilter {
public fileFilter()
{
}
@Override
public boolean accept(File arg0) {
String name = new String(arg0.getName());
if(name.endsWith("txt"))
return true;
else
return false;
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return ".txt files";
}
}
Then play around with it and see how it works. pretty much you start by creating a frame and adding pieces to it in a way that makes it look presentable. this is a simple text editor that can open and save files, it also uses a thread to keep track of the changing gui. post any questions you might have
EDIT: also my commenting is pretty sparse, just play around with it and see if you can get the hang of it. if not, if i get time this week ill go overboard with the comments so you can have an in depth look at why things are done as they are