GUI Applications

GUI Applications

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

View Answers

June 14, 2012 at 5:22 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:30 PM

If you want the simple one, then here is another example of JMenuItem.

import javax.swing.*;

public class SwingMenu{
  public static void main(String[] args) {
  SwingMenu s = new SwingMenu();
  }


  public SwingMenu(){
  JFrame frame = new JFrame();
  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);


  frame.setJMenuBar(menubar);
  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
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
GUI framework
GUI framework  what do u mean by GUI framework
GUI component
GUI component  How can a GUI component handle its own events
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
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
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
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
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
Write a GUI Application  best way to write a GUI based application
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
gui question
gui question  design a gui application and write code to accept a string from the user in a textfeild and print using option pane whether it is a palindrome or not. hint: abba is a palindrome   import java.awt.*; import
GUI problem
GUI problem  How do I make a Jbutton which is shaped like a circle. This button needs to be clicked in order to change color.   import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class
Applications and Applets
Applications and Applets       Now a days, Java is widely used for applications and applets. The code... and is executed by a run-time interpreter. Applications are stand alone and are executed
Applications of Business intelligence
Applications of Business intelligence  What are the applications of Business intelligence in an enterprise
Netbeans GUI Ribbon
Netbeans GUI Ribbon  how to create ribbon task in java GUI using netbeans
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI code
ModuleNotFoundError: No module named 'gui'
ModuleNotFoundError: No module named 'gui'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'gui' How to remove the ModuleNotFoundError: No module named 'gui' error
Java GUI - Java Beginners
Java GUI  HOW TO ADD ICONS IN JAVA GUI PROGRAMMES
Convert the code to GUI
Convert the code   How to convert a code to GUI look alike
Convert the code to GUI
Is it possible to convert a code into GUI  Is it possible to convert a code into GUI
how to refresh my GUI page
how to refresh my GUI page  how to refresh a GUI in java
Magic Matrix in GUI
Magic Matrix in GUI  I want program in java GUI contain magic matrix for numbers
Applications and Applets
Applications and Applets       Now a days, Java is widely used for applications and applets. The code... and is executed by a run-time interpreter. Applications are stand alone and are executed
Artifacts of gj-gui
List of Artifacts of gj-gui maven depenency
Maven Repository/Dependency: gj-gui | gj-gui
Maven Repository/Dependency of Group ID gj-gui and Artifact ID gj-gui. Latest version of gj-gui:gj-gui dependencies. # Version Release Date You can read more at: Maven Tutorials
Flex SDK GUI
Flex SDK GUI  Hi....... please give me ans of this question.. What classes do you typically extend in the Flex SDK for creating GUI controls and why? Thanks
GUI application program
GUI application program  Write a GUI application program that meets the following requirements: Create an array with 100 randomly chosen integers. Create a textfield to enter an array index and another textfield to display
Java GUI - Java3D
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
Maven Dependency gj-gui >> 0.1
You should include the dependency code given in this page to add Maven Dependency of gj-gui >> gj-gui version0.1 in your project
data science applications in engineering
data science applications in engineering  Hi, I am beginner in Data...: data science applications in engineering Try to provide me good examples or tutorials links so that I can learn the topic "data science applications
applications of data science ppt
applications of data science ppt  Hi, I am beginner in Data Science...: applications of data science ppt Try to provide me good examples or tutorials links so that I can learn the topic "applications of data science ppt"

Ads