• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

coding a texteditor: want to open a separate option window

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {


}








 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you guys for helping me out.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would sit better on our GUIs forum. Moving thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic