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);
  }
}

Ads









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
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
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> <
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
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 '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 '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 '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 '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   

Ads