Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners open jmenu item in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


MANOJKUMAR
open jmenu item in java
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners


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


Advertisement
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
how to open java
how to open Java  how to open java file simple Java Example
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 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
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
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
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
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
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
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
How to change object item to integer item
How to change object item to integer item  How to change objectitem to integer item?thankyou
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
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
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
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
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
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
delete an item from database
delete an item from database  how to delete an item from the database using jsp
how to open a file with a jbutton? - Java Beginners
how to open a file with a jbutton?  Hello Is it possible that by clicking on a button it can open java html file or doc exists on the pc? I searched and I found that it is possible to open a web page so I tried to open
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
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
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..... 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
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
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> <
open source web services tool java
Open Source web services tool in java        ...: The Web Services Invocation Framework (WSIF) is a simple Java API
Java Swing Open Browser
Java Swing Open Browser Java provides different methods to develop useful... that will open the specified url on the browser using java swing. This example will really...("Open Browser"); button.addActionListener(new ActionListener() { public
Open website on Button Click - Java Beginners
Open website on Button Click  Hello sir I want to open website on button click using java swing plz help me sir. in my swing application one "VISIT US BUTTON" i want to open my website on Button CLick  Hi Friend
Open InternetExplorer Webpage with Java - Java Beginners
Open InternetExplorer Webpage with Java  Hello Sir I want to Open my webpage from swing application when I Click On "VISIT US" Boutton then website...(); final JTextField url =new JTextField(20); JButton button = new JButton("Open
Open TextFile on JButton Click - Java Beginners
Open TextFile on JButton Click  Hello Sir I Want to open TextFile on JButton Click. plz Help Me  Hi Friend, Do you want to simply open the text file or you want to open the file using JFileChooser,read that file
Advertisements
 

 

 

DMCA.com