open jmenu item in java

open jmenu item in java

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

View Answers

June 14, 2012 at 5:27 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
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
Advertisements
remove item from list box using java script - Java Beginners
remove item from list box using java script  remove item from list box using java script  Hi friend, Code to remove list box item using java script : Add or Remove Options in Javascript function addItem
How to store JComboBox selected Item into Ms Access Database - Java Beginners
How to store JComboBox selected Item into Ms Access Database  How to store JComboBox selected Item into Ms Access Database.  Hi Friend...(); } public SwingFrame(){ JFrame f = new JFrame("Frame in Java Swing
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... SwingFrame(){ JFrame f = new JFrame("Frame in Java Swing"); f.getContentPane
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
How can i select a particular item in jpopup menu through java programs
How can i select a particular item in jpopup menu through java programs ... a perticular menu item in jpopup menu to be selected. I created a gui where it has... menu item in it. assume that the user pressing down key 3 times. I need to show
how to open java
how to open Java  how to open java file simple Java Example
delete an item from database
delete an item from database  how to delete an item from the database using jsp
open source - Java Beginners
open source  hi! what is open source? i heard that SUN has released open source .what is re requisite to understand that open source. i know core concepts only  Hi friend, Open source is an approach to design
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
java swings - Java Beginners
click on the menu than another frame open which contain the some data like as item... JMenuItem("Item 1"); JMenuItem item2 = new JMenuItem("Item 2"); JMenu menu2 = new JMenu("Menu 2"); JMenuItem item3 = new JMenuItem("Item 3"); JMenuItem
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
Java JComboBox Get Selected Item Value
Java JComboBox Get Selected Item Value In this section we will discuss about how to get the selected item value form JComboBox. javax.swing.JComboBox.... In this tutorial we are discussing about how to store the selected item value
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
Java open source software
Open source software for Java In this page we will tell list down the most used Open source in Java. Java is one of the programming language used... is the list of java open source software used for the development and deployment
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> <
how to open notepad using java
how to open notepad using java  Hi, how to open notepad using java? I want to open windows notepad from java program. Thanks   Hi, Use following code: import java.util.*; import java.io.*; class Notepad { public
Open Source Testing Tools in Java
Open Source Testing Tools in Java In this page we are discussing about the different Open Source Testing Tools available in Java. JUnit JUnit... by the developer who implements unit tests in Java. JUnit is Open Source Software
Open Jgraph Problem - Java Beginners
Open Jgraph Problem  Hi, I have go through the example given in http://sourceforge.net/projects/openjgraph/ for Open Jgraph by Jesus M. Salvo Jr... on this. I am a newbie in Java language. Thank you. Ulamraja
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
awt list item* - Swing AWT
awt list item*  how do i make an item inside my listitem...); choice.add("Java "); choice.add("Jsp"); choice.add("Servlets... information. http://www.roseindia.net/java/example/java/awt/ Thanks
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
Problem to display checkbox item
) gettable.jsp: <%@page language="java" import ="java.sql.*" %> <% String
Open Source software written in Java
Open Source software written in Java Open Source Software  or OSS... Code Coverage Open Source Java Collections API... Purpose ERP/CRM Written in Java Open Source
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 - paper-item version 2.1.0 in Java projects. Follow the step...; org.webjars.bowergithub.polymerelements - paper-item version 2.1.0 java...org.webjars.bowergithub.polymerelements  - Version 2.1.0 of paper-item
java code to open and display MS-word document
java code to open and display MS-word document  java code to open and display MS-word document
How to open a file in java using JFileChooser
How to open a file in java using JFileChooser  How to open a file in java using JFileChooser
select one item name throug combo box than other combobox show item price
select one item name throug combo box than other combobox show item price  i have one table in database item master..if i select one item name through combo box than other combobox show item price only select item name... how i
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

Ads