Home Answers Viewqa Java-Beginners GUI Applications

 
 


MANOJKUMAR
GUI Applications
2 Answer(s)      a year ago
Posted in : Java Beginners

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 Pages:
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
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
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
Rationale for GUI tutorial decisions
Table of Contents Rationale for GUI tutorial decisions Java offers many... learn GUI programming? All "normal" programs are GUI based, so this question... that some Java textbooks are written without any GUI coverage, and others cover
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
Serializing GUI Components Across Network - tutorial
Serializing GUI Components Across Network 2001-03-14 The Java Specialists' Newsletter [Issue 013a] - Serializing GUI Components Across Network Author... to as many people as you know who might be interested in Java. Serializing GUI
SmartClient AJAX GUI System
SmartClient AJAX GUI System       SmartClient is the cross-platform AJAX GUI system chosen by top... application stack, from rich, skinnable, extensible GUI components to declarative
Applications - text example
are called applications. A typical MS Windows application has an extension.... Applications, as opposed to applets for example, must have is a main method. Here is a simple application that writes one line. It has no graphical user interface (GUI
GUI framework
GUI framework  what do u mean by GUI framework
EasyEclipse Desktop Java
GUI applications with Swing or SWT. Composition: This distribution includes..., compile, run, debug, test, refactor, document and deploy Java applications
GUI component
GUI component  How can a GUI component handle its own events
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 code  GUI code
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
Java Applications
Java Applications  Hi, Is there Java applications for shopping cart? Thanks
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
Ajax applications
Ajax applications  Hi, Where can I find Ajax applications? Thanks
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
Convert the code to GUI
GUI Application example  GUI Application example
Convert the code to GUI
GUI Example  GUI Example code to learn
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
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
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
GUI Structural Patterns
is perhaps the best choice for small GUI applications. The Presentation contains... the Model In all ways of structuring a GUI program, there is one vitally important...) with the following interfaces. GUI interface. And it should be easy to change
Eclipse Plugin-Rich Client Applications
you can rapidly build business applications on the web with advanced GUI... Eclipse Plugin-Rich Client Applications...-in enables Rich Internet Applications (RIA) Platform for Eclipse. It allows
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
Netbeans GUI Ribbon
Netbeans GUI Ribbon  how to create ribbon task in java GUI using netbeans
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI code
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
iRows
iRows       iRows is a web-based spreadsheet currently in beta with a GUI similar to the traditional desktop-based spreadsheet applications, such as Microsoft Excel and OpenOffice. Read
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
Summary - Basic GUI Components
Java: Summary - Basic GUI Components Swing API provides lots components for crating the GUI for Java Swing applications. In this section we are studying the Basic GUI components of Swing framework. We will study the following GUI
Applications of Business intelligence
Applications of Business intelligence  What are the applications of Business intelligence in an enterprise
Magic Matrix in GUI
Magic Matrix in GUI  I want program in java GUI contain magic matrix for numbers
how to refresh my GUI page
how to refresh my GUI page  how to refresh a GUI in java