0

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.

Jud
  • 1,324
  • 3
  • 24
  • 47
  • you have to read the contens of the txt file, save it to a String and then output the String – alexandre1985 Mar 21 '15 at 18:49
  • 1
    @alexandre1985 what if the file is not a txt file? Is there no way to actually open a file using JButtons? – Jud Mar 21 '15 at 18:50

2 Answers2

2
Desktop.getDesktop().open(File file);

This might help you:

Related Question and Answer.

Community
  • 1
  • 1
  • *"Related Question and Answer."* Then you should have voted to close the question as a duplicate and commented the fact – MadProgrammer Mar 21 '15 at 20:19
1
Desktop.getDesktop().open(new java.io.File("path to file"));

Just a suggestion: Learn C\C++ for these kind of jobs. I did.

nom
  • 256
  • 3
  • 16