I have made a small notepad demo and I am trying to open the file in same frame in the main frame but while opening the file it opens in different frame, I want it open in the first frame that opens when I run the program.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class DemoMenu extends Frame
{
Frame frm=new Frame();
MenuBar bar=new MenuBar();
//frm.setMenuBar(bar);
Menu mf=new Menu("File");
Menu me=new Menu("Edit");
Menu mfo=new Menu("Format");
Menu mv=new Menu("View");
Menu mh=new Menu("Help");
MenuItem mf1=new MenuItem("New"+" "+"Ctrl+N");
MenuItem mf2=new MenuItem("Open..."+" "+"Ctrl+O");
MenuItem mf3=new MenuItem("Save"+" "+"Ctrl+S");
MenuItem mf4=new MenuItem("Save As...");
MenuItem mf5=new MenuItem("Page Setup...");
MenuItem mf6=new MenuItem("Print..."+" "+"Ctrl+P");
MenuItem mf7=new MenuItem("Exit");
MenuItem me1=new MenuItem("Undo"+" "+"Ctrl+Z");
MenuItem me2=new MenuItem("Cut"+" "+"Ctrl+X");
MenuItem me3=new MenuItem("Copy"+" "+"Ctrl+C");
MenuItem me4=new MenuItem("Paste"+" "+"Ctrl+V");
MenuItem me5=new MenuItem("Delete"+" "+"Del" );
MenuItem me6=new MenuItem("Find..."+" "+"Ctrl+F");
MenuItem me7=new MenuItem("Find Next"+" "+"F3");
MenuItem me8=new MenuItem("Replace..."+" "+"Ctrl+H");
MenuItem me9=new MenuItem("Go To"+" "+"Ctrl+G");
MenuItem me10=new MenuItem("Select All"+" "+"Ctrl+A");
MenuItem me11=new MenuItem("Date/Time"+" "+"F5");
MenuItem mfo1=new MenuItem("Word Wrap");
MenuItem mfo2=new MenuItem("Font...");
MenuItem mv1=new MenuItem("Status Bar");
MenuItem mh1=new MenuItem("Help Topics");
MenuItem mh2=new MenuItem("About Notepad");
public static void main(String args[])
{
DemoMenu dm=new DemoMenu();
}
public DemoMenu()
{
super("United- Notepad");
setMenuBar(bar);
bar.add(mf);
bar.add(me);
bar.add(mfo);
bar.add(mv);
bar.add(mh);
mf.add(mf1);
mf.add(mf2);
mf.add(mf3);
mf.add(mf4);
mf.addSeparator();
mf.add(mf5);
mf.add(mf6);
mf.addSeparator();
mf.add(mf7);
mf1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane smd=new JOptionPane();
smd.showMessageDialog(null,"Press Ok to open new page");
}
});
final JTextArea jta=new JTextArea(10,20);
JScrollPane scrl=new JScrollPane(jta);
frm.add(scrl);
mf2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser=new JFileChooser();
int selection=chooser.showOpenDialog(frm);
if(selection==JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null,"so u want to open the file...");
frm.setSize(520,540);
frm.setVisible(true);
File file = chooser.getSelectedFile();
FileInputStream fis=null;
try
{
fis=new FileInputStream(file);
byte b[]=new byte[fis.available()];
fis.read(b);
String str=new String(b);
jta.setText(str);
}
catch(Exception f)
{
f.printStackTrace();
}
}
}
});
mf3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser=new JFileChooser();
chooser.showSaveDialog(null);
}
});
mf4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser=new JFileChooser();
chooser.showDialog(null,"Save As");
}
});
mf7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane scd=new JOptionPane();
scd.showConfirmDialog(null,"Are you want to exit");
}
});
me.add(me1);
me.addSeparator();
me.add(me2);
me.add(me3);
me.add(me4);
me.add(me5);
me.addSeparator();
me.add(me6);
me.add(me7);
me.add(me8);
me.add(me9);
me.addSeparator();
me.add(me10);
me.add(me11);
me11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date d1=new Date();
JOptionPane smddate=new JOptionPane();
smddate.showMessageDialog(null,d1);
}
});
mfo.add(mfo1);
mfo.add(mfo2);
mv.add(mv1);
mh.add(mh1);
mh.addSeparator();
mh.add(mh2);
setSize(520,540);
setVisible(true);
}
}