Hi,
i am a beginner in
java . i m currently working on text editor . i m making a text editor . i have added the menubar to it with several menus . among them is font (as in normal text editors). i want my texteditor to open a separate dialog with several options in it (like font options , and bold italics options) when we click the font menuitem. what am i going to use for that ? Jdialog ?? or something else and how am i going to add components on it .. please guide . my text editor coding so far is listed below and its working fine . i have completed the file menu and insert menu and they are working well . now i m working on the format menu. i want to open a separate dialog (window) with options on it , when we click the font menuitem . please guide me .
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class textEditor {
private JFrame frame;
private JPanel panel1, panel2;
private JTextArea area;
private JLabel label1, label2, label3;
public static void main (
String[] args) {
textEditor te = new textEditor();
te.go();
}
public void go() {
frame = new JFrame("Untitled-Text Editor");
panel1 = new JPanel();
panel2 = new JPanel();
area = new JTextArea(25,70);
JScrollPane scroller = new JScrollPane(area);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
area.setLineWrap(true);
area.setWrapStyleWord(true);
label1 = new JLabel("Ln 1,Col 1");
label2 = new JLabel(" ");
label3 = new JLabel("Word Count: 0");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu insertMenu = new JMenu("Insert");
JMenu formatMenu = new JMenu("Format");
JMenu helpMenu = new JMenu("Help");
JMenuItem newMenuItem = new JMenuItem("New");
JMenuItem openMenuItem = new JMenuItem("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem exitMenuItem = new JMenuItem("Exit");
JMenuItem undoMenuItem = new JMenuItem("Undo");
JMenuItem cutMenuItem = new JMenuItem("Cut");
JMenuItem copyMenuItem = new JMenuItem("Copy");
JMenuItem pasteMenuItem = new JMenuItem("Paste");
JMenuItem findMenuItem = new JMenuItem("Find");
JMenuItem replaceMenuItem = new JMenuItem("Replace");
JMenuItem selectAllMenuItem = new JMenuItem("Select All");
JMenuItem dateTimeMenuItem = new JMenuItem("Date/Time");
JMenuItem fontMenuItem = new JMenuItem("Font");
JMenuItem bulletsMenuItem = new JMenuItem("Bullets and Numbering");
JMenuItem helpTopicsMenuItem = new JMenuItem("Help Topics");
JMenuItem aboutTextEditorMenuItem = new JMenuItem("About Text Editor");
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(exitMenuItem);
editMenu.add(undoMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
editMenu.add(findMenuItem);
editMenu.add(replaceMenuItem);
editMenu.add(selectAllMenuItem);
insertMenu.add(dateTimeMenuItem);
formatMenu.add(fontMenuItem);
formatMenu.add(bulletsMenuItem);
helpMenu.add(helpTopicsMenuItem);
helpMenu.add(aboutTextEditorMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(insertMenu);
menuBar.add(formatMenu);
menuBar.add(helpMenu);
frame.setJMenuBar(menuBar);
panel1.add(scroller);
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS) );
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
newMenuItem.addActionListener( new newMenuItemListener() );
openMenuItem.addActionListener( new openMenuItemListener() );
saveMenuItem.addActionListener( new saveMenuItemListener() );
exitMenuItem.addActionListener( new exitMenuItemListener() );
dateTimeMenuItem.addActionListener( new dateTimeMenuItemListener() );
//fontMenuItem.addActionListener( new fontMenuItemListener() );
//bulletsMenuItem.addActionListener( new bulletsMenuItemListener() );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,500);
frame.setVisible(true);
}
private void clearArea() {
area.setText("");
}
private void openFile(File file) {
frame.setTitle(file.getName() );
clearArea();
try {
BufferedReader reader = new BufferedReader( new FileReader(file) );
String line = null;
while ( (line = reader.readLine() ) != null) {
area.append(line);
area.append("\n");
}
reader.close();
} catch(IOException ex) {
System.out.println("cannot open file");
ex.printStackTrace();
}
}
private void saveFile(File file) {
frame.setTitle(file.getName() );
try {
FileWriter writer = new FileWriter(file);
writer.write(area.getText() );
writer.close();
} catch (IOException ex) {
System.out.println("cannot save file");
ex.printStackTrace();
}
}
public class newMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.setTitle("Untitled-Text Editor");
clearArea();
}
}
public class openMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JFileChooser fileOpen = new JFileChooser();
fileOpen.showOpenDialog(frame);
openFile(fileOpen.getSelectedFile() );
}
}
public class exitMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
System.exit(0);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public class saveMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(frame);
saveFile(fileSave.getSelectedFile() );
}
}
public class dateTimeMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String today = String.format("%tA, %<tB %<td %<tY %<tI:%<tM %<tp",new Date() );
area.append(today);
}
}
//public class fontMenuItemListener implements ActionListener {
//public void actionPerformed(ActionEvent event) {
}