This question already has answers here:
JasperViewer - How to set file name and set the extension (format)?
(2 answers)
Closed 6 years ago.
I am creating Java desktop application. I am using DynamicReports API to create report.
The report was viewed in a JasperViewer. When I click save button, I want to display a name for report to save by default. How to set name for the report by default?
it is necessary to import the package com.jasperassistant.designer.viewer.ViewerComposite
so on that viewer composite you can see a save icon on you left top corner. you can provide any name and location where you want to save. i think that the good way of saving report through your jasper report.
Then i think that you have to change in JFileChooser instead of looking into API.
You can give default name to be displayed.
This way the approach would be:
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setSelectedFile(new File("fileToSave.txt"));
jFileChooser.showSaveDialog(parent);
And If you pass a File with an absolute path, JFileChooser will try to position itself in that directory (if it exists).
I got this source from following place
Related
I'm trying to set a JFileChooser to only allow choosing a specific file type (pdf) via the showOpenDialog.
I've set a File Filter but I'm confused as to what action on the JFileChooser it has.
What I'm trying to achieve is:
Visually exclude other file types to prevent the user from choosing them from the list.
Actually prevent selection of other types or an invalid file. (i.e. have the getSelectedFile() to actually return a valid pdf file)
Here is my code:
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
fc.setDialogTitle("Load MSDS");
int op = fc.showOpenDialog(this);
if(op == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
lbl_msds_loaded.setForeground(Color.BLACK);
lbl_msds_loaded.setText(f.getName() + " (Size: " + utils.FileUtils.getFileSizeMegaBytes(f, 3) + ")");
}
I get this behavior:
Visually - The filtering works and the dialog does only show PDF Files, therefore I can only choose pdf files from the list.
But - I'm still able to manually select an invalid file, by typing in some name in the 'File name:' field and click open (or hit enter).
For example: if I write Untitled.png (which does exist in the currently opened directory) and open, I will get that png file loaded.
Or if write a file name that doesn't exist and click open, I will actually get a new file with that name loaded.
(By loaded I mean the file that getSelectedFile() will return).
Is there a way to not allow the dialog approve the open action if an invalid file is set (based on the filter ofcourse)?
Shouldn't this already be the case when using JFileChooser Dialogs with filters?
What exactly is the filter doing here? The documentation for JFileChooser does not explain any of these aspects.
I would really appreciate an explanation on how this works.
Also what is the difference between setFileFilter and addChoosableFileFilter? They give the exact same behavior.
Finally here's a few screenshots of the Dialog and the JFrame form I'm working on for some context:
https://ibb.co/bFVqVmt
https://ibb.co/5BcsXSW
https://ibb.co/2qq0qr9
https://ibb.co/jMXXXyN
https://ibb.co/g3kvtfd
https://ibb.co/2FshJpt
Thanks alot!
How can I get the icon/image associated with an file extension in Java?
This should do the trick:
Icon ico = javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( file )
To show it on a label, just add it to a JLabel:
myJpanel.add(new JLabel(ico));
I am working with .pdf files that are available on my companies' website only. I am not aware of any way to download them and store in one folder.
The link that I click to get the .pdf file has the following source code:
<a href="javascript:propertiesView('documentName')">
As I click on the link, a .pdf file pops up in a new browser window with no url and no source code. I presume that there is no way to manipulate that .pdf directly, then how can I save it then in order to manipulate the .pdfs from a folder?
Thank You
You may have luck by simply telling your browser to always save PDF files to disk (credits to Dirk):
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
If that doesn't work, you are probably able to iterate through all open windows/tabs by using the switchTo() methods. Try something like this to get some insight about your opened windows (credits to Prashant Shukla):
public void getWindows() {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
System.out.println(driver.getTitle());
}
}
A non-selenium solution to download the file would be to use the apache-commons library (creadits to Shengyuan Lu):
org.apache.commons.io.FileUtils.copyURLToFile(URL, File)
But this would require that you know the URL of the window, which you probably are able to fetch with the second approach i mentioned (driver.switchTo()) and driver.getCurrentUrl().
I presume that there is no way to manipulate that .pdf directly
That's correct. With Selenium, you cannot.
how can I save it then in order to manipulate the .pdfs from a folder?
I've actually implemented this exact thing in our regression system where I work.
My first step, was to construct a Url based on what the propertiesView(). method did.
in your case, propertiesView() does some sort of window.open is my guess. So your goal is, to extract that Url that it opens, and use concatenation to construct the url.
Once you've found your Url, the rest is a cakewalk. Just download the url to a folder named /pdfs. See this question for how to do that.
It may even require calling that method to figure it out.. Due to my ignorance of your System Under Test, it's difficult for me to give you a code answer, unless you posted it.
A hint that i'll tell you, is if you are using Selenium 1, use
String url =selenium.getEval("var url = something; url;");
to fetch the url and get it into a java object.
(If using selenium 2, use the JavaScriptExecutor#executeScript)
If you want to save a PDF to your hard drive in IE with selenium, you need to use pywinauto with selenium. I just used this code for PDF files that open up in the browser.
//selenium imports
from pywinauto import application //pywinauto import
//write selenium code to open up pdf in the browser
driver = webdriver.Ie("IEDriverServer.exe", capabilities = caps)
//this could be a get or driver.execute_script() to click a link
driver.get("link to pdf")
//save pdf
app = application.Application()
//get the ie window by the title of the application (assuming only one window is open here)
ie = app.window_(title_re = ".*Internet Explorer.*")
//this line focuses on the pdf that is open in the browser
static = ie.Static
//focus on the pdf so we can access the internal controls
static.SetFocus()
//control + h shows the pdf bar, but you don't really need this step
//for it to work. i just used it as a debug
static.TypeKeys("^H")
//open save file dialog
static.TypeKeys("+^S")
//tricky here because the save file dialog opens up as another app instance
//which is how pywinauto sees it
app2 = application.Application()
//bind to the window by title - name of the dialog
save = app2.window_(title_re = ".*Save As.*")
//this is the name of the property where you type in the filename
//way to be undescriptive microsoft
file_name = save[u'FloatNotifySink']
//type in the file name
save.TypeKeys("hello")
//pause for a second - you don't have to do this
time.sleep(4)
//find and bind the save button
button = save[u'&SaveButton']
//click the save button
button.Click()
This question already has answers here:
JFileChooser, want to lock it to one directory
(3 answers)
Closed 9 years ago.
I have a JFileChooser that opens in a specific directory and then allows the user to choose a directory within it (when selected w/ single-click and the OK button is pressed).
However, when the directory is double-clicked, the file chooser opens that directory instead of choosing it.
How can I either
override the double-click to choose the directory
disable navigation outside of the initial directory
disable double-clicking?
I've tried overriding the isTraversable() method in FileView and FileSystemView which works to restrict the file chooser to a directory, however, it then does not show any items inside of said directory.
Here's the code I have right now:
JFileChooser fc = new JFileChooser(dir);
fc.setApproveButtonText("OK");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setMultiSelectionEnabled(false);
fc.showOpenDialog(fileChooserDialog);
File file = fc.getSelectedFile();
if (file.getParent().equals(dir)) {
//do something
}
You could modify the action map. I don't have a access to a compiler ATM so I can't test this out, but it should work .
JFileChooser chooser = new JFileChooser(".");
ActionMap am = chooser.getActionMap();
Action key = am.get("WHATEVER_THEACTIONAME_FOR_OPEN-DIR._IS") //I think it's "Open Folder";
key.setEnabled(false);
I will update this answer later when I have time and access to a compiler.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
I need to display the file name without the extension in JFileChooser(open mode). How?
Can anyone tell me how switch off extensions in view openDialog ?
I want see only name file without his extension in this view.
thanks
you can use a custom writen javax.swing.filechooser.FileView that you set in your filechooser
The Customizing the File View section in the Java Tutorial includes an example of defining your own FileView object to control how the files are displayed in a JFileChooser.
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
Override the FileView.getName() method to display a customized name for each file.
Download the FileChooserDemo2 demo from this web page to try the example and change method ImageFileView.getName() to return the filename with the file extension removed.