LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   outputting from menu in swing applet, saving to file... (https://www.linuxquestions.org/questions/programming-9/outputting-from-menu-in-swing-applet-saving-to-file-454596/)

ohfaney 06-14-2006 02:50 AM

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.

ppanyam 06-14-2006 06:13 AM

Use ActionListener with a menu click. Inside the ActionListener, use FileWriter

ppanyam

ohfaney 06-14-2006 08:57 PM

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?

Mcribs 06-14-2006 09:12 PM

Surround the filewriter operation in a try-catch to handle the exception.

ohfaney 06-14-2006 09:35 PM

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.

Mcribs 06-14-2006 09:41 PM

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

ohfaney 06-14-2006 09:48 PM

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

Mcribs 06-14-2006 09:53 PM

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.

ohfaney 06-14-2006 09:55 PM

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.

Mcribs 06-14-2006 10:07 PM

BoooYah! Keep the questions coming...its good review.

ohfaney 06-14-2006 10:07 PM

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""

Mcribs 06-14-2006 10:14 PM

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
      }


ohfaney 06-14-2006 10:29 PM

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;

Mcribs 06-14-2006 10:35 PM

You don't need to import FileWriter fw. Just write that statement where I showed you.

ohfaney 06-14-2006 10:39 PM

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



All times are GMT -5. The time now is 02:22 PM.