7 Replies - 4030 Views - Last Post: 31 July 2010 - 02:45 PM Rate Topic: -----

#1 RebelCom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-July 10

Help with JFileChooser

Posted 27 July 2010 - 02:59 AM

Hi all,

I have the following code to parse a file, I know the parsing works as I had hard coded in a path to a file. I am now trying to give the user the option of picking a file to parse. I am having trouble editing the code to use the JFileChooser:

import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.swing.JFileChooser;
import javax.swing.JFrame;


public class XMLReader
{

 public static void main(String argv[]) {
     
     // create a file chooser
     JFileChooser fileChooser = new JFileChooser();
     // show open file dialog
     int result = fileChooser.showOpenDialog( null );
     if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
     {
         File file = null;
         try
         {
         // get the file 
         File file = fileChooser.getSelectedFile();
         } // end try
         catch ( Exception e )
          {
              System.err.println( "Could not find file" );
           } // end catch
           if ( file != null )
           {
               File file = new file( );
     
     ArrayList timeStamp = new ArrayList();
     ArrayList Price = new ArrayList();

  try {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(file);
  doc.getDocumentElement().normalize();
  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  NodeList nodeLst = doc.getElementsByTagName("reading");
  System.out.println("Company Data");

  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
    
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  
      Element fstElmnt = (Element) fstNode;
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("timeStamp");
      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
      NodeList fstNm = fstNmElmnt.getChildNodes();
      String timeStamp = fstNm.item(0).getNodeValue(); 
      timeStamp.add(timeStamp); 
      System.out.println("timeStamp : "  + ((Node) fstNm.item(0)).getNodeValue());
      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Price");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      String YValue = lstNm.item(0).getNodeValue(); 
      Price.add(YValue);
      System.out.println("Price : " + ((Node) lstNm.item(0)).getNodeValue());
    }

  }
  } catch (Exception e) {
    e.printStackTrace();
  }
  System.out.println(timeStamp);
  System.out.println(Price);
 }
}

}
}




Any suggestions would be greatly received.

Thanks,
RebelCom.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with JFileChooser

#2 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 253
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Help with JFileChooser

Posted 27 July 2010 - 05:12 AM

You repeatedly try to use variables that have already been declared, i.e. variables with the same name. On line 24 you declare a file named file, yet you attempt to do so again twice, on lines 28 and 36.

//line 28
file = fileChooser.getSelectedFile();



Delete line 36.

Line 60 attempts to declare a String named TimeStamp. Sorry, but you earlier declared an ArrayList with the same name. Fix that, I used timeStamp 2 for now, but you can fix to your liking.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with JFileChooser

Posted 27 July 2010 - 04:28 PM

Are we talking about compliation errors here ?
The post wasn't clear at all about that
Rule #5 of this forum Describe any errors you are encountering. Help us help you!
You'll save us a lot of time
Was This Post Helpful? 0
  • +
  • -

#4 RebelCom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-July 10

Re: Help with JFileChooser

Posted 29 July 2010 - 03:24 AM

This is the code as it stands:
import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class XMLReader2
{

 public static void main(String argv[]) {
     
     // create a file chooser
     JFileChooser fileChooser = new JFileChooser();
     // show open file dialog
     int result = fileChooser.showOpenDialog( null );
     File file = new File ( result );
     file = fileChooser.getSelectedFile();
     ArrayList XTimeStamp = new ArrayList();
     ArrayList Price = new ArrayList();

  try {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(file);
  doc.getDocumentElement().normalize();
  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  NodeList nodeLst = doc.getElementsByTagName("reading");
  System.out.println("Company Data");

  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
    
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  
      Element fstElmnt = (Element) fstNode;
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("XTimeStamp");
      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
      NodeList fstNm = fstNmElmnt.getChildNodes();
      String timeStamp = fstNm.item(0).getNodeValue(); 
      XTimeStamp.add(timeStamp); 
      System.out.println("XTimeStamp : "  + ((Node) fstNm.item(0)).getNodeValue());
      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Price");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      String YValue = lstNm.item(0).getNodeValue(); 
      Price.add(YValue);
      System.out.println("Price : " + ((Node) lstNm.item(0)).getNodeValue());
    }

  }
  } catch (Exception e) {
    e.printStackTrace();
  }
  System.out.println(XTimeStamp);
  System.out.println(Price);
 }
}




This is the error message I get when I try to compile it:

XMLReader2.java:28:cannot find symbol
symbol: constructor File (int)
location: class java.io.File
File file = new File ( result );

Note: XMLReader2.java uses unchecked or unsafe operations.
Note: Recompile wit -Xlint:unchecked for details.
Was This Post Helpful? 0
  • +
  • -

#5 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Help with JFileChooser

Posted 29 July 2010 - 03:39 AM

If you look at the File class, you can see there isn't a constructor that takes an integer. :)

Constructor Summary

File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with JFileChooser

Posted 29 July 2010 - 03:19 PM

File file = new File ( result );  
file = fileChooser.getSelectedFile();  


What are you trying to do there ? Should simply be

File file = fileChooser.getSelectedFile();

You need to try to create a File object (with a constructor that does not exists) and then overload that object with the one provided by getSelectedFile()
Was This Post Helpful? 1
  • +
  • -

#7 RebelCom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-July 10

Re: Help with JFileChooser

Posted 31 July 2010 - 02:38 PM

Have been searching around the internet and doing a lot of code editing but still having trouble with the fileChooser. Here is the code I have at present:
import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class XMLReader2 extends JFrame implements ActionListener {
   private JEditorPane jep = new JEditorPane();
   public JFileChooserExample() {
      super("Editor");
      Container cp = getContentPane( );
      cp.add(new JScrollPane(jep), BorderLayout.CENTER);
      JMenu menu = new JMenu("File");
      menu.add(make("Open"));
	  menu.add(make("Quit"));
      JMenuBar menuBar = new JMenuBar();
      menuBar.add(menu); setJMenuBar(menuBar);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(200,300);
   }
   
      public void actionPerformed(ActionEvent e) {
      String ac = e.getActionCommand();
      if(ac.equals("Open")) openFile();
      else if(ac.equals("Save")) saveFile();
      else if(ac.equals("Quit")) System.exit(0);
   }

   private void openFile() {
      JFileChooser jfc = new JFileChooser();
      int result = jfc.showOpenDialog(this);
      if(result == JFileChooser.CANCEL_OPTION) return;
      try {
         File file = jfc.getSelectedFile();
         BufferedReader br = new BufferedReader(new FileReader(file));
         String s=""; int c=0;
         while((c=br.read())!=-1) s+=(char)c;
         br.close(); jep.setText(s);
      } catch (Exception e) {
         JOptionPane.showMessageDialog(this,e.getMessage(),
         "File error",JOptionPane.ERROR_MESSAGE);}
   }
    private JMenuItem make(String name) {
      JMenuItem m = new JMenuItem(name);
      m.addActionListener(this);
      return m;
   }
	public static void main(String args) {
		// create a file chooser
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setDialogTitle("Choose a file");
		this.getContentPane().add(fileChooser);
		fileChooser.setVisible(true);
		if(returnVal == JFileChooser.APPROVE_OPTION)
		{
			file = fileChooser.getSelectedFile();
	}
     ArrayList XTimeStamp = new ArrayList();
     ArrayList Price = new ArrayList(  );

  try {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse( file );
  doc.getDocumentElement().normalize();
  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  NodeList nodeLst = doc.getElementsByTagName("reading");
  System.out.println("Company Data");

  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
    
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  
      Element fstElmnt = (Element) fstNode;
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("XTimeStamp");
      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
      NodeList fstNm = fstNmElmnt.getChildNodes();
      String timeStamp = fstNm.item(0).getNodeValue(); 
      XTimeStamp.add(timeStamp); 
      System.out.println("XTimeStamp : "  + ((Node) fstNm.item(0)).getNodeValue());
      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Price");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      String YValue = lstNm.item(0).getNodeValue(); 
      Price.add(YValue);
      System.out.println("Price : " + ((Node) lstNm.item(0)).getNodeValue());
    }

	}
  } catch (Exception e) {
    e.printStackTrace();
		}
  System.out.println(XTimeStamp);
  System.out.println(Price);
	}
 }



This code does not compile it returns the following error:
error: Class names, 'XMLReader2', are only accepted if annotation processing is explicitly requested
1 error



What does this error mean?
Was This Post Helpful? 0
  • +
  • -

#8 Mercurial   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Help with JFileChooser

Posted 31 July 2010 - 02:45 PM

Are you compiling in cmd? Have you added .java at the end?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1