Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default [SOLVED] Help with JTextFile display

    Hi all,

    I am new to this forum and this is my first post: I am a Java beginner and I hope to have some help from you.

    I have a method that reads and return how many rows there are in a txt file, and it works fine.

    Java Code:
    public int numberOfLines(){
            lines = 0;
            if(checkIsFile()){
                try{
                    FileReader fr = new FileReader(fileToImport);
                    BufferedReader br = new BufferedReader(fr);
                    while((br.readLine()!=null)){
                        lines++;
                    }//end while loop
                    br.close();
            }catch(Exception e){
                    System.out.println(e);
                }
            }
              else{
                   System.out.println("There is no file to import");
                    }
            return lines;
            }
    I would like to have this returned number (total rows in file) into a JTextField, as the attached picture: how should I modify my method?

    Thanks!

    Susanna Ferrari
    Attached Thumbnails Attached Thumbnails Help with JTextFile display-a00061.png  
    Last edited by susieferrari; 03-13-2011 at 08:39 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I don't think that the method needs any modification for what you desire, since it shouldn't even be involved in displaying to a JTextField. It should open the file, read the number of lines and return the int and nothing more, since a good rule of thumb is that a method should do one thing only. Another method can call this method and then use the int returned to set the JTextField's text (after converting it to a String with String.valueOf(...)).

    If I'd change anything in the method above, I'd give it a String parameter so you could tell it which file to open and check via the parameter. I'd also put the close() method in the try/catch's finally block, meaning you'd have to declare the BufferedReader before the block.

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    272
    Rep Power
    4

    Default

    Assuming your JTextField is named jTextField:

    Java Code:
    jTextField.setText(String.valueOf(numberOfLines()));

  4. #4
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    Thanks for your quick replies: I've added as actionPerformed to the jTextField your suggestion, can't understand why I get this error as the attached picture.

    jTextField is in main class while numberOfLines method is in another class (FileImport class) , is declared as public and it is in the same package of main class.

    Susanna
    Attached Thumbnails Attached Thumbnails Help with JTextFile display-a00062.jpg  

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    272
    Rep Power
    4

    Default

    Ok, if your Main class is for example mainClass then:
    Java Code:
    jTextField.setText(String.valueOf(mainClass.numberOfLines()));

    is declared as public and it is in the same package of main class.

    Still you have to be explicit about where your method is: mainClass.numberOfLines()

  6. #6
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    My main class is SplitJFrame (I have a GUI in this main class with the jTextField), while the other class is FileImport where I have the methods to read and create arrays for a txt file.

    I've try to add both classes name to this method and also in create a new object where to call numberOfLines method but it seems it doesn't see this method.

    Susanna
    Attached Thumbnails Attached Thumbnails Help with JTextFile display-a00063.jpg   Help with JTextFile display-a00064.jpg   Help with JTextFile display-a00065.png  
    Last edited by susieferrari; 03-13-2011 at 07:25 PM.

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    272
    Rep Power
    4

    Default

    Did you call your FileImport class from SplitJFrame?
    Something like:

    Java Code:
    class SplitJFrame
    
        ...
        
        FileImport fileImport = new FileImport();
    
        ...

    In that case you should put fileImport.numberOfLines().

    If you didn't go this way then let me know from where you have instantiated your FileImport.

    To call just FileImport.numberOfLines() means FileImport is static and then your numberOfLines() also have to be static, but this I would not recomand.

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    272
    Rep Power
    4

    Default

    One scenario:

    Java Code:
    private void jFileReaderActionPerformed(java.awt.event.ActionEvent evt) {                                            
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new TxtFileFilter());
        int returnVal = fileChooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION){
    
            File myFile = fileChooser.getSelectedFile();
            FileImport obj1 = new FileImport(myFile);
            [B]jTotalRowsTextField.setText(String.valueOf(obj1.numberOfLines()[/B]
            System.out.println(obj1.checkIsFile());
            System.out.println(obj1.numberOfLines());
    
          obj1.importToArray();
          obj1.printArray();
    
          System.out.println("--------------------------------------");
    
          obj1.buildDataArray(obj1.getArray());
          obj1.printDataArray();
     }                                           
        }
        private void jTotalRowsTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                    
            [B]// you don't need this method unless you are going to do some actions in responce to tapping ENTER while this component is in focus[/B]
    }

    This should work if your jTotalRowsTextField is reachible from jFileReaderActionPerformed method.
    Please try and let me know if this works.

  9. #9
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    It works!

    Thank you very much milovan, now it works perfectly!

    Susanna

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    272
    Rep Power
    4

    Default

    Great! Good luck with your project... Mark this thread as solved :-)

  11. #11
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    Sure I will have to ask again for your help...I hope to not disturb you too much!

    Have a good night

    Susanna

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  2. no display
    By Kyle227 in forum Java Applets
    Replies: 6
    Last Post: 07-22-2010, 10:01 AM
  3. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  4. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  5. Replies: 0
    Last Post: 06-28-2008, 09:26 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
  •