Regarding GUI Applications

Regarding GUI Applications

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

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:
Regarding GUI Applications
Regarding GUI Applications  How to create a save and open jmenu item in java desktop application
Regarding GUI Applications
GUI Applications  How to create a save and open jmenu item in java desktop application.   Here is a simple JMenuItem example in java swing through which you can perform open and save operations on File. import
Advertisements
Regarding GUI Applications
Regarding GUI Applications
Regarding GUI Applications
GUI
GUI  How to GUI in Net-beans ... ??   Please visit the following link: http://www.roseindia.net/java/java-tips/background/30java_tools/netbeans.shtml
GUI
GUI  Write a GUI application for the WebBuy Company that allows a user to compose the three parts of a complete email message: the â??To:â??, â??Subject:â?? and â??Message:â?? text. The â??To:â??, and â??Subject:â?? Text areas
regarding rev
regarding rev  write a prog. in java that display all the arguments passed at the command line in reverse order
Regarding repository
Regarding repository  How to check in and check out using SVN repository using eclipse
GUI-based Applications
GUI-based Applications       In this section you will learn about how to create a window for your... as shown below. One point to remember here is that Applications respond
regarding java - JDBC
regarding java  i am making a project on supply chain system i have... can to able to insert or update data entry in these tables through GUI which will be directly saved in the database i have seen many examples regarding
Regarding Hibernate
Regarding Hibernate  Both JDBC and Hibernate are used to connect to database then whats the need of going to hibernate? What are the main differences? And could you let me know please
Regarding Project
Regarding Project  sir, i am doing a project in java. my project is CITY GUIDE it is an web application. please give me documentation of my project
Regarding Project
Regarding Project  sir, i am doing a project in java. my project is CITY GUIDE it is an web application. please give me documentation of my project
GUI framework
GUI framework  what do u mean by GUI framework
Regarding javascript
Regarding javascript  Hi, I want 4 upload buttons in HTML CODE and Among 4 browse buttons user is not selected any one then provide a prompt message like plz select any 1 file among 4 buttons.If user is selected either 1st
Regarding Datasource
Regarding Datasource  Hi friend, Good morning,how r u,i want to share 1 information to u,I got a job in VERIZON DATA SERVICES INDIA PVT.LTD,u help a lot when i am in academic project,with that project only i got a job
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container below that row itself and show the required details of the row
GUI component
GUI component  How can a GUI component handle its own events
regarding project
regarding project  OBJECTIVES OF THIS PROJECT: -Ability to test the programmer for basic interaction skills to understand the client requirement. -Ability of the programmer to ask the right questions and get the relevant
regarding project
regarding project  OBJECTIVES OF THIS PROJECT: -Ability to test the programmer for basic interaction skills to understand the client requirement. -Ability of the programmer to ask the right questions and get
regarding project
regarding project   sir we need help about project. we have idea about his project please guide me sir. OBJECTIVES OF THIS PROJECT: -Ability to test the programmer for basic interaction skills to understand the client
Convert the code to GUI
GUI code  GUI code
Java Applications
Java Applications  Hi, Is there Java applications for shopping cart? Thanks
Applications of Java
Applications of Java  Hi, What are the Applications of Java in development of enterprise applications? Thanks   Hi, You can use java to develop various kinds of Java applications for: a) Desktop b) Websites c
Query regarding JSP, JDBC
Query regarding JSP, JDBC  can I get the code for the question asked by me regarding JSP,JDBC
Running Java Applications as software
Running Java Applications as software   Please tell me what to do if you want your java application to run like a real software,eg a java GUI application that connects to the database(Mysql)running on the computer like any other
Ajax applications
Ajax applications  Hi, Where can I find Ajax applications? Thanks
gui question
gui question  design a gui application for me and write its code in which the user enters a no. in a textfield and onn clicking the button the sum of the digits of the no. should be displayed. hint: suppose the user enters 12
GUI problem
GUI problem  Create a class called CDProgram and write a GUI program to compute the amount of a certificate of deposit on maturity. The sample data follows: Amount deposited: 80000.00 Years: 15 Interest Rate: 7.75 Hint
regarding jdbc - JDBC
regarding jdbc  how i can configure java with ms access
java gui
java gui   friends... good day.. i have doubt in java gui. ? i created 1 java gui application. That has two text fields jtext1,jtext2. case: user entered value in first textfield(jtext1) and pressed the enter key . the cursor
Convert the code to GUI
GUI Application example  GUI Application example
Convert the code to GUI
GUI Example  GUI Example code to learn
Version of gj-gui>gj-gui dependency
List of Version of gj-gui>gj-gui dependency
Regarding project - Applet
Regarding project  hi friend , iam doing project in Visual cryptography in Java so i need the Help regarding how to make a share of a original imahe into shares anu
Regarding weblogic portal
Regarding weblogic portal  Hi, My name is Ramanuja.i am working on weblogic portal.if u know any info regarding plz provide the materials or information to me.plz help me. Thanks in advance
Java GUI
Java GUI  1) Using Java GUI, create a rectangular box that changes color each time a user click a change color button. 2) Modify Question 1 to include a new button named insert image, that allow user to insert a bitmap image
regarding JTable - JDBC
regarding JTable  how to populate a JTable with mysql data after clicking JButton please explain with the example
regarding tags - Struts
regarding tags  What is the difference between html:submit and nested:submit and usage of tags based on the situation
regarding struts 2
regarding struts 2   is it not possible to get values from applicationresources.properties into our application in struts 2
Regarding core java
Regarding core java  how do we find reverse of string with out using String functions
regarding oops - Java Interview Questions
regarding oops  what is the differences between web server... webserver. 3)web server serves web based applications.(i.e servlets and jsps) whereas application server serves web based applications and enterprise based
regarding j2me - Java Beginners
regarding j2me  sir but i have to use the drawString u tell me how can i display two too long strings
regarding mini project - JDBC
regarding mini project  i need to make a mini project using servlet and jdbc.pls help me with a real time application
Convert the code to GUI
Java GUI Class Example  Java GUI Class Example
Convert the code to GUI
GUI Java JSP application  GUI Java JSP application
Convert the code to GUI
GUI Application Development   GUI Application Development
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
Write a GUI Application  best way to write a GUI based application

Ads