answersLogoWhite

0

addWindowListener is a method in Java's AWT (Abstract Window Toolkit) that allows you to register an event listener to receive notifications about window events, such as opening, closing, minimizing, or resizing a window. By implementing the WindowListener interface, you can define specific actions to take when these events occur. This method is commonly used in GUI applications to manage window behavior and enhance user interaction.

User Avatar

AnswerBot

2w ago

What else can I help you with?

Continue Learning about Engineering

Source code for notepad in java?

import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.io.*; public class Editor extends Frame { String filename; TextArea tx; Clipboard clip = getToolkit().getSystemClipboard(); Editor() { setLayout(new GridLayout(1,1)); tx = new TextArea(); add(tx); MenuBar mb = new MenuBar(); Menu F = new Menu("file"); MenuItem n = new MenuItem("New"); MenuItem o = new MenuItem("Open"); MenuItem s = new MenuItem("Save"); MenuItem e = new MenuItem("Exit"); n.addActionListener(new New()); F.add(n); o.addActionListener(new Open()); F.add(o); s.addActionListener(new Save()); F.add(s); e.addActionListener(new Exit()); F.add(e); mb.add(F); Menu E = new Menu("Edit"); MenuItem cut = new MenuItem("Cut"); MenuItem copy = new MenuItem("Copy"); MenuItem paste = new MenuItem("Paste"); cut.addActionListener(new Cut()); E.add(cut); copy.addActionListener(new Copy()); E.add(copy); paste.addActionListener(new Paste()); E.add(paste); mb.add(E); setMenuBar(mb); mylistener mylist = new mylistener(); addWindowListener(mylist); } class mylistener extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } class New implements ActionListener { public void actionPerformed(ActionEvent e) { tx.setText(" "); setTitle(filename); } } class Open implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this, "select File",FileDialog.LOAD); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); ReadFile(); } tx.requestFocus(); } } class Save implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); try { DataOutputStream d = new DataOutputStream(new FileOutputStream(filename)); String line = tx.getText(); BufferedReader br = new BufferedReader(new StringReader(line)); while((line = br.readLine())!=null) { d.writeBytes(line + "\r\n"); d.close(); } } catch(Exception ex) { System.out.println("File not found"); } tx.requestFocus(); } } } class Exit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } void ReadFile() { BufferedReader d; StringBuffer sb = new StringBuffer(); try { d = new BufferedReader(new FileReader(filename)); String line; while((line=d.readLine())!=null) sb.append(line + "\n"); tx.setText(sb.toString()); d.close(); } catch(FileNotFoundException fe) { System.out.println("File not Found"); } catch(IOException ioe){} } class Cut implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection ss = new StringSelection(sel); clip.setContents(ss,ss); tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd()); } } class Copy implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection clipString = new StringSelection(sel); clip.setContents(clipString,clipString); } } class Paste implements ActionListener { public void actionPerformed(ActionEvent e) { Transferable cliptran = clip.getContents(Editor.this); try { String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor); tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd()); } catch(Exception exc) { System.out.println("not string flavour"); } } } public static void main(String args[]) { Frame f = new Editor(); f.setSize(500,400); f.setVisible(true); f.show(); } }


Related Questions

How do you create a note pad in java?

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package simpletexteditor; /** * * @author Ibrahim Alwazir */ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; class MyPad extends JFrame implements ActionListener, KeyListener { boolean txtChanged = false; String fname = ""; JMenuBar mbar; JMenu mnuFile, mnuEdit; JMenuItem fileNew, fileOpen, fileSave, fileExit; JMenuItem editCut, editCopy, editPaste, editSelectAll, editDel; JToolBar tlbr; ImageIcon iconNew, iconOpen, iconSave; ImageIcon iconCut, iconCopy, iconPaste; JButton bttnNew, bttnOpen, bttnSave; JButton bttnCut, bttnCopy, bttnPaste; JCheckBoxMenuItem wordWrap; JTextArea txtPad; Container c; MyPad() { initComponents(); setTitle("Simple Text Editor"); setSize(400,300); setVisible(true); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WinHandler()); } void initComponents() { //get a handle (reference) to the //content pane of window c = getContentPane(); //set BorderLayout Manager to arrange components in content pane c.setLayout(new BorderLayout()); initMenu(); //create textarea txtPad = new JTextArea(); Font f = new Font("Arial", Font.PLAIN, 20); txtPad.setFont(f); txtPad.addKeyListener(this); txtPad.setLineWrap(false); /* Sets word wrap style to false. */ txtPad.setWrapStyleWord(false); //add txtPad in scrollpane JScrollPane jscroll = new JScrollPane(txtPad); //add scrollpane in window c.add(jscroll, BorderLayout.CENTER); } void initMenu() { //create menubar mbar = new JMenuBar(); //create menus mnuFile = new JMenu("File"); mnuEdit = new JMenu("Edit"); //create menuitems fileNew = new JMenuItem("New"); fileOpen= new JMenuItem("Open"); fileSave= new JMenuItem("Save"); fileExit = new JMenuItem("Exit"); wordWrap = new JCheckBoxMenuItem("word wrap"); editCut = new JMenuItem("Cut"); editCopy= new JMenuItem("Copy"); editPaste = new JMenuItem("Paste"); editSelectAll = new JMenuItem("Select All"); editDel= new JMenuItem("Delete"); //add menuitems in menus mnuFile.add(fileNew); mnuFile.add(fileOpen); mnuFile.add(fileSave); mnuFile.add(fileExit); mnuEdit.add(editCut); mnuEdit.add(editCopy); mnuEdit.add(editPaste); mnuEdit.addSeparator(); mnuEdit.add(editSelectAll); mnuEdit.add(editDel); mnuEdit.add(wordWrap); //attach menus to menubar mbar.add(mnuFile); mbar.add(mnuEdit); //attach menubar to window setJMenuBar(mbar); //attach actionlister to menuitems fileNew.addActionListener(this); fileOpen.addActionListener(this); fileSave.addActionListener(this); fileExit.addActionListener(this); editCut.addActionListener(this); editCopy.addActionListener(this); editPaste.addActionListener(this); editSelectAll.addActionListener(this); editDel.addActionListener(this); wordWrap.addActionListener(this); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if(src.equals(fileNew)) { newFile(); } else if(src.equals(fileOpen) ) { openFile(); } else if(src.equals(fileSave) ) { saveFile(); } else if(src.equals(fileExit)) { exitFile(); } else if(src.equals(wordWrap)) { /* Calls the warpFile() method. */ wrapFile(); } else if(src.equals(editCut)) { txtPad.cut(); } else if(src.equals(editCopy) ) { txtPad.copy(); } else if(src.equals(editPaste)) { txtPad.paste(); } else if(src.equals(editSelectAll)) { txtPad.selectAll(); } else if(src.equals(editDel)) { //replace selected content with a blank txtPad.replaceSelection(""); } }//end of actionPerformed void newFile() { if(txtChanged true) { /* Sets the line and word wrap style to TRUE. */ txtPad.setLineWrap(true); txtPad.setWrapStyleWord(true); } else { /* Sets the line and word wrap style to FALSE. */ txtPad.setLineWrap(false); txtPad.setWrapStyleWord(false); } } public void keyPressed(KeyEvent e) { //System.out.println("KP"); } public void keyReleased(KeyEvent e) { //System.out.println("KR"); } public void keyTyped(KeyEvent e) { txtChanged = true; } class WinHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { exitFile(); } } public static void main(String args[]) { MyPad mp = new MyPad(); } } class HelpDlg extends JDialog { public HelpDlg(MyPad m) { //MyPad object is the parent window of this dialog super(m,true); //register it //m is the MYPad ref that will be the owner //true means current dialog will be a modal window setSize(200, 150); setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } }


Source code for notepad in java?

import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.io.*; public class Editor extends Frame { String filename; TextArea tx; Clipboard clip = getToolkit().getSystemClipboard(); Editor() { setLayout(new GridLayout(1,1)); tx = new TextArea(); add(tx); MenuBar mb = new MenuBar(); Menu F = new Menu("file"); MenuItem n = new MenuItem("New"); MenuItem o = new MenuItem("Open"); MenuItem s = new MenuItem("Save"); MenuItem e = new MenuItem("Exit"); n.addActionListener(new New()); F.add(n); o.addActionListener(new Open()); F.add(o); s.addActionListener(new Save()); F.add(s); e.addActionListener(new Exit()); F.add(e); mb.add(F); Menu E = new Menu("Edit"); MenuItem cut = new MenuItem("Cut"); MenuItem copy = new MenuItem("Copy"); MenuItem paste = new MenuItem("Paste"); cut.addActionListener(new Cut()); E.add(cut); copy.addActionListener(new Copy()); E.add(copy); paste.addActionListener(new Paste()); E.add(paste); mb.add(E); setMenuBar(mb); mylistener mylist = new mylistener(); addWindowListener(mylist); } class mylistener extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } class New implements ActionListener { public void actionPerformed(ActionEvent e) { tx.setText(" "); setTitle(filename); } } class Open implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this, "select File",FileDialog.LOAD); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); ReadFile(); } tx.requestFocus(); } } class Save implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); try { DataOutputStream d = new DataOutputStream(new FileOutputStream(filename)); String line = tx.getText(); BufferedReader br = new BufferedReader(new StringReader(line)); while((line = br.readLine())!=null) { d.writeBytes(line + "\r\n"); d.close(); } } catch(Exception ex) { System.out.println("File not found"); } tx.requestFocus(); } } } class Exit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } void ReadFile() { BufferedReader d; StringBuffer sb = new StringBuffer(); try { d = new BufferedReader(new FileReader(filename)); String line; while((line=d.readLine())!=null) sb.append(line + "\n"); tx.setText(sb.toString()); d.close(); } catch(FileNotFoundException fe) { System.out.println("File not Found"); } catch(IOException ioe){} } class Cut implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection ss = new StringSelection(sel); clip.setContents(ss,ss); tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd()); } } class Copy implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection clipString = new StringSelection(sel); clip.setContents(clipString,clipString); } } class Paste implements ActionListener { public void actionPerformed(ActionEvent e) { Transferable cliptran = clip.getContents(Editor.this); try { String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor); tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd()); } catch(Exception exc) { System.out.println("not string flavour"); } } } public static void main(String args[]) { Frame f = new Editor(); f.setSize(500,400); f.setVisible(true); f.show(); } }