Results 1 to 4 of 4
  1. #1
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default open text document using JButton

    Hi Im trying to make an action for a JButton to open. I realise you would need to use JFileChooser and use chooser.getSelectedFile but how can I instruct it to open the file in to a separate window?

    Java Code:
    		JFileChooser chooser = new JFileChooser("C:/OOP"); 
    		status=chooser.showOpenDialog(null);
    		if (status == JFileChooser.APPROVE_OPTION) 
    		{
    			chooser.getSelectedFile();...
                     }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I don't understand what you mean by instruct it to open the file into a separate window? Perhaps you mean once the file is loaded you want it displayed in a separate frame?

    You can display in another window by creating a new JFrame or a new JDialog, whichever you choose, and then adding the text from the file to that frame before you set it visible.

    As for the JButton, add an action listener to the JButton like this:
    Java Code:
    javax.swing.JButton myButton = new javax.swing.JButton("Button text");
    myButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            myButtonAction(evt);
        }
    });
     
    private void myButtonAction(java.awt.event.ActionEvent evt) {
        JFileChooser...
        ...
    }

  3. #3
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    I meant open it as you would if you were to go My Documents and click on the text file. Do you think that is possible?

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Ah then you would use java.awt.Desktop:
    Java Code:
    if(Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        desktop.open(file);
    }

Similar Threads

  1. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  2. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 07:53 AM
  3. Importing text into a word document
    By Frys82 in forum AWT / Swing
    Replies: 5
    Last Post: 08-24-2009, 09:41 PM
  4. i want to open a document from my program
    By akinpam in forum New To Java
    Replies: 1
    Last Post: 12-21-2008, 07:09 PM
  5. how to click a jbutton and open an url
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 11: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
  •