Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

problem_in_my_code

Thread view: 
matrix - 29 Oct 2006 01:19 GMT
my goal in my code is to save the form1 in a text file ;but when you
run this code on your computers the button of the "save as file"
doesn't respond on clicking and show me the saveDialog window. by the
way,I donot have compile error
I want to test my code and know where is my error and send me an
explanation

MY CODE:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Form1 extends JFrame implements ActionListener
{
    JFrame frame=new JFrame() ;
    JFileChooser filechooser;
    JTextField text1 ;
    JTextField text2 ;
    JTextField text3 ;
    JTextArea  area4 ;
    JButton button1;
    public Form1 (String name)
    {
        super(name);
        setSize(350,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField text1 = new JTextField (30);
        JTextField text2 = new JTextField (30);
        JTextField text3 = new JTextField (30);
        JTextArea  area4 = new JTextArea  (10,30);

        JButton button1 = new JButton("save to file");

        JLabel label1 = new JLabel ("Name");
        JLabel label2 = new JLabel ("Type");
        JLabel label3 = new JLabel ("Date");
        JLabel label4 = new JLabel ("Description");
        button1.addActionListener(this);

        JPanel pane =new JPanel();
        pane.add(label1);
        pane.add(text1);
        pane.add(label2);
        pane.add(text2);
        pane.add(label3);
        pane.add(text3);
        pane.add(label4);
        pane.add(area4);

        pane.add(button1);
        setContentPane(pane);
        area4.setLineWrap(true);
        area4.setWrapStyleWord(true);
        setVisible(true);

    }

    public static void main (String [] arguments)
    {
        Form1 f= new Form1( "form");

    }
    public File getAFileForSave(){
        File file = null;
        File currentdirectory = new File(".");
        JFileChooser filechooser = new JFileChooser(currentdirectory);

        int replycode = filechooser.showSaveDialog(null);
        if (replycode == JFileChooser.APPROVE_OPTION){
            file = filechooser.getSelectedFile();
        }
        return file;

    }
    public void writeStringToFile(File file, String s){

        try{
            FileWriter filewriter = new FileWriter(file);
            StringReader stringreader = new StringReader(s);
            BufferedReader bufferedreader = new BufferedReader(stringreader);
            String lineread = "";
            while ((lineread = bufferedreader.readLine()) != null){
                filewriter.write(lineread + "\r\n");
            }
            filewriter.close();
        }catch (FileNotFoundException fnfe){
            System.err.println("FileNotFoundException: " + fnfe.getMessage());

        }catch (IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());

        }

    }
    public void actionPerformed(ActionEvent evt )
    {
        Object src=evt.getSource();
        if(src==button1 )
        {

         File f = getAFileForSave();
         String s = text1.getText()+"\n"+text2.getText()+"\n"
                     +text3.getText()+"\n"+area4.getText();
         writeStringToFile(f,s);
       
        }
       
    }
}
hiwa - 29 Oct 2006 05:27 GMT
---
Your local variables declared in the constructors hide
all the class fields. So, your button et al are all null.
hiwa - 29 Oct 2006 05:42 GMT
Here's a canonical form of Swing GUI program involving a long I/O task.
See:
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
------------------------------------------------
/*
  my goal in my code is to save the form1 in a text file ;but when you
  run this code on your computers the button of the "save as file"
  doesn't respond on clicking and show me the saveDialog window. by
the
  way,I donot have compile error
  I want to test my code and know where is my error and send me an
  explanation

  MY CODE:
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Form1 extends JFrame implements ActionListener{
 JFrame frame;
 JFileChooser filechooser;
 JTextField text1, text2, text3, text4;
 JLabel label1, label2, label3, label4;
 JTextArea  area4 ;
 JPanel pane;
 JButton saveButton;
 File saveFile;

 public Form1 (String name){
   super(name);
   setSize(350,500);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   text1 = new JTextField (30);
   text2 = new JTextField (30);
   text3 = new JTextField (30);
   area4 = new JTextArea  (10,30);

   saveButton = new JButton("save to file");
   saveButton.addActionListener(this);

   label1 = new JLabel ("Name");
   label2 = new JLabel ("Type");
   label3 = new JLabel ("Date");
   label4 = new JLabel ("Description");

   pane = new JPanel();
   pane.add(label1);
   pane.add(text1);
   pane.add(label2);
   pane.add(text2);
   pane.add(label3);
   pane.add(text3);
   pane.add(label4);
   pane.add(area4);

   pane.add(saveButton);
   setContentPane(pane);
   area4.setLineWrap(true);
   area4.setWrapStyleWord(true);
   setVisible(true);
   frame = this;
 }

 public static void main (String [] arguments){
   Form1 f = new Form1("form");
 }

 public File getAFileForSave(){
   try{
     SwingUtilities.invokeAndWait(new Runnable(){ // here we have to
wait
       public void run(){                         // user selection
settled
         JFileChooser filechooser = new JFileChooser(".");
         int replycode = filechooser.showSaveDialog(frame);
         if (replycode == JFileChooser.APPROVE_OPTION){
           saveFile = filechooser.getSelectedFile();
         }
       }
     });
   }
   catch (Exception e){
     e.printStackTrace();
   }
   return saveFile;
 }

 public void writeStringToFile(File file, String s){

   try{
     FileWriter filewriter = new FileWriter(file);
     StringReader stringreader = new StringReader(s);
     BufferedReader bufferedreader = new BufferedReader(stringreader);
     String lineread = "";
     while ((lineread = bufferedreader.readLine()) != null){
       filewriter.write(lineread + "\r\n");
     }
     filewriter.close();
   }
   catch (FileNotFoundException fnfe){
     System.err.println("FileNotFoundException: " +
fnfe.getMessage());
   }
   catch (IOException ioe){
     System.err.println("IOException: " + ioe.getMessage());
   }
 }

 public void actionPerformed(ActionEvent evt){
   Object src = evt.getSource();

   if (src == saveButton){
     Thread t = new Thread(){
       public void run(){
         File f = getAFileForSave();
         String s = text1.getText() + "\n" + text2.getText() + "\n"
          + text3.getText() + "\n" + area4.getText();
         writeStringToFile(f, s);
       }
     };
     t.start();
   }
 }
}
-------------------------------------------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.