How to open a file using a JButton

Jaken Herman

I have a file, we can call it 'example.txt' and I've already called the file in my program. I want to add functionality to open the file when a JButton, myButton is clicked. This is my code so far:

myButton.setText("Button");
 File example_file = new File("~/path/to/example.txt");
 myButton.addActionListener(new java.awt.event ActionListener(){
   public void actionPerformed (java.awt.event.ActionEvent evt){
     myButtonActionPerformed(evt);
   }
 });

private void myButtonActionPerformed(java.awt.event.ActionEvent evt){
   //open the file
}

I'm sure there is a very simple way to do this, but I haven't been able to find anything.

Benjamin Winters
Desktop.getDesktop().open(File file);

This might help you:

Related Question and Answer.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to open a file using Excel VBA and turn off updating links

分類Dev

How to open an excel file with write protection using C#

分類Dev

how to open video file in code blocks using opencv

分類Dev

How to open file in C program?

分類Dev

How to open a .txt file in Django?

分類Dev

How to open file in Qt application?

分類Dev

How to know if an image file is open

分類Dev

How to read file permission bits using only the open and read system calls?

分類Dev

Python: open either zipped or unzipped file using "with"

分類Dev

Reading .img file in C using open()

分類Dev

Can not open PDF file from using WebView

分類Dev

Using Javascript to open a local MS Access file

分類Dev

How to open templates using vim?

分類Dev

How to open a file handle on a string in Perl 6?

分類Dev

How to open and run an exported JAR file

分類Dev

How to get value of variable from open file

分類Dev

How to open a file only for appending in python 3

分類Dev

How to open a file in binmode with perl oneliner?

分類Dev

How to open a file for editing in Administrator mode?

分類Dev

How to create search tab and open the .txt file?

分類Dev

how to open an exe file from google chrome

分類Dev

How can I use variables in a picture's file name and loops to open multiple pictures using pygame without cluttering the code

分類Dev

How to simply keep a file in open status without help of program in C language/etc.. by using windows bare basic features?

分類Dev

How to avoid Multiple Jbutton selections

分類Dev

How can i open an .png file with an .sh file?

分類Dev

How to open activity using Voice Interaction?

分類Dev

Is it possible to open an Access 2010 database file without using Wine or VirtualBox?

分類Dev

Trying to open a php file in background using ajax on link click

分類Dev

unable to open database file in sqlite (using finisar.sqlite)

Related 関連記事

  1. 1

    How to open a file using Excel VBA and turn off updating links

  2. 2

    How to open an excel file with write protection using C#

  3. 3

    how to open video file in code blocks using opencv

  4. 4

    How to open file in C program?

  5. 5

    How to open a .txt file in Django?

  6. 6

    How to open file in Qt application?

  7. 7

    How to know if an image file is open

  8. 8

    How to read file permission bits using only the open and read system calls?

  9. 9

    Python: open either zipped or unzipped file using "with"

  10. 10

    Reading .img file in C using open()

  11. 11

    Can not open PDF file from using WebView

  12. 12

    Using Javascript to open a local MS Access file

  13. 13

    How to open templates using vim?

  14. 14

    How to open a file handle on a string in Perl 6?

  15. 15

    How to open and run an exported JAR file

  16. 16

    How to get value of variable from open file

  17. 17

    How to open a file only for appending in python 3

  18. 18

    How to open a file in binmode with perl oneliner?

  19. 19

    How to open a file for editing in Administrator mode?

  20. 20

    How to create search tab and open the .txt file?

  21. 21

    how to open an exe file from google chrome

  22. 22

    How can I use variables in a picture's file name and loops to open multiple pictures using pygame without cluttering the code

  23. 23

    How to simply keep a file in open status without help of program in C language/etc.. by using windows bare basic features?

  24. 24

    How to avoid Multiple Jbutton selections

  25. 25

    How can i open an .png file with an .sh file?

  26. 26

    How to open activity using Voice Interaction?

  27. 27

    Is it possible to open an Access 2010 database file without using Wine or VirtualBox?

  28. 28

    Trying to open a php file in background using ajax on link click

  29. 29

    unable to open database file in sqlite (using finisar.sqlite)

ホットタグ

アーカイブ