save and open jmenu item

save and open jmenu item

How to create a save and open jmenu item in java desktop application.

View Answers

June 14, 2012 at 5:28 PM

Here is a simple JMenuItem example in java swing through which you can perform open and save operations on File.

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class SwingMenu{
    final JTextArea txtArea=new JTextArea(0,0); 
    JFrame frame = new JFrame();
    String Filename;
    String filename;
  public static void main(String[] args) {
  SwingMenu s = new SwingMenu();
  }
   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");
             txtArea.setText(sb.toString());
             d.close();
         }
         catch(FileNotFoundException fe)
             {
             System.out.println("File not Found");
         }
         catch(IOException ioe){}
     }

  public SwingMenu(){


   JScrollPane scroll=new JScrollPane(txtArea);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JMenuBar menubar = new JMenuBar();
  JMenu filemenu = new JMenu("File");
  JMenuItem fileItem1 = new JMenuItem("Open");
  JMenuItem fileItem2 = new JMenuItem("Save");
  filemenu.add(fileItem1);
  filemenu.add(fileItem2);
  menubar.add(filemenu);

      fileItem1.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e)
             {
             FileDialog fd = new FileDialog(frame, "select File",FileDialog.LOAD);
             fd.show();
             if (fd.getFile()!=null)
                 {
                 filename = fd.getDirectory() + fd.getFile();
                 frame.setTitle(filename);
                 ReadFile();
             }
             txtArea.requestFocus();
         }
      });


      fileItem2.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e)
             {
             FileDialog fd = new FileDialog(frame,"Save File",FileDialog.SAVE);
             fd.show();
             if (fd.getFile()!=null)
                 {
                 filename = fd.getDirectory() + fd.getFile();
                 frame.setTitle(filename);
                 try
                     {
                     DataOutputStream d = new DataOutputStream(new FileOutputStream(filename));
                     String line = txtArea.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");
                 }
                 txtArea.requestFocus();
             }
         }
          });

  frame.setJMenuBar(menubar);
  frame.add(txtArea);
  frame.setSize(400,400);
  frame.setVisible(true);
  }
}









Related Tutorials/Questions & Answers:
Linking JMenu Item with a JPane in Netbeans
Linking JMenu Item with a JPane in Netbeans  How do you link a Jpane window to a JMenu Item in Java Netbeans
save excel file with save and open dilog box
save excel file with save and open dilog box  following is my jsp code it is working correct.. i want to save excel file throgh jsp bt not by hard...");) user should have freedom to save the file in any location so plz give me
Advertisements
How to save JCombobox Selected Item in to Access Database - Java Beginners
How to save JCombobox Selected Item in to Access Database  How to save JCombobox Selected Item in to Access Database  Hi Friend, Try the following code: import java.sql.*; import javax.swing.*; import
how to capture IE browser FileDownload box open ,save and cancel event - JSP-Servlet
how to capture IE browser FileDownload box open ,save and cancel event  I have a JSP page which allows user to download a file.When user click... to capture open,save and cancel event of filedownload box in javascript. Please
item lookup
item lookup  I need to create a lookup box in a grid that searches and filters at the same time. Upon selection the item with its price should be displayed. The control would then position below the first selection, so
How to change object item to integer item
How to change object item to integer item  How to change objectitem to integer item?thankyou
return item in jquery
return item in jquery  How to return items/variables in JQuery?   See the code below to find out how to return an Item in Jquery: function findItem() { var item; while(if_item_not_found) { // search
delete an item from database
delete an item from database  how to delete an item from the database using jsp
ModuleNotFoundError: No module named 'item'
ModuleNotFoundError: No module named 'item'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'item' How to remove the ModuleNotFoundError: No module named 'item' error
Item renderer in flex
Item renderer in flex  Hi...... please tell me about How to create item renderers in flex? give an example for that Thanks
navigation item back button
navigation item back button  I wants to add a navigationBar on the top of my UIWebView also a navigation item "back button". can anyone please explain how the back and refresh button works
Item renderer in flex
Item renderer in flex  Hi..... How does item renderer work? How do I add item renderer at runtime? Please me the right answer so i cam implement this in my apps. Thanks  Ans: The base class for Spark item
Item renderer in flex
Item renderer in flex  Hi... Please tell me about what does listData contain in ItemRenderers? What is the Type of listData? please give an example for that........ Thanks
Item renderer in flex
Item renderer in flex  Hi..... I have a problem How do you call a method in particular ItemRenderer. Also the ItemRenderer is your own Custom Component? please give me an example for that...... Thanks
highlight menu item html
highlight menu item html  highlight menu item in html or CSS   <body id="index"> <div id="menu"> <ul> <li class="index" ><a href="index.html">Index page</a></li> <
Adding checkbox to List as item
Adding checkbox to List as item   can we add checkox to List   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.awt....()); CheckListItem item = (CheckListItem) list.getModel().getElementAt
ModuleNotFoundError: No module named 'item-tracking'
ModuleNotFoundError: No module named 'item-tracking'  Hi, My... 'item-tracking' How to remove the ModuleNotFoundError: No module named 'item-tracking' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Output_item'
ModuleNotFoundError: No module named 'Output_item'  Hi, My Python... 'Output_item' How to remove the ModuleNotFoundError: No module named 'Output_item' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'print-item'
ModuleNotFoundError: No module named 'print-item'  Hi, My Python... 'print-item' How to remove the ModuleNotFoundError: No module named 'print-item' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'item-tracking'
ModuleNotFoundError: No module named 'item-tracking'  Hi, My... 'item-tracking' How to remove the ModuleNotFoundError: No module named 'item-tracking' error? Thanks   Hi, In your python
Add a new item to a dictionary in Python
Add a new item to a dictionary in Python  Hi, I have an dictionary in Python and I want to add new item to it. What is the best way to add an item to a python dictionary? Share me example for adding a new item to a dictionary
org.webjars.bowergithub.polymerelements - paper-item version 2.1.0 Maven dependency. How to use paper-item version 2.1.0 in pom.xml?
org.webjars.bowergithub.polymerelements  - Version 2.1.0 of paper-item...; - Version 2.1.0 of paper-item in pom.xml? How to use paper-item version 2.1.0... to use org.webjars.bowergithub.polymerelements  - Version 2.1.0 of paper-item
awt list item* - Swing AWT
awt list item*  how do i make an item inside my listitem not visible  Hi friend, import java.awt.*; import java.awt.event.*; public class ChoiceListDemo{ public static void main(String[] args) { Frame
How to save data - Swing AWT
How to save data  Hi, I have a problem about how to save data ,but first before data save to dababase, data must view in jLisit or Jtable... save data from jList and Jtable(in jList or jTable data will be many).Thank's
ModuleNotFoundError: No module named 'django-featured-item'
ModuleNotFoundError: No module named 'django-featured-item'  Hi...: No module named 'django-featured-item' How to remove the ModuleNotFoundError: No module named 'django-featured-item' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-featured-item'
ModuleNotFoundError: No module named 'django-featured-item'  Hi...: No module named 'django-featured-item' How to remove the ModuleNotFoundError: No module named 'django-featured-item' error? Thanks   Hi
ModuleNotFoundError: No module named 'gs2-python-sdk-consumable-item'
ModuleNotFoundError: No module named 'gs2-python-sdk-consumable-item' ...: No module named 'gs2-python-sdk-consumable-item' How to remove the ModuleNotFoundError: No module named 'gs2-python-sdk-consumable-item' error
ModuleNotFoundError: No module named 'item-based-recommender'
ModuleNotFoundError: No module named 'item-based-recommender'  Hi...: No module named 'item-based-recommender' How to remove the ModuleNotFoundError: No module named 'item-based-recommender' error? Thanks   Hi
ModuleNotFoundError: No module named 'odoo8-addon-pricelist-item-generator'
ModuleNotFoundError: No module named 'odoo8-addon-pricelist-item-generator...: ModuleNotFoundError: No module named 'odoo8-addon-pricelist-item-generator' How to remove the ModuleNotFoundError: No module named 'odoo8-addon-pricelist-item
ModuleNotFoundError: No module named 'scrapy-Custom-item'
ModuleNotFoundError: No module named 'scrapy-Custom-item'  Hi, My... named 'scrapy-Custom-item' How to remove the ModuleNotFoundError: No module named 'scrapy-Custom-item' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'scrapy-Custom-item'
ModuleNotFoundError: No module named 'scrapy-Custom-item'  Hi, My... named 'scrapy-Custom-item' How to remove the ModuleNotFoundError: No module named 'scrapy-Custom-item' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'scrapy-mongoengine-item'
ModuleNotFoundError: No module named 'scrapy-mongoengine-item'  Hi...: No module named 'scrapy-mongoengine-item' How to remove the ModuleNotFoundError: No module named 'scrapy-mongoengine-item' error? Thanks   
ModuleNotFoundError: No module named 'upc_item_db_api'
ModuleNotFoundError: No module named 'upc_item_db_api'  Hi, My... named 'upc_item_db_api' How to remove the ModuleNotFoundError: No module named 'upc_item_db_api' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-admin-item-owner'
ModuleNotFoundError: No module named 'django-admin-item-owner'  Hi...: No module named 'django-admin-item-owner' How to remove the ModuleNotFoundError: No module named 'django-admin-item-owner' error? Thanks   
ModuleNotFoundError: No module named 'django-featured-item'
ModuleNotFoundError: No module named 'django-featured-item'  Hi...: No module named 'django-featured-item' How to remove the ModuleNotFoundError: No module named 'django-featured-item' error? Thanks   Hi
ModuleNotFoundError: No module named 'edc-action-item'
ModuleNotFoundError: No module named 'edc-action-item'  Hi, My... named 'edc-action-item' How to remove the ModuleNotFoundError: No module named 'edc-action-item' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'fake_item_generator'
ModuleNotFoundError: No module named 'fake_item_generator'  Hi, My... named 'fake_item_generator' How to remove the ModuleNotFoundError: No module named 'fake_item_generator' error? Thanks   Hi
ModuleNotFoundError: No module named 'girder-item-licenses'
ModuleNotFoundError: No module named 'girder-item-licenses'  Hi...: No module named 'girder-item-licenses' How to remove the ModuleNotFoundError: No module named 'girder-item-licenses' error? Thanks   Hi
ModuleNotFoundError: No module named 'girder-item-tasks'
ModuleNotFoundError: No module named 'girder-item-tasks'  Hi, My... named 'girder-item-tasks' How to remove the ModuleNotFoundError: No module named 'girder-item-tasks' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'gs2-python-sdk-consumable-item'
ModuleNotFoundError: No module named 'gs2-python-sdk-consumable-item' ...: No module named 'gs2-python-sdk-consumable-item' How to remove the ModuleNotFoundError: No module named 'gs2-python-sdk-consumable-item' error
ModuleNotFoundError: No module named 'item-based-recommender'
ModuleNotFoundError: No module named 'item-based-recommender'  Hi...: No module named 'item-based-recommender' How to remove the ModuleNotFoundError: No module named 'item-based-recommender' error? Thanks   Hi
Item Events in Java
Item Events in Java       Introduction In this section, you will learn about handling item events in java. This demonstrates that the event generated when you select an item
How to store JComboBox item into database
How to store JComboBox item into database   import...(javax.swing.SwingConstants.CENTER); jLabel2.setText("Item Code"); getContentPane().add(jLabel2,new...); jLabel3.setText("Item Name"); getContentPane().add(jLabel3,new AbsoluteConstraints(27
Version of com.leonarduk>freecycle-item-finder dependency
List of Version of com.leonarduk>freecycle-item-finder dependency
image save to folder in java
image save to folder in java  Hi, I am working with java. In my application i want to give facility to user to add and change image. I use open dialog box to select image, it will work properly i.e on button click open dialog
retaining save dialog box even after clicking save button - Java Beginners
when i open save dialog box and input a file name that is already present... get it back??(even if i cal save dialog box again it would open again from...retaining save dialog box even after clicking save button  I am
Hibernate save()
This tutorial explain how save() works in hibernate
upload and save the file
upload and save the file  upload and save a file from a computer to specific location of remote computer using jsp
ModuleNotFoundError: No module named 'save'
ModuleNotFoundError: No module named 'save'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'save' How to remove the ModuleNotFoundError: No module named 'save' error
Save Table - SQL
Save Table  Where Oracle Tables or Database is saved? How to see Oracle Tables ? What are the websites to learn step by step Oracle. Help Me

Ads