Results 1 to 10 of 10
  1. #1
    matrixellox is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Unhappy The variables are not reseting even when I specify

    so I am trying to reset the variable so when I reopen the open dialog and select an invalid file the error message in the textfield will show
    See= openFile and display

    import java.awt.*;
    import java.io.*;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    import java.awt.event.*;


    /**
    * This program opens a JFrame window with a JMenuBar with the JMenu, "File",
    * The JMenu has a JMenuItem, "Quit", which has an ActionListener. When executed,
    * it also automatically displays an open dialog, where you can browse the specified
    * directory. If the file is correctly chosen, the first line of the text is outputted
    * in the JTextField.
    * <p>
    * @author Darshil Parikh
    * @version 1.00 February 9, 2011
    */

    public class FileChooser extends JFrame implements ActionListener
    {
    File fileName;
    String fileHeader;
    JPanel p;
    int d=0;
    JTextField t;

    /**
    * This is the constructor of the program. The constructor creates the JFrame window with
    * JMenu, JMenuBar, and JMenuItem and sets up the JPanel for other components. It also adds
    * ActionLsitener to the JMenuItem, "Quit". The cosntructor sets the size, visibilty and
    * location of the JFrame window.
    * <p>
    */
    public FileChooser ()
    {
    super ("JFrame Test");
    p = new JPanel ();
    getContentPane ().add (p);

    JMenuItem quitItem = new JMenuItem ("Quit");
    JMenuItem openItem=new JMenuItem ("Browse for a new file");

    JMenu fileMenu = new JMenu ("File");

    fileMenu.add (quitItem);
    fileMenu.add (openItem);

    JMenuBar myMenus = new JMenuBar ();

    myMenus.add (fileMenu);

    setJMenuBar (myMenus);

    quitItem.addActionListener (this);
    openItem.addActionListener(this);
    setSize (300, 300);
    setVisible (true);

    openFile ();
    display ();

    setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
    p.updateUI ();
    }

    /**
    * This method sets an action when the user clicks "Quit" which is closing the windoe
    * <p>
    *@param ae A variable that is used to get the action command
    */
    public void actionPerformed (ActionEvent ae)
    {
    if (ae.getActionCommand ().equals ("Quit"))
    {
    System.exit (0);
    }
    else
    {
    openFile();
    }
    }

    /**
    * This method specifies the file directory to the whole hard drive,
    * including removable discs. Then it adds an optional file filter
    * for displaying files with the extension .brb. Then it errortraps
    * the selection by not opening non-BRB files and files that do not
    * have the file header as "DPX". If there are errors, then a message
    * dialog will be displayed indicating the error and an "OK" button.
    * <p>
    */
    private void openFile ()
    {
    JFileChooser fileChooser = new JFileChooser (".\\");

    fileChooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
    ExampleFileFilter filter = new ExampleFileFilter ();
    filter.addExtension ("txt");
    filter.setDescription ("Be Right Back Files");
    fileChooser.setFileFilter (filter);
    fileChooser.showOpenDialog (this);
    fileName = fileChooser.getSelectedFile ();
    String name= fileName.toString();
    if (fileName == null || fileName.getName ().equals ("") || fileName.getName ().length () > 255)
    {
    JOptionPane.showMessageDialog (this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
    d=1;
    }
    else
    {
    BufferedReader input;

    try
    {
    input = new BufferedReader (new FileReader (fileName));
    fileHeader = input.readLine ();
    d=1;
    if (!name.substring(name.length()-4).equals (".txt"))
    {
    JOptionPane.showMessageDialog (this, "Invalid File Type", "Invalid File Type", JOptionPane.ERROR_MESSAGE);
    if (!fileHeader.equals ("DPX"))
    {
    JOptionPane.showMessageDialog (this, "Invalid File Header", "Invalid File Header", JOptionPane.ERROR_MESSAGE);
    }
    else
    {
    d=-1;
    }
    }
    else
    {
    d=-1;
    }
    }
    catch (Exception e)
    {
    }
    }
    }

    /**
    * This method displays different components on the panel depending on the
    * file selection. If the file selection was correct, then it will displays the
    * fileHeader in the text file, but if the file selection errors, it will display
    * the error message. This method also update its UI after each display.
    * <p>
    */
    public void display ()
    {

    JLabel n = new JLabel ("Your string was");
    JButton b =new JButton("Browse File");
    t = new JTextField (10);
    p.add (n);
    p.add (t);
    if (d < 0)
    {
    t.setText(fileHeader);
    }
    else
    {
    if (d > 0)
    {
    t.setText("Invalid File, please browse for a correct one by using the menu item");
    }
    }
    d=0;
    }

    /**
    * This is the main of the file FileChooser, it is the
    * driver method of the other methods and executes the program.
    * <p>
    * @param args This parameter calls the argument
    */
    public static void main (String[] args)
    {
    new FileChooser ();

    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Too much unformatted code.
    No question
    Vague description: reset what variable?

    Java Code:
    catch (Exception e)
    {
    }
    Also, never ever do this as you have no idea if something went wrong with your code or not. At the very least add a call to printStackTrace.

  3. #3
    matrixellox is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    the vairable d is not resetting to show the error message
    what do u mean by unformatted code

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by matrixellox View Post
    what do u mean by unformatted code
    Isn't it obvious by looking at your original post? Or do you always write code like that?

    When posting code place code tags around it. [ code ] before and [ /code ] after but without the spaces.

  5. #5
    matrixellox is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    well i am new here so i will make sure next time but my output in the text field doesn't change when i browse a new file, stays the same

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Not next time. NOW! I am not going to try and resolve your problem by reading through a bunch of unformatted code which is duifficult to read. It is your responsibility to provide as much relevant information as possible in a readable format otherwise people are not going to bother to help you. This has nothing to do witt being new to the forum (other than not knowing about code tags which you could have found out about if you read about the forums rules etc) and everything to do with common sense when communicating with people.

  7. #7
    matrixellox is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    PHP Code:
    // The "Test" class.
    /*
     *
     This is an EXAMPLE of JFileChooser!  This coding is INCOMPLETE and does not errortrap
     for all contingencies!
     
     You WILL need to make this more functional and do extensive errortrapping.  Right now it opens
     just about anything and attempts to load it into a single string. If it is unsuccessful it
     crashes.
     
     NOTE:  DO NOT use common file extensions for your files!  Create your own unique
     extension that only YOUR program can read.
     
     */
    
    import java.awt.*;
    import java.io.*;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    /**
     * This program opens a JFrame window with a JMenuBar with the JMenu, "File",
     * The JMenu has a JMenuItem, "Quit", which has an ActionListener. When executed,
     * it also automatically displays an open dialog, where you can browse the specified 
     * directory. If the file is correctly chosen, the first line of the text is outputted
     * in the JTextField.
     * <p>
     * @author Darshil Parikh
     * @version 1.00 February 9, 2011
     */
    
    public class FileChooser extends JFrame implements ActionListener
    {
      File fileName;
      String fileHeader;
      JPanel p;
      int d=-1;
      JTextField t;
      
      /**
       * This is the constructor of the program. The constructor creates the JFrame window with 
       * JMenu, JMenuBar, and JMenuItem and sets up the JPanel for other components. It also adds
       * ActionLsitener to the JMenuItem, "Quit". The cosntructor sets the size, visibilty and 
       * location of the JFrame window.
       * <p>
       */
      public FileChooser ()
      {
        super ("JFrame Test");
        p = new JPanel ();
        getContentPane ().add (p);
        
        JMenuItem quitItem = new JMenuItem ("Quit");
        JMenuItem openItem=new JMenuItem ("Browse for a new file");
        
        JMenu fileMenu = new JMenu ("File");
        
        fileMenu.add (quitItem);
        fileMenu.add (openItem);
        
        JMenuBar myMenus = new JMenuBar ();
        
        myMenus.add (fileMenu);
        
        setJMenuBar (myMenus);
        
        quitItem.addActionListener (this);
        openItem.addActionListener(this);
        setSize (300, 300);
        setVisible (true);
        
        openFile ();
        display ();
        
        setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        p.updateUI ();
      }
      
      /**
       * This method sets an action when the user clicks "Quit" which is closing the windoe
       * <p>
       *@param ae A variable that is used to get the action command
       */
      public void actionPerformed (ActionEvent ae)
      {
        if (ae.getActionCommand ().equals ("Quit"))
        {
          System.exit (0);
        }
        else 
        {
          openFile();
        }
      }
      
      /**
       * This method specifies the file directory to the whole hard drive, 
       * including removable discs. Then it adds an optional file filter 
       * for displaying files with the extension .brb. Then it errortraps 
       * the selection  by not opening non-BRB files and files that do not 
       * have the file header as "DPX". If there are errors, then a message 
       * dialog will be displayed indicating the error and an "OK" button.
       * <p>
       */
      private void openFile ()
      {
        JFileChooser fileChooser = new JFileChooser (".\\");
        
        fileChooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
        ExampleFileFilter filter = new ExampleFileFilter ();
        filter.addExtension ("txt");
        filter.setDescription ("Be Right Back Files");
        fileChooser.setFileFilter (filter);
        fileChooser.showOpenDialog (this);
        fileName = fileChooser.getSelectedFile ();
        String name= fileName.toString();
        if (fileName == null || fileName.getName ().equals ("") || fileName.getName ().length () > 255)
        {
          JOptionPane.showMessageDialog (this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
          d=1;
        }
        else
        {
          BufferedReader input;
          
          try
          {
            input = new BufferedReader (new FileReader (fileName));
            fileHeader = input.readLine ();
            d=1;
            if (!name.substring(name.length()-4).equals (".txt"))
            {
              JOptionPane.showMessageDialog (this, "Invalid File Type", "Invalid File Type", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
              d=-1;
            }
            if (!fileHeader.equals ("DPX"))
            {
              JOptionPane.showMessageDialog (this, "Invalid File Header", "Invalid File Header", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
              d=-1;
            }
          }
          catch (Exception e)
          {
          }
        }
      }
      
      /**
       * This method displays different components on the panel depending on the
       * file selection. If the file selection was correct, then it will displays the 
       * fileHeader in the text file, but if the file selection errors, it will display
       * the error message. This method also update its UI after each display.
       * <p>
       */
      public void display ()
      {
        
        JLabel n = new JLabel ("Your string was");
        JButton b =new JButton("Browse File");
        t = new JTextField (10);
        p.add (n);
        p.add (t);
        try
        {
        if (d < 0)
        {
          t.setText(fileHeader);   
        }
        }
        catch (Exception e) 
        {
          t.setText("Invalid File, please browse for a correct one by using the menu item");
        }
        d=0;
      }
      
      /**
       * This is the main of the file FileChooser, it is the
       * driver method of the other methods and executes the program.
       * <p>
       * @param args This parameter calls the argument
       */
      public static void main (String[] args)
      {
        new FileChooser ();
        
      } 
    }

  8. #8
    matrixellox is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    ^is that better

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Much better. Now all we need is a better explanation of your problem.

    You say that the variable d is not getting reset. Under what conditions are you expecting it to change, to what value do you think is should be changed to and what value is it actually?

  10. #10
    matrixellox is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    so i figured, i didnt really need the variable d, but my new problemis that i added an errortrap for the cancel button but the error trap wont work and when i click cancel there is a nullpointerexception error in the interactions pane


    PHP Code:
    // The "Test" class.
    /*
     *
     This is an EXAMPLE of JFileChooser!  This coding is INCOMPLETE and does not errortrap
     for all contingencies!
     
     You WILL need to make this more functional and do extensive errortrapping.  Right now it opens
     just about anything and attempts to load it into a single string. If it is unsuccessful it
     crashes.
     
     NOTE:  DO NOT use common file extensions for your files!  Create your own unique
     extension that only YOUR program can read.
     
     */
    
    import java.awt.*;
    import java.io.*;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    /**
     * This program opens a JFrame window with a JMenuBar with the JMenu, "File",
     * The JMenu has a JMenuItem, "Quit", which has an ActionListener. When executed,
     * it also automatically displays an open dialog, where you can browse the specified 
     * directory. If the file is correctly chosen, the first line of the text is outputted
     * in the JTextField.
     * <p>
     * @author Darshil Parikh
     * @version 1.00 February 9, 2011
     */
    
    public class FileChooser extends JFrame implements ActionListener
    {
      File fileName;
      String fileHeader;
      JPanel p;
      JTextField t;
      int result;
      
      /**
       * This is the constructor of the program. The constructor creates the JFrame window with 
       * JMenu, JMenuBar, and JMenuItem and sets up the JPanel for other components. It also adds
       * ActionLsitener to the JMenuItem, "Quit". The cosntructor sets the size, visibilty and 
       * location of the JFrame window.
       * <p>
       */
      public FileChooser ()
      {
        super ("JFrame Test");
        p = new JPanel ();
        getContentPane ().add (p);
        
        JMenuItem quitItem = new JMenuItem ("Quit");
        JMenuItem openItem=new JMenuItem ("Browse for a new file");
        
        JMenu fileMenu = new JMenu ("File");
        
        fileMenu.add (quitItem);
        fileMenu.add (openItem);
        
        JMenuBar myMenus = new JMenuBar ();
        
        myMenus.add (fileMenu);
        
        setJMenuBar (myMenus);
        
        quitItem.addActionListener (this);
        openItem.addActionListener(this);
        setSize (300, 300);
        setVisible (true);  
        openFile();
        display();
        setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
      }
      
      /**
       * This method sets an action when the user clicks "Quit" which is closing the windoe
       * <p>
       *@param ae A variable that is used to get the action command
       */
      public void actionPerformed (ActionEvent ae)
      {
        if (ae.getActionCommand ().equals ("Quit"))
        {
          System.exit (0);
        }
        else 
        {
          openFile();
        }
      }
      
      /**
       * This method specifies the file directory to the whole hard drive, 
       * including removable discs. Then it adds an optional file filter 
       * for displaying files with the extension .brb. Then it errortraps 
       * the selection  by not opening non-BRB files and files that do not 
       * have the file header as "DPX". If there are errors, then a message 
       * dialog will be displayed indicating the error and an "OK" button.
       * <p>
       */
      private void openFile ()
      {
        JFileChooser fileChooser = new JFileChooser (".\\");
        fileChooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
        ExampleFileFilter filter = new ExampleFileFilter ();
        filter.addExtension ("txt");
        filter.setDescription ("Be Right Back Files");
        fileChooser.setFileFilter (filter);
        result =fileChooser.showOpenDialog (this);
        fileName = fileChooser.getSelectedFile ();
        String name= fileName.toString();
        if (result!=JFileChooser.CANCEL_OPTION && (fileName == null || fileName.getName ().equals ("") || fileName.getName ().length () > 255))
        {
          
          JOptionPane.showMessageDialog (this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
          if (result != JFileChooser.CANCEL_OPTION)
          {
            BufferedReader input;
            try
            {
              input = new BufferedReader (new FileReader (fileName));
              fileHeader = input.readLine ();
              if (!fileHeader.equals ("DPX"))
              {
                JOptionPane.showMessageDialog (this, "Invalid File Header", "Invalid File Header", JOptionPane.ERROR_MESSAGE);
              }
              else if (!name.substring(name.length()-4).equals (".txt"))
              {
                JOptionPane.showMessageDialog (this, "Invalid File Type", "Invalid File Type", JOptionPane.ERROR_MESSAGE);  
              }
              else
              {
                display();
              }
            }
            catch (Exception e)
            {
            }
          }
        }
      }
      
      
      /**
       * This method displays different components on the panel depending on the
       * file selection. If the file selection was correct, then it will displays the 
       * fileHeader in the text file, but if the file selection errors, it will display
       * the error message. This method also update its UI after each display.
       * <p>
       */
      public void display ()
      {
        
        JLabel n = new JLabel ("Your string was");
        t = new JTextField (fileHeader,10);
        p.add (n);
        p.add (t);   
      }
      
      /**
       * This is the main of the file FileChooser, it is the
       * driver method of the other methods and executes the program.
       * <p>
       * @param args This parameter calls the argument
       */
      public static void main (String[] args)
      {
        new FileChooser ();
        
      } 
    }

    Please note: the brb and txt are synonomous, i used txt because i cant create a new brb file

Similar Threads

  1. clearing/reseting text fields in GUI
    By reis3k in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 06:12 PM
  2. reseting text fields; .setText("") best option?
    By Unsub in forum Java Applets
    Replies: 2
    Last Post: 02-23-2010, 02:48 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. [SOLVED] reseting a value
    By s34nn4 in forum Java Applets
    Replies: 5
    Last Post: 11-22-2008, 01:31 PM
  5. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •