Note that there are some explanatory texts on larger screens.

plurals
  1. POInclude "New" in file menu in notepad program
    text
    copied!<p>Need only this <code>if(e.getSource() == this.newFile)</code> part's answer and if the <code>textArea</code> contains any text then prompt "Do you want to save this file or <strong>DISCARD</strong> to NewFile?"</p> <pre><code>import java.awt.BorderLayout; import java.awt.Font; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.MenuShortcut; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.util.Scanner; import javax.swing.JFileChooser; import javax.swing.JFrame; public class Notepad extends JFrame implements ActionListener { private TextArea textArea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); private MenuBar menuBar = new MenuBar(); private Menu File = new Menu(); private MenuItem newFile = new MenuItem(); private MenuItem openFile = new MenuItem(); private MenuItem saveFile = new MenuItem(); private MenuItem close = new MenuItem(); Notepad(){ this.setSize(500,300); this.setTitle("Java Notepad"); setDefaultCloseOperation(EXIT_ON_CLOSE); this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(textArea); this.setMenuBar(this.menuBar); this.menuBar.add(File); this.File.setLabel("File"); this.newFile.setLabel("New"); this.newFile.addActionListener(this); this.newFile.setShortcut(new MenuShortcut(KeyEvent.VK_N, false)); this.File.add(this.newFile); this.openFile.setLabel("Open "); this.openFile.addActionListener(this); this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); this.File.add(this.openFile); this.saveFile.setLabel("Save "); this.saveFile.addActionListener(this); this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false)); this.File.add(this.saveFile); this.close.setLabel("Close"); this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false)); this.close.addActionListener(this); this.File.add(this.close); } public void actionPerformed (ActionEvent e){ if(e.getSource() == this.close) this.dispose(); else if(e.getSource() == this.openFile){ JFileChooser open = new JFileChooser(); int option = open.showOpenDialog(this); if(option == JFileChooser.APPROVE_OPTION){ this.textArea.setText(""); try{ Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath())); while(scan.hasNext()) this.textArea.append(scan.nextLine()+"\n"); } catch(Exception ex){ System.out.println(ex.getMessage()); } } } else if(e.getSource() == this.saveFile){ JFileChooser save = new JFileChooser(); int option = save.showSaveDialog(this); if(option == JFileChooser.APPROVE_OPTION){ try{ BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath())); out.write(this.textArea.getText()); out.close(); } catch(Exception ex){ System.out.println(ex.getMessage()); } } } else if(e.getSource() == this.newFile){ } } public static void main(String args[]){ Notepad app = new Notepad(); app.setVisible(true); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload