Results 1 to 4 of 4
Thread: open text document using JButton
- 05-03-2011, 11:35 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
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();... }
-
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... ... }
- 05-04-2011, 12:38 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
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?
- 05-04-2011, 01:02 AM #4
Ah then you would use java.awt.Desktop:
Java Code:if(Desktop.isDesktopSupported()) { Desktop desktop = Desktop.getDesktop(); desktop.open(file); }
Similar Threads
-
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 06:56 PM -
How To Open a JFrame Form from a jButton
By Lyricid in forum AWT / SwingReplies: 12Last Post: 03-28-2010, 07:53 AM -
Importing text into a word document
By Frys82 in forum AWT / SwingReplies: 5Last Post: 08-24-2009, 09:41 PM -
i want to open a document from my program
By akinpam in forum New To JavaReplies: 1Last Post: 12-21-2008, 07:09 PM -
how to click a jbutton and open an url
By katie in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 11:44 PM
Bookmarks