Minimizing the application in windows problem - java

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);

Related

Using JFileChosser or FileDialog to generate save dialog window

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.

jOptionPane dont show on top windows. Java 1.4

jOptionPane dont show on top windows.
I've read that using
JFrame frmOpt = new JFrame();
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
should be enough, but the problems is that I'm using Java 1.4 and 'setAlwaysOnTop' does not exists.
so....is there a way to solve this situation?
thanks in advance.
EDIT: Here is what I'm doing:
JFrame frmOpt = new JFrame();
frmOpt.setVisible(false);
response = JOptionPane.showOptionDialog(frmOpt,message,mens, 0,JOptionPane.OK_CANCEL_OPTION,null,options,null);
First I create a JFrame, then I create a new JOptionFrame setting the JFrame.
And it still shows at the back. Notice I do not use setAlwaysOnTop because of Java 1.4

Can't write to textbox in a Swing application executed from console via ssh

I made a swing application with a JFrame. But when I execute this application from the console with java -jar when I am logged via ssh, I cannot write in any of the textboxes. Everything that I write appears in the console rather than in the textbox. See the image attached to show what happens. How can I solve this? Thank you very much in advance.
The problem http://img833.imageshack.us/img833/8688/screenshotoftheproblem.jpg
Javier
I had this problem also but solved by using ssh -Y (instead of -X). Found on forums that some java applications requires trusted (-Y) ssh to work properly. Hope that help others.
I'm assuming you are dealing with unix like operative systems, even you don't mention it. What version of JVM are you using? Is the same version in your ssh server and client? I had a problem with OS X's JVM, I could not run GUI through ssh from a Linux client, but between same Linux flavour, there were no problems. You might want to add debug output to your ssh command line, through "-v" switch. I would recommend trying a very simple application, a trivial example: just a text box on a JFrame; to rule out any possible layout stack or listener issues.
Could you try these code, and see if you can modify the JTextField
public class SimplestGUI extends JFrame
{
public static void main(String [] args)
{
SimplestGUI window = new SimplestGUI();
window.start();
}
public SimplestGUI()
{
initGUI();
}
private void initGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(150,100);
JTextField textField = new JTextField();
textField.setText("123 probando");
getContentPane().add(textField);
}
public void start()
{
setVisible(true);
}
}
To compile and run use these command (assuming you have a JDK on your PATH):
javac SimplestGUI.java ; java -cp . SimplestGUI
If that works, then you should start adding your components, listener, adapters, etc. one by one, and see which one is causing the text fields not getting the input.
If doesn't work either, then my guess is that you might have a problem with different X Window versions or implementations.
¿Can you share your code to try it on another environment?

How to make full screen java applets?

I am designing a psychology experiment with java applets. I have to make my java applets full screen. What is the best way of doing this and how can I do this.
Since I haven't been using java applets for 3 years(The last time I've used it was for a course homework :) ) I have forgotten most of the concepts. I googled and found that link:
Dani web
But in the method described in above link you have to put a JFrame inside the applet which I have no idea how to do it.
Whatever I need a quick and dirty method b'cause I don't have much time and this is the reason why I asked it here.
Thanx in advance
The obvious answer is don't use applets. Write an application that uses a JFrame or JWindow as its top-level container. It's not a huge amount of work to convert an applet into an application. Applets are designed to be embedded in something else, usually a web page.
If you already have an applet and want to make it full screen, there's two quick and dirty hacks:
1). If you know the screen resolution, just set the applet parameters to be that size in the HTML and then run the browser in full screen mode.
2). Run the applet in appletviewer, rather than a web page, and maximise the appletviewer window.
Why not just open a new Frame from the applet (either from the "start()" method or, preferably, after the user presses an "open" button) and set it to be maximized?
JFrame frame = new JFrame();
//more initialization code here
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(dim.width, dim.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Don't forget: The JFrame should be created and opened from the EDT. Applet start() is not guaranteed to be called on that thread, so use SwingUtilities.invokeLater(). Of course, if you opt for the button route, button listener is called on the EDT, so you should be safe.
I think you want to use WebStart. You can deploy from a browser but it is otherwise a full blown application. There are a few browserish security restrictions, but, as you're using an Applet currently, I think I can assume they're not a problem.
I've found a solution for this problem that works fine. Tested in Linux 64 bits (Chrome and Firefox) and in Windows 7 64 bits (Chrome and Explorer)
The only problem has been that my applet uses all the space in the browser and when the user switch off the full screen mode, the applet is not scaled to the browser size. The solution has been to keep the previous size of the applet before to enter in a fullscreen mode and then, set this size when the applet returns to the normal mode:
public void setFullScreen() {
if (!this.fullscreen) {
size = this.getSize();
if (this.parent == null) {
this.parent = getParent();
}
this.frame = new Frame();
this.frame.setUndecorated(true);
this.frame.add(this);
this.frame.setVisible(true);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = ge.getScreenDevices();
devices[0].setFullScreenWindow(this.frame);
this.fullscreen = true;
} else {
if (this.parent != null) {
this.parent.add(this);
}
if (this.frame != null) {
this.frame.dispose();
}
this.fullscreen = false;
this.setSize(size);
this.revalidate();
}
this.requestFocus();
}

JOptionPane won't show its dialog on top of other windows

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.

Categories

Resources