I am working on Jasper Report. I need to ask from a user where to save the generated report. For that, I need to open a "Save As" dialog box. I tried it using JFileChooser and FileDialog.
But, during execution of my code, when execution reaches the point where the code for the Save As dialog box is written, the code remains stuck there. One thing I noticed is that if you run the JFileChooser and FileDialog code for an open dialog box in a separate java class with its own PSVM, it works well.
But when it is invoked by some other function, execution remain stuck there.
Is there any plugin or jar I need to add to use JFileChooser and FileDialog? Or something else I am missing?
I am using eclipse Java EE kepler and Spring MVC.
A FileDialog is easily handled like this:
public String getFileFromDialog(){
FileDialog dialog=new FileDialog(yourFrame,"Save as",FileDialog.SAVE);
dialog.setVisible(true);
//When you call setVisible(true), the method blocks until the user
//chooses a file or cancels the dialog
return dialog.getDirectory()+dialog.getFile();
}
Be aware that, if the user closes the dialog without any file chosen the method will return the rather weird String "nullnull" as both getDirectory() and getFile() return the String "null" (not null but "null"). You probably have to check this to make sure the user has actually chosen a file.
JFrame frame= new JFrame();
frame.setAlwaysOnTop(true);
frame.setVisible(true);
FileDialog fd = new FileDialog(frame, "Save as", FileDialog.SAVE);
fd.setAlwaysOnTop(true);
fd.setVisible(true);
String path=fd.getDirectory().toString()+fd.getFile().toString();
input=cl.getResourceAsStream(total_employee_record);
report=JasperCompileManager.compileReport(input);
parameters.put("mypic",inputimage);
print=JasperFillManager.fillReport(report, parameters,new JRBeanCollectionDataSource(beanlist,false));
JasperExportManager.exportReportToPdfFile(print,path);
}
..........
This is my service method. Now thing that happened is save as dialog box is opened,but in the background. Means everytime I have to minimize eclipse to set the path.
Get me solution please.
My apologies for the bad formating.
Related
I am trying to set the default close operation in NetBeans 8.0.2 (in Ubuntu 14.04 on an older Asus gaming laptop.) My program is very large but uses no JFrame or java.swing components.
I merely need to save some values when the "x" in the lower right corner is clicked (this is one usual way to stop execution of a java program in NetBeans.)
I found suggestions that involved swing & JFrame, but it wasn't clear just where to insert the code:
DefaultApplicationView view = new DefaultApplicationView(this);
javax.swing.JFrame frame = view.getFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.out.println("CLOSING");
}
}
show(view);
I also found a set of instructions that I think I would prefer to use, but the post is old enough that my NetBeans doesn't have the tabs/menu-items referred to:
Set Window to Design Mode by clicking the 'Design' Tab
In the Navigator: Right click the 'JFrame' -> 'Properties'
In the Properties Tab: Set 'defaultCloseOperation' (top of the list) to 'DO_NOTHING'
Select 'Events' Tab
Scroll down to 'windowClosing'
Click on the "..." button on the right of the event to bring up the custom editor
Click 'Add...' and name the handler (i.e. custom function that you want to have execute on click of the 'X', or window close event).
Click 'Ok'
Netbeans now automatically creates the function and takes to you the function body in the source view
Now simply add what you want to do here: eg. dispose(), or system.exit or pintln(), or whatever your heart desires, as long as its JAVA and makes sense to the app.
Then there are a few other possibly relevant posts, but they all explicitly involve JFrame and/or swing. (Am I ignorant of some fact such as "All NetBeans java applications use JFrame", or some such?)
A pared down example of code for what I'm trying to do would be:
public class MyApp{
public static void main(String[] args){
loadMyVariables();
// do some work that changes variables' values
// during this work user clicks the 'x' box to halt execution
// I need then automatically to save the variables' new values
}
// needs to be called by the OS or GUI when execution is halted by user
public static void saveMyVariables{
// here the usual printStream stuff saves some values to a file
System.exit(0);
}
public static void loadMyVariables{
// here the usual Scanner stuff reads some values from a file
}
}
(I need help setting the tags for this, so I'm doing as instructed and asking the community.)
THANKS
I have a program that runs in the CLI, but uses a JOptionPane in order to display a popup menu to alert the user of an event. I have noticed that when the showMessageDialog method is called, the resulting popup menu does not focus automatically.
Is there a way to request focus for the popup dialog box? I have tried calling requestFocus on the JOptionPane, but to no avail.
Here is some sample code.
System.out.println("Backing up...");
//backUp();
Component frame = null;
JOptionPane jop = new JOptionPane();
jop.showMessageDialog(frame, "Backup complete. Ready for encryption.");
I apologize if this is a repost, but I haven't been able to find a working answer so far.
Create a JDialog and make a call to setAlwaysOnTop passing true.
JOptionPane jop = new JOptionPane();
JDialog dlog = jop.createDialog(null, "Backup complete. Ready for encryption.");
dlog.setAlwaysOnTop(true);//make JDialog on top of other windows
dlog.setVisible(true);
The above works on Windows, but note that this behavior may be platform dependent (see docs - you can check if this is supported via the isAlwaysOnTopSupported method)
I've wrote a simple application to store some text in a derby DB. I have 2 button each one creating a new inputDialog. My problem is that when I run the program on my Ubuntu PC all is well. When I run it on a windows 7 PC when the input dialog is displayed the whole thing is minimized and hidden from the user. So each time I want some input from the user he has to restore the application. And the other problem is that the program doesn't appear in the alt-tab menu too. Here is the code that I use to display the dialog:
String s = (String) JOptionPane.showInputDialog(this, "Моля въведете име:");
All help will be greatly appreciated.
I tried the following code - directly from main() via eclipse running on Windows 7 64-bit. The JFrame remains on display, even if I try otherwise.
JFrame f = new JFrame();
f.setSize(750, 500);
f.show();
JOptionPane.showInputDialog(f, "hello", "there");
System.out.println("hi");
Try this, and if you get the same result then at least we know it's a windows issue that we're dealing with rather than a Java issue.
EDIT:
After looking through your code, I found the offending line. Also as a side note, you should generally call setVisible() after you have done configuring your window. This is especially true with my code, as it would throw an exception if you try to call setUndecorated() after you have displayed the window.
Your Code:
this.setVisible(true); //This should be called after you finish configuration
device.setFullScreenWindow(this); //This is the problem!!!
Instead you should use:
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);
If you want to have your window fullscreen then use:
this.setUndecorated(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);
I have an installer program that lets the user choose a directory in which to install. The JFileChooser implementation on MacOS uses a native dialog (or at least it looks native). That's great.
The only problem is there's no way to create a directory from this dialog ... you can only choose a pre-existing one, which is clunky. Is there a way to get this functionality?
I use the JFileChooser in "select directories only" mode. Thus it isn't the same dialog as the usual MacOS file picker which does have that functionality.
That is correct that the showOpenDialog method will not give you an option to create new folders. This is a usability thing as it does not really make sense to open something that does not exist. If you use the showSaveDialog there will be a button 'Make new Folder' or similar to that.
public static void main(String[] args) {
JFrame frame = new JFrame();
FileDialog d = new FileDialog(frame);
d.setVisible(true);
}
I have problem currently for my swing reminder application, which able to minimize to tray on close. My problem here is, I need JOptionPane dialog to pop up on time according to what I set, but problem here is, when I minimize it, the dialog will pop up, but not in the top of windows when other application like explorer, firefox is running, anyone know how to pop up the dialog box on top of windows no matter what application is running?
Create an empty respectively dummy JFrame, set it always on top and use it as the component for the JOptionPane instead of null. So the JOptionPane remains always on top over all other windows of an application. You can also determine where the JOptionPane appears on screen with the location of the dummy JFrame.
JFrame frmOpt; //dummy JFrame
private void question() {
if (frmOpt == null) {
frmOpt = new JFrame();
}
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
String[] options = {"delete", "hide", "break"};
int response = JOptionPane.showOptionDialog(frmOpt, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "delete");
if (response == JOptionPane.YES_OPTION) {
removeRow();
}
frmOpt.dispose();
}
Old post, but I was struggling with this.
My problem was more with Javafx allowing the JOptionPane to go behind the current Java window.
Therefore I used the following which does what the original poster asked by putting the JOptionPane in front of all windows; even JAVAFX.
Firstly the old JOptionPane:
JOptionPane.showMessageDialog(null, "Here I am");
Now an JOptionPane that stays in front:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Here I am");
And for fun here is everything in one long line:
JOptionPane.showMessageDialog(
((Supplier<JDialog>) () -> {final JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); return dialog;}).get()
, "Here I am");
You can make a static method some where that will return the JDialog for you and then just call it in the JOptionPane to clean up your code a bit.
Are you using one of the canned JOptionPanes? (Like JOptionPane.showCOnfirmDialog(...))
You may want to look at extending JDialog and making your own dialog panel, and then calling myDialog.setAlwaysOnTop(true);
Windows is blocking this operation since XP.
The scenario before was like:
Your a tiping in some text in an editor and not recognize that another dialog is coming to front when you are tipping the text. The coming dialog gets the focus and you are tiping in the new dialog. Maybe you click enter after you are ready and do this in the wrong dialog, which is asking whether you realy want to delet your hard disk ;)
The come to front call in java is only working for java windows.
The possibibilty to notify the user of a new window is to implement a Frame, which will highlighted/flashing in the windows task bar.
Correction the post above..
I have resolve my problem as below:
this.setVisible(true); // show main frame
MyDialog dialog = New MyDialog(this, true); // show my custom dialog
dialog.setVisible(true);
this.setVisible(false);
it works fine for me :)
You might think about using a JFrame instead. It may give you a little more flexibility.
If you are using a JFrame and you want it to popup on top of the other windows use:
myFrame.setVisible(true);
myFrame.setState(Frame.NORMAL);
The setState will show the window to the user if it was in minimized state previously.