aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes coding a texteditor: want to open a separate option window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "coding a texteditor: want to open a separate option window" Watch "coding a texteditor: want to open a separate option window" New topic
Author

coding a texteditor: want to open a separate option window

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
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) {


}









SCJP 6.0 (86%) SCWCD 5 (94%)
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

Welcome to the ranch.

For code posting please use code tags.

For the text editor, a JDialog will work well for your (font or whatever) options panel. The potential problem you may get into is how are you going to propapate those changes back to the text area.

Of course there's always a way And it depends on how you write your main GUI class. Currently you have everything (text area, menus, panels buttons) in the main class. I suggest you can use more of a OO approach by say separating the menu bar to a new class extends JMenuBar and load your new menu class in the main class.

Hope this help and good luck with your app.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
The code below is a simple frame that has a button. when i click the button i want to open a dialog with options (including checkboxes, and combobox) . can you please tell me how to create and show that dialog . I am still not able to understand how to use Jdialog box . If i get to understand this basic thing here in this simple code , I will be able to do it in my textEditor application.I want the dialog box to generate in the action performed method
Please reply.thanks.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
A JDialog works exactly like a JFrame. You create it, add components to it and set it visible.

So if you know how to create a frame you know how to create a dialog.
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Thanks you guys for helping me out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Would sit better on our GUIs forum. Moving thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: coding a texteditor: want to open a separate option window