LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2006, 02:50 AM   #1
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Rep: Reputation: 15
Question outputting from menu in swing applet, saving to file...


I bet ya'll are getting sick of me by now, but well i've got some developments. So far I have this applet, my code is below

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.Image;

public class Bits extends JFrame
{
    public static void main (String args[])
        throws java.io.IOException
    {
        FileWriter fw = new FileWriter ("textpage.html");
        new Bits ();
        

    }


    public Bits ()
    {
        
        setSize (1000, 600);

        setLocation (100, 100);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        obj ();
        //pack();
        setVisible (true);
    }






  

    JMenuBar menuBar = new JMenuBar ();
    JMenu menu1 = new JMenu ("Choose A Menu Option");
    JMenuItem menuItem1 = new JMenuItem ("Getting Started A");
    JMenuItem menuItem2 = new JMenuItem ("Learn about html B");
    JMenuItem menuItem3 = new JMenuItem ("Basic Codes C");
    JMenuItem menuItem4 = new JMenuItem ("Tutorials D");
    JMenuItem menuItem5 = new JMenuItem ("Build your own page");

    JMenuItem menuItem6 = new JMenuItem ("Test your skills with color codes ");
    JMenuItem menuItem7 = new JMenuItem ("Make a photo page");
    JMenuItem menuItem8 = new JMenuItem ("Create a basic page (header, text, one link) ");

    JMenuItem menuItem9 = new JMenuItem ("Create a full working page with background image ");
    JMenuItem menuItem10 = new JMenuItem ("Quiz Yourself F ");
    JMenuItem menuItem11 = new JMenuItem ("Exit");

    JTextField nameTxtFld = new JTextField (10);
    JButton submitBtn = new JButton ("Submit");

    private void obj ()
    {

        menu1.add (menuItem1);
        menu1.add (menuItem2);
        menu1.add (menuItem3);
        menu1.add (menuItem4);
        menu1.add (menuItem5);
        menu1.add (menuItem6);
        menu1.add (menuItem7);
        menu1.add (menuItem8);
        menu1.add (menuItem9);
        menu1.add (menuItem10);
        menu1.add (menuItem11);

        menuItem11.addActionListener (new ActionListener ()
        {
            public void actionPerformed (ActionEvent evt)
            {
                System.exit (0);
            }
        }
        );

        menuBar.add (menu1);
        setJMenuBar (menuBar);

        Container c = getContentPane ();

        JPanel row1 = new JPanel ();
        row1.setLayout (new BorderLayout ());
        row1.add (new JLabel ("What's your name?: "), BorderLayout.CENTER);
        row1.add (nameTxtFld, BorderLayout.EAST);
        row1.add (submitBtn, BorderLayout.SOUTH);


        c.setLayout (new FlowLayout (FlowLayout.CENTER));
        c.add (row1);

        setBackground (Color.magenta);
        getContentPane ().setBackground (Color.magenta);

    





    }
}
My problems are...

1. How can i output my information from the menu?
2. How can i save to file?

Thanks ya'll.
 
Old 06-14-2006, 06:13 AM   #2
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
Use ActionListener with a menu click. Inside the ActionListener, use FileWriter

ppanyam
 
Old 06-14-2006, 08:57 PM   #3
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ppanyam
Use ActionListener with a menu click. Inside the ActionListener, use FileWriter

ppanyam
yeah i got the menu options working, but my problemo now is how do i do user input in them? and.. with the FileWriter, it was saying something about the throws java.io.IOException, where do i put that?
 
Old 06-14-2006, 09:12 PM   #4
Mcribs
LQ Newbie
 
Registered: Nov 2005
Location: Chicago
Distribution: Debian
Posts: 15

Rep: Reputation: 0
Surround the filewriter operation in a try-catch to handle the exception.
 
Old 06-14-2006, 09:35 PM   #5
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mcribs
Surround the filewriter operation in a try-catch to handle the exception.
okay thanks!

would you happen to know how i can do the user input, like i ask a question...the input and then it saves, because my menu options open in seperate message windows, if you know what i mean.
 
Old 06-14-2006, 09:41 PM   #6
Mcribs
LQ Newbie
 
Registered: Nov 2005
Location: Chicago
Distribution: Debian
Posts: 15

Rep: Reputation: 0
To do something like that you would need to write an actionListener method for you button and when it's pressed the program should store the value by
String value=textfield.getText();
 
Old 06-14-2006, 09:48 PM   #7
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mcribs
To do something like that you would need to write an actionListener method for you button and when it's pressed the program should store the value by
String value=textfield.getText();

with the try/catch, can you tell me if this is right? lol

Code:
try {
 
        FileWriter fw = new FileWriter ("textpage.html");

} catch(IOException e){

}
or am i missing something here? lol
 
Old 06-14-2006, 09:53 PM   #8
Mcribs
LQ Newbie
 
Registered: Nov 2005
Location: Chicago
Distribution: Debian
Posts: 15

Rep: Reputation: 0
That code is fine except for the FileWriter parameter. You should make it a directory like "C:/testpage.html" so that you know where your file is saving.

Last edited by Mcribs; 06-14-2006 at 09:54 PM.
 
Old 06-14-2006, 09:55 PM   #9
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mcribs
That code is fine except for the FileWriter parameter. You should make it a directory like "C:/testpage.html" so that you know where your file is saving.

woo i'm learning haha.
 
Old 06-14-2006, 10:07 PM   #10
Mcribs
LQ Newbie
 
Registered: Nov 2005
Location: Chicago
Distribution: Debian
Posts: 15

Rep: Reputation: 0
BoooYah! Keep the questions coming...its good review.
 
Old 06-14-2006, 10:07 PM   #11
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mcribs
That code is fine except for the FileWriter parameter. You should make it a directory like "C:/testpage.html" so that you know where your file is saving.
Code:
  JPanel row1 = new JPanel ();
        row1.setLayout (new BorderLayout ());
        row1.add (new JLabel ("What's your name?: "), BorderLayout.CENTER);
        row1.add (nameTxtFld, BorderLayout.EAST);
        row1.add (submitBtn, BorderLayout.SOUTH);
        fw.write (nameTxtFld + "C:/textpage.html");
i get an error

"no field name "fw" was found in "bits""
 
Old 06-14-2006, 10:14 PM   #12
Mcribs
LQ Newbie
 
Registered: Nov 2005
Location: Chicago
Distribution: Debian
Posts: 15

Rep: Reputation: 0
It has to do with variable scope. To solve this you need to declare a global variable (FileWriter fw) in the class but outside of the methods.

Something like this:
Code:
      //Import statements
      public class Bits{
         FileWriter fw;
         //The rest of your variables
         
         //main and your other methods
       }
 
Old 06-14-2006, 10:29 PM   #13
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mcribs
It has to do with variable scope. To solve this you need to declare a global variable (FileWriter fw) in the class but outside of the methods.

Something like this:
Code:
      //Import statements
      public class Bits{
         FileWriter fw;
         //The rest of your variables
         
         //main and your other methods
       }

where in my code would i import that statement,
Code:
FileWriter fw;
 
Old 06-14-2006, 10:35 PM   #14
Mcribs
LQ Newbie
 
Registered: Nov 2005
Location: Chicago
Distribution: Debian
Posts: 15

Rep: Reputation: 0
You don't need to import FileWriter fw. Just write that statement where I showed you.
 
Old 06-14-2006, 10:39 PM   #15
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mcribs
You don't need to import FileWriter fw. Just write that statement where I showed you.

yeah i did that and it wouldn't run, here...i'm assuming you meant here because i don't have just a 'public class bits' i have...

Code:
public class Bits extends JFrame
{

    public static void main (String args[])
        throws java.io.IOException
        
    {

        
        new Bits ();
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        //filewriter is surrounded by a try/catch to handle the exception when saving userinput to file
        
        try
        {

            FileWriter fw = new FileWriter ("C:/textpage.html");

        }
        catch (IOException e)
        {

        }

        FileWriter fw //in here? orrrrr



    }



    public Bits ()
        throws java.io.IOException
    {
         FileWriter fw //here?
        setSize (500, 400);
        setLocation (350, 250);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        obj ();
        //pack();
        setVisible (true);

    }


    //menu options
    JMenuBar menuBar = new JMenuBar ();
    JMenu menu1 = new JMenu ("Choose A Menu Option");
    JMenuItem menuItem1 = new JMenuItem ("Getting Started");
    JMenuItem menuItem2 = new JMenuItem ("Learn about html");
    JMenuItem menuItem3 = new JMenuItem ("Basic Codes");
    JMenuItem menuItem4 = new JMenuItem ("Tutorials");



    JMenuItem menuItem8 = new JMenuItem ("Create a basic page (header, text, one link) ");


    JMenuItem menuItem10 = new JMenuItem ("Quiz Yourself");
    JMenuItem menuItem9 = new JMenuItem ("Answers to the quiz");
    JMenuItem menuItem11 = new JMenuItem ("Exit");

    JTextField nameTxtFld = new JTextField (10);
    JButton submitBtn = new JButton ("Submit");
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
file menu applet zipjay Linux - Software 0 01-15-2005 09:12 AM
failure loading or saving config info for rhn-applet strimp099 Linux - General 0 05-24-2004 11:30 PM
outputting ftp contents to file kubicon Linux - Networking 2 09-15-2003 02:01 PM
Cannot Get Swing to work in a Java Applet? Checkers Linux - Software 0 03-17-2003 03:23 PM
Can't compile Java Swing Applet with javac chansky Programming 4 09-23-2002 04:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration