Issue
I have been trying to open a htmlfile
on clicking help button. And i need to import the file to source package so after build it wont show errors. I am using netbeans
. When i copied the file to src
and tried running the file it showed error in compiling. I want to know how to add the file to src
and how to open by asking the user with a list of browsers installed in the system. Here is the code i have searched and tried..Thanks in advance
try
{
File htmlFile = new File(this.getClass().getResource("help.html").getFile());
Desktop.getDesktop().browse(htmlFile.toURI());
}
catch (IOException ex)
{
System.out.println(ex);
}
Solution
Try this one.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FileOpenBrowser {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("FileOpenBrowser"); // set API Name
JPanel topPanel = new JPanel(new FlowLayout()); // set Panel Layout
Button btn = new Button("Help");
btn.setBounds(50, 100, 60, 30);
topPanel.add(btn);
frame.add(topPanel, BorderLayout.PAGE_START);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
System.out.println("Start..");
File file = new java.io.File("src/help.html").getAbsoluteFile();
Desktop.getDesktop().open(file);
System.out.println("End..");
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
}
Answered By - Hariharan G R
Answer Checked By - Robin (JavaFixing Admin)