Results 1 to 8 of 8
  1. #1
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default Showing the content of a file and show in a text area

    Hi,
    can anybody help me
    How can i create a frame where a button will browse for a file and will show the content of the file in a text area,

    so basically there will be a button,TextBox or something(will show the path of the file choose) and a text area(will show the content of the file),

    Can anybody help me in this...

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

    Default

    Quote Originally Posted by krishanu View Post
    can anybody help me
    Probably.

    How can i create a frame where a button will browse for a file and will show the content of the file in a text area, so basically there will be a button,TextBox or something(will show the path of the file choose) and a text area(will show the content of the file)
    Is this a Swing GUI? AWT? Other?

    Where are you stuck? If Swing, do you know how to create a JFrame and a JButton and how to add an ActionListener to the JButton? Do you know how to add a JTextArea to your GUI using layout managers? Do you know how to use a JFileChooser to select a file? Do you know how to read a file's text into a JTextArea?

    So if you can tell us specifically where you're stuck, we can probably give you specific advice. Right now, I'm going to recommend that you check out these terms above in the Swing tutorials.

    Luck!

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    For selecting a file JFileChooser would probably be the easiest.

    Like Fubarable said, be more specific so we can help you more.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

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

    Default

    If that's all that has to be done, a JFileChooser with an accessory component to display the selected file's content is all that's needed. Go through the tutorial linked from the JFileChooser API.

    db

  5. #5
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Ok.....

    i want to create a button with the name "Browse" and there will be a textbox on the leftside of the button, upon clicking this "browse button" i need to open "file chooser" from where i can select a file..on selecting this specific file , the path will be shown on the "textbox" , there will be a text area which will show the content of the selecting file..
    Using swing....i have to do...

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok, then you need to look at Swing tutorials and API. Use JFileChooser to get your file and follow JosAH's advice.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Here is i am trying to write some code...
    my target is when button will be clicked Filechooser will be opend and we can choose a file and can read the file and show in a text area in the same frame...

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class ButtonAction {
     
        public static void main(String[] args) {
    
    
            final JFrame frame1 = new JFrame("JAVA");
            frame1.setSize(600,600);
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JButton button = new JButton(" Browse a file");
            //Add action listener to button
            button.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    //FileChooser fc = new JFileChooser();
            //fc.showOpenDialog(frame);
    
                }
            });
    
            frame1.getContentPane().add(button);
            frame1.pack();
            frame1.setVisible(true);
        }
    
    
    
        }
    Last edited by JosAH; 07-24-2011 at 05:52 PM. Reason: Added [code] ... [/code] tags

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

    Default

    Try to get that code out of the main method and into a proper object. You'll want to have a JTextArea as a non-static field of the class, you'll want to try to use your JFileChooser rather than comment out the code -- give it a try, you really can't break anything, and you'll want to use code tags when posting code in the forum so we can read it. Please check out the link in my signature for more on how to use code tags.

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. show text file to textArea
    By louboulos in forum New To Java
    Replies: 14
    Last Post: 06-12-2011, 01:16 PM
  3. Replies: 2
    Last Post: 04-27-2011, 04:09 PM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. Replies: 10
    Last Post: 05-24-2010, 08:25 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
  •