jmenu item in java desktop application

jmenu item in java desktop application

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
Java Spring Desktop application
Java Spring Desktop application  Hi i am developing a desktop java spring application..It has many Jframes forms and classes...How to close one Jframe from another..Please tell... First jframe package desktopapplication2
Advertisements
Simple Java Desktop Upload application
Simple Java Desktop Upload application  I try do simple example for upload applicationtake file from c:\ put to d:\ :) PLEASE HELP
desktop application
desktop application  how to transfer data swing text field to browser text field to their corresponding label/caption. please reply as soon as possible. thanks vijay
Java Spring Desktop application with multiple classes and jframe
Java Spring Desktop application with multiple classes and jframe  Hi i am developing a desktop java spring application..It has many Jframes forms and classes...How to close one Jframe from another..Please tell... First jframe
steps to create desktop application in java swing using eclipse with drag and drop options
steps to create desktop application in java swing using eclipse with drag and drop options  please help to create desktop application in java swing using eclipse with drag and drop and what are the plugins required
ssl comunication between a desktop application and a web application
ssl comunication between a desktop application and a web application  Hi Lusiano, I am trying to implement a ssl comunication between a desktop application and a web application. can you please share code with me. Please
ModuleNotFoundError: No module named 'Messenger-Desktop-Client-Application'
ModuleNotFoundError: No module named 'Messenger-Desktop-Client-Application...: ModuleNotFoundError: No module named 'Messenger-Desktop-Client-Application' How...-Application python with following command: pip install Messenger-Desktop-Client
ModuleNotFoundError: No module named 'Messenger-Desktop-Server-Application'
ModuleNotFoundError: No module named 'Messenger-Desktop-Server-Application...: ModuleNotFoundError: No module named 'Messenger-Desktop-Server-Application' How...-Application python with following command: pip install Messenger-Desktop-Server
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
java--desktop appilication - JDBC
java--desktop appilication  when class.forName() is called ,,,a class is loaded...once after the class is loaded ..if we call class.forName, it will call the class which is already present.......what to do if we want to load
java desktop - Java Beginners
java desktop  Hi, Iam created a desktop login application using swings. pls observe the following code: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; class LoginDemo{ JButton
java desktop - Java Beginners
java desktop  Hi, Iam created a desktop login application using swings. pls observe the following code: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; class LoginDemo{ JButton
Problem in setting IP address for a JFrame Class Desktop Application
Problem in setting IP address for a JFrame Class Desktop Application  I have designed a browser for our final year project, it is working well. But when we put it in a system which has a proxy setting for internet connection
DESKTOP SHARING USING JAVA
DESKTOP SHARING USING JAVA  Sir i have the following code for scren capturing it is capturing in png format but when i am sending it over other m/c it is taking delay of around 16 seconds..i think it is because this line of code
How to launch my web application from my desktop without opening Netbeans IDE everytime i run the application?
How to launch my web application from my desktop without opening Netbeans IDE... dont want all these steps, so i knw that java application can be launched using Java Web Start but dont knw abt web application, Is there any way to do
i want to create an icon on my desktop... with this i want to open my web application
i want to create an icon on my desktop... with this i want to open my web application  i want to create an icon on my desktop... with this i want to open my web application(automatically run my server... )how
Version of co.openfin>openfin-desktop-java-adapter dependency
List of Version of co.openfin>openfin-desktop-java-adapter dependency
EasyEclipse Desktop Java
EasyEclipse Desktop Java       EasyEclipse Desktop Java For development of Desktop... started. Java JDK for Windows - Run Java application on Sun Java(tm
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
Java program to get the desktop Path
Java program to get the desktop Path  ... the desktop path of the system. In the java environment we can get the desktop path also with the system's property. For getting the desktop path we have to add
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
path setting for tomcat to java for Desktop PC - JDBC
path setting for tomcat to java for Desktop PC  Hi sir, I want to connect my java programms to Database which of either sql,oracle,mysql... or configuration about the Tomcat Server in Java. Kindly visit them . We hope
Create a Desktop Pane Container in Java
Create a Desktop Pane Container in Java   ... pane container in Java. The desktop pane container is a container, which has... under the desktop pane. Following figure shows the JDesktopPane component of Java
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
Maven Dependency openfin-desktop-java-adapter >> 3.0.1.2
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version3.0.1.2 in your project
Maven Dependency openfin-desktop-java-adapter >> 3.0.1.3
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version3.0.1.3 in your project
Maven Dependency openfin-desktop-java-adapter >> 4.40.2.5
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version4.40.2.5 in your project
Maven Dependency openfin-desktop-java-adapter >> 4.40.2.6
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version4.40.2.6 in your project
Maven Dependency openfin-desktop-java-adapter >> 4.40.2.8
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version4.40.2.8 in your project
Maven Dependency openfin-desktop-java-adapter >> 4.40.2.9
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version4.40.2.9 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.2.0
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.2.0 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.2.2
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.2.2 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.2.3
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.2.3 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.2.4
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.2.4 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.3.1
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.3.1 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.3.2
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.3.2 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.3.4
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.3.4 in your project
Maven Dependency openfin-desktop-java-adapter >> 5.44.3.6
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version5.44.3.6 in your project
Maven Dependency openfin-desktop-java-adapter >> 6.0.0.1
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version6.0.0.1 in your project
Maven Dependency openfin-desktop-java-adapter >> 6.0.0.2
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version6.0.0.2 in your project
Maven Dependency openfin-desktop-java-adapter >> 6.0.1.0
You should include the dependency code given in this page to add Maven Dependency of co.openfin >> openfin-desktop-java-adapter version6.0.1.0 in your project
Java Application
Java Application  Hi, What is the applications of Java in development of software? What type of applications can be developed using Java programming Language? Can anyone tell me the url for downloading sample projects in Java
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
Maven dependency for com.axiastudio - zoefx-desktop version 0.2.0 is released. Learn to use zoefx-desktop version 0.2.0 in Maven based Java projects
- zoefx-desktop version 0.2.0 in Java projects. Follow the step by step tutorial...-desktop version 0.2.0 java library in your project. ADS_TO_REPLACE_2 Now you can...-desktop released The developers of   com.axiastudio - zoefx-desktop
Maven dependency for com.rhcloud - timemanager-desktop version 1.3 is released. Learn to use timemanager-desktop version 1.3 in Maven based Java projects
- timemanager-desktop version 1.3 in Java projects. Follow the step by step... - timemanager-desktop version 1.3 java library in your project. ADS_TO_REPLACE_2...-desktop released The developers of   com.rhcloud - timemanager-desktop
Maven dependency for com.rhcloud - timemanager-desktop version 1.4 is released. Learn to use timemanager-desktop version 1.4 in Maven based Java projects
- timemanager-desktop version 1.4 in Java projects. Follow the step by step... - timemanager-desktop version 1.4 java library in your project. ADS_TO_REPLACE_2...-desktop released The developers of   com.rhcloud - timemanager-desktop

Ads