Attempting to have a button in my Java GUI open a PDF. My PDF is in the src folder, the same folder where my code is but I get a 'file not found' error and am unsure what is wrong with my pathing.
aboutButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (Desktop.isDesktopSupported()) {
try {
File aboutPDF = new File("./aboutGUI.pdf");
Desktop.getDesktop().open(aboutPDF);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
});
You'll have to put it in your resources folder as that is where your IDE looks by default for files unless specified otherwise. Then do "filename.pdf" or "src/main/resources/filename.pdf"
Related
I must check downloaded PDF and open it using Selenium. For that, I am using Robot class. This is not the permanent or we can say general solution of this.
Question : Can anyone please help and provide more reliable solution for the same ?
Please find below code:
public boolean CommonEvents(WebDriver driver) throws InterruptedException {
try {
Thread.sleep(2000);
Robot robot = new Robot();
robot.mouseMove(100, 700);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(10000);
} catch(Exception e) {
BaseTest.reportPass(driver, null, "Should click on PDF to open", "Failed to click on PDF to open");
}
}
Just in case you really need to open every downloaded PDF then I would simply add this line to your preferences (e.g. for Firefox):
ffprofile.setPreference("browser.helperApps.neverAsk.openFile", "application/pdf");
Then it will automatically open the downloaded file after finishing the download.
You can use something similar to this if you have a fixed directory where you are saving the downloaded pdf files .
public static void main(String[] args) {
try {
File pdfFile = new File("c:\\Hello.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported.");
}
} else {
System.out.println("File doesn't exists.");
}
System.out.println("File opened.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
Reference : https://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html
We use below logic:
First set preference for a download location, so that the file will be downloaded to your desired location.
chromePrefs.put("download.default_directory", downloadFilepath);
Set Preference so that it won't ask the pop to download.
chromePrefs.put("profile.default_content_settings.popups", 0);
As #Mudit_ has answered check for the *.pdf with regex pattern, if you want to make it dynamic.
I'm making plugin for eclipse which opens frame with some table's when plugin command is activated. Now I want to add help file to plugin's frame, so that when clicked on help file's link in frame, file opens (executes). File is suppose to be part of plugin. My problems are:
Don't know how to make link and add it to frame.
Don't know how to locate that file in plugin from run time application.
JLabel lblFileLink = new JLabel("Help");
lblFileLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
lblFileLink.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
try {
/* Add code for opening file from plugin.*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
Found this code somewhere, now I need to implement link, any thoughts?
If i understand you question correct, something like this should work:
JLabel lblFileLink = new JLabel("Help");
lblFileLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
lblFileLink.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
try {
java.awt.Desktop.getDesktop().edit(INSERTYOURFILEHERE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
This will open the standard text editor and show your file. Just replace INSERTYOURFILEHERE with your own text file.
Edit: If you want to open it in Eclipse maybe look at this
Edit2: The gist of the link above:
File fileToOpen = new File("externalfile.xml");
if (fileToOpen.exists() && fileToOpen.isFile()) {
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditorOnFileStore( page, fileStore );
} catch ( PartInitException e ) {
//Put your exception handler here if you wish to
}
} else {
//Do something if the file does not exist
}
I would like to open file specified by its path in NetBeans editor (IOProvider.getDefault().getIO(...);).
I would like the same functionality as is when some Java/C/C++ or any other programming language prints an Exception. As far as I went for now:
Write the output in console (see the example at the end)
By using OutputListener resolve what should be printed as hypertext
OutputListener.outputLineAction that defines what to do when clicked on hypertext IOColorPrint.print(InputOutput io, CharSequence text, OutputListener listener, boolean important, Color color)
Open the file on the system when clicking on
An example of error message I need to resolve:
The export was successful. The exported file can be found in: C:\Users\MY_USER\Desktop\myFile.xml
The problem that I have is that I have to print all the output in one line and the OutputEvent gives me all the line. Is there any way to get only the Highlited text (The path) ?
This call open new console output tab:
IOProvider.getDefault().getIO(...)
You should take inputStream and use class while(x=is.read()!=n ....
IOProvider.getDefault().getIO(...).getInputStream
Let me know, if this was usefull.
Here the Listener :
public class HyperlinkToFileOutputListener implements OutputListener {
private final File file;
public HyperlinkToFileOutputListener(File file) {
this.file = file;
}
#Override
public void outputLineSelected(OutputEvent oe) {
}
#Override
public void outputLineAction(OutputEvent oe) {
try {
if (file.exists()) {
Desktop.getDesktop().open(file);
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
#Override
public void outputLineCleared(OutputEvent oe) {
}
}
here the call
IOColorPrint.print(io, file.getName(), new HyperlinkToFileOutputListener(file), true, Color.BLUE);
best regards
Hey Guys I did a little App, where I type into a textbox a specific value (height, weight) and save it into a file.
I did this but I do not know, which path I have to use for Android.
Hope you can help :)
public void SaveList(View view) {
//Pf`enter code here`ad, im privaten Speicherbereich
File file = new File("I need this path :)");
try {
OutputStreamWriter fdg = new OutputStreamWriter(new FileOutputStream(file));
fdg.write(""+this.weight);
fdg.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can use Environment.getDataDirectory() to get the root directory, if you dont have an SD card.
If you have an SD card, use Environment.getExternalStorageState()
Read more about them in the docs
Thus, change your code as follows
File file = new File(Environment.getDataDirectory()+"/your_folder_name/your_file_name");
This will create a file with name your_file_name in the folder your_folder_name in your internal storage.
My japplet is able to run normally in NetBeans environment, but shows only a gray block screen in my browser (Chrome, IE). But when I removed all the image icons from my japplet, it runs normally in the browser and I have totally no idea why. Need some guidance for this.
My japplet class content
public void init()
{
try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }
catch(Exception ex) { System.out.println(ex); }
try
{
java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } });
}
catch (Exception ex) { ex.printStackTrace(); }
jlbReg.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon1.png")));
jlbAnn.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon2.png")));
jlbSubmit.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon3.png")));
jlbForum.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon4.png")));
}
My html content
<APPLET codebase="classes" code="MyJApplet.class" width=1150 height=1000></APPLET>
Your applet can't find the images in the jar file. Wait a minute - that's because you're not using a jar file. You need to use a jar file.