Results 1 to 16 of 16
  1. #1
    gezzel is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default how to use JFileChooser to select file -> string

    I'm trying to use jfilechooser to select a text file, and convert it to a string that i can manipulate. The file is going to be fairly large so i don't know if i need a buffer or anything.

    My end goal is to select a text file, format it by replacing certain strings, and then output the newly formatted text file.

    Thanks in advance

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The WebPageScanner app on this page shows a way to read a File into a String.

  3. #3
    mypoortom is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    I'm having problem in adding a MouseListener to FilePane of JFileChooser.
    My end goal is: show my own PopupMenu in FilePane, but I can't handle the MouseEvent, FilePane always show its default PopupMenu, bypass my mouse handler.
    Does gezzel, hardwired or someone has experience in this stuff???
    I can show an example code here:
    /*Class fileChooser_Frame, include main() method, is Runable */
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import sun.swing.FilePane;
    import javax.swing.JFileChooser;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JList;
    import java.awt.Point;
    import javax.swing.JPopupMenu;
    import javax.swing.JMenuItem;

    public class fileChooser_Frame extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    public fileChooser_Frame() {
    try {
    jbInit();
    } catch (Exception exception) {
    exception.printStackTrace();
    }
    }

    private void jbInit() throws Exception {
    getContentPane().setLayout(borderLayout1);
    JFileChooser jF = new JFileChooser();
    FilePane filePane = (FilePane) jF.getComponent(2);
    final JPopupMenu jPop = new JPopupMenu("Testing");
    JMenuItem jMenuItem = new JMenuItem("Item 1");
    jPop.add(jMenuItem);

    MouseListener mouseListener = new MouseListener(){
    public void mouseClicked(MouseEvent e) {
    System.out.println("Mouse Clicked");
    checkPopup(e);
    }
    public void mousePressed(MouseEvent e) {
    System.out.println("Mouse Pressed");
    checkPopup(e);
    }
    public void mouseReleased(MouseEvent e) {
    System.out.println("Mouse Released");
    checkPopup(e);
    }
    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    private void checkPopup(MouseEvent e){
    if(e.isPopupTrigger())
    jPop.show(e.getComponent(),e.getX(),e.getY());
    }
    };
    filePane.addMouseListener(mouseListener);
    getContentPane().add(filePane, BorderLayout.CENTER);
    /*The result here is: FilePane show only its default PopupMenu, not my jPop :((, I need help here, pls*/
    }

    public static void main(String[] args) {
    fileChooser_Frame filechooser_frame = new fileChooser_Frame(); filechooser_frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
    filechooser_frame.pack();
    filechooser_frame.setVisible(true);
    }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    I'm having problem
    Do you get an error message?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I think you cannot use the FilePane API. At the time with the latest JDK version it's deprecated I think.

  6. #6
    mypoortom is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Do you get an error message?
    Oh, I got no error message. It just doesn't show my own PopupMenu.
    Even when I have removed all MouseListener, like this:
    Java Code:
    MouseListener[] mouseList = filePane.getMouseListeners();
            for(int i = 0 ; i < mouseList.length; i++){
                filePane.removeMouseListener(mouseList[i]);
            }
    the FilePane still showed its default PopupMenu.
    Do you have any solution?

  7. #7
    mypoortom is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I think you cannot use the FilePane API. At the time with the latest JDK version it's deprecated I think.
    Oops, I don't know this :(
    I'm using JDK 1.5 , with JBuilder 2006.
    So, if I update my JDK, will JFileChooser be easier to handle? I haven't tried this...

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not that it's easier to handle, it's depends on how did you handle it. What I want to say is better to use the best solution, FilePane can be deprecated very soon.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Darryl, seems you are hang on with lots of forums. ;)

    I've no idea what to do with such instances.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    Eranga, I'm not active or even a member on all those forums, but when I saw the topic on the Sun forum I remembered having seen it here, but had forgotten that it was a thread hijack in the first place, so the topic was something different.

    So then I went to Google and discovered the others ;)

    It's sad that people can be so discourteus. I often feel that reading How To Ask Questions The Smart Way should be made compulsory when signing up for any forum membership.

    db

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, thanks for that link lol. If you can please put it into your sig. I'll put it in mine too. I read it on a previous post you have send in another thread. Thanks.

    Actually I agreed with you that what pointing here. I don't like to even read such a thread. But I cannot, because as a mod I have to. I'll think of your suggestion, actually lots of members are pointing this. But there are lots of circumstances to think about. I've to discuss this with Administrators as well. I'll try to so something on this soon.

  13. #13
    mypoortom is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    My English is not good enough to understand all previous posts of Eranga & Darryl, but I feel that I'm a bad guy here... :-?
    I'm a "cross poster" <== I have just understood this word recently, and it means that I'm really bad, right?
    I have Google about my problem, but nothing there... And I'm really on a fire after long days sitting but still couldn't find the right solutions, with no much time left for my project, so I SOS the experts.
    This is the first time I do something like this!
    And Darryl, you did help me by posting some "useful" links for me... :(
    I have respect for Eranga and Norm, and also for other posters that gave me useful advices, so that encourage me to continue, and that's why I posted previous replies, with my DEEPEST RESPECT. I did not make a "hit and run". I'm not that bad kind of cross poster.
    PS: I have fixed the problem: write a method:
    Java Code:
    recursiveAddMouseListener(Container container, MouseListener mouseListener){...};
    , nearly like method recursiveSetInheritsPopupMenu(Container container, boolean b){}; in FilePane source code!
    Last edited by mypoortom; 10-22-2008 at 07:54 PM.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    From
    JavaRanch FAQ : Carefully Choose One Forum

    If you post the same question to two forums, and two different people each post essentially the same answer to one of the two threads, then one of those two people has wasted her time. That's a shame -- there's only so much time to go around, and because of that waste, someone else's question might not get answered.

    db

    edit Also
    JavaRanch FAQ : Be Forthright When Cross Posting To Other Sites
    Last edited by DarrylBurke; 10-22-2008 at 08:57 PM.

  15. #15
    mypoortom is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Can u say that: u're wasting time when saving a human life? You are not in my case, you have enough time to search for some cross posters, not me, I really need someone help coz my G-day is coming, this is my career!
    If i'm not in a hurry, I will never do this. Why don't you try to find my other cross posts, will you find any other of mine? Come on...
    If 2 different experts post the same answers in 2 different forums, it assert that: that's a good solution that I should follow, there's no redundant, no wasting there. And I promise definitely that I will say THANKS, and have respect for them, cause they're saving me, not just helping me.
    And this is my last reply for this topic, I post this becoz I just don't want somebody say that: this guy is just a hit and run dog!
    PS: Again, I want to give my deepest respect to Eranga and Norm, and other experts! Please continue helping people, and sometimes, maybe you don't know, that you are SAVING people with your useful posts!

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

    Default

    Can u say that: u're wasting time when saving a human life? You are not in my case, you have enough time to search for some cross posters, not me, I really need someone help coz my G-day is coming, this is my career!
    This is quite selfish of you. If you need help that badly, then you should either talk to your instructor or hire a tutor. We are all volunteers and should not be expected to do extra work or futile duplication of work that has been done elsewhere just because you have a deadline.
    Come on...
    and back at you!
    The least you can do is to provide links within your posts to your cross-posts to avoid duplication of efforts. The next step is yours.

Similar Threads

  1. [SOLVED] jfilechooser for saving a file....?
    By prabhurangan in forum AWT / Swing
    Replies: 10
    Last Post: 10-06-2013, 10:17 PM
  2. Cannot display a random string from .dat file
    By explosion242 in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 01:48 PM
  3. can i update the tag value of XMl file by other value(string
    By pankaj_salwan in forum Advanced Java
    Replies: 7
    Last Post: 07-04-2008, 08:12 AM
  4. Need JFileChooser Help
    By Wraithier in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 05:40 PM
  5. Read a string from a txt file
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:50 AM

Posting Permissions

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