Spring/Virgo - java swing: JDialog starts behind other windows - java

I have a service running on Virgo that occasionally needs to prompt the user and get some feedback.
I've created a class that extends the JDialog class:
public class PPNDialog extends JDialog implements ActionListener, WindowListener{
public PPNDialog(JFrame frame, String title){
super(frame, title, true);
... //rest of dialog initialisation code
}
... //(other methods
}
And another class that creates a new JFrame and popups up the dialog:
public class GUIStarter {
JFrame frame;
public GUIStarter(){
this.initialise();
dialog = new PPNDialog(this.frame, "");
dialog.setVisible(true);
}
private void initialise() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
Everything works out ok except when the JDialog pops up, it starts in the background behind all my other windows (other apps). The problem is that since the JDialog doesn't create a taskbar entry, the dialog goes unnoticed unless all the existing windows are minimised.
I understand that this happens because the GUI is started from virgo but I was wondering if it's possible to bring it forward so it starts in the foreground. I noticed that the same thing happens when I use JOptionPane. All JOptionPane messages appear in the background. That's only when the GUI is started from a virgo service. I don't have that issue if I run the GUI as a standalone application.

Try using: setAlwaysOnTop(true);
In your case:
dialog.setAlwaysOnTop(true);

Related

Loading JFrame blank when thread processing

I want to show a "Please wait" JFrame while my program copies some files.
Context : I have a first JFrame that allows a user to pick files and then clicks on a button to trigger the copy. This is when I want to display the "Please wait" JFrame and make it disappears when everything is done.
I have succeeded to make this JFrame appeared and disappeared but it is always blank.
The code for the "Please Wait" JFrame :
public class Loader implements Runnable {
JFrame loadingFrame = new JFrame();
#Override
public void run() {
// Parameters for size and position
loadingFrame.setSize(180,100);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Please wait ...", SwingConstants.CENTER), "Center");
loadingFrame.add(panel);
loadingFrame.setLocationRelativeTo(null);
loadingFrame.setVisible(true);
//((JComponent) loadingFrame.getContentPane()).revalidate();
//loadingFrame.repaint();
}
public void destroy() {
loadingFrame.setVisible(false);
loadingFrame.dispose();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Loader());
}
}
As you can see, I tried "repaint()" and "revalidate()" but it didn't work.
I call the Loader class in this function :
public void analyzeAndCopy(){
// Show loading Frame
Loader loader = new Loader();
loader.run();
[... copy code...]
// Hide loading Frame
loader.destroy();
}
The Please Wait JFrame appears blank, and stays (as wanted) while files are being copied and disappears after, but it stays blank all the way. :/
I think it may be related to Thread, I tried to create a 2nd thread to call the JFrame, it didn't work. I'm running out of ideas...
Any help will be much appreciated ! :)
Thanks
try to replace
loader.run();
with
new Thread(loader).start();
and add a line to method run:
loadingFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
and you can use JDialog instead of JFrame

JFrame.setAlwaysOnTop is not working

I have created an SSCCE to show my problem
public class TopTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(512, 512);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This program should create a window that is always on top, however if I run this and then click on another window behind it, the JFrame is sent behind it.
I think the problem is that you have another window that is always on top.So when you click on that this windows loose the focus and goes back.
One easy way if it is a task which needs this window to be on top and Focusable is to frequently check with a Thread maybe:
if(!frame.isFocused()) frame.setFocused(true);
Just something to help....

Modal dialog not always on top of an undecorated JFrame when another JFrame is visible

I have a weird problem with modal dialogs and undecorated JFrame.
If I create a main undecorated JFrame then I display a modal dialog thanks to the JOptionPane, everything goes well. The modal dialog stays always on top and I can't click on the main fame.
But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.
This problem doesn't happen:
if the main frame is decorated
or if the second frame is not visible
EDIT
I use jdk1.7.0.0_09 on Linux Suse.But I have the same result with jre 1.6.0_32
The code I used to test:
public static void main(String[] args) {
// creates main frame and set visible to true
final JFrame mainFrame = new JFrame();
mainFrame.setUndecorated(true); // if I comment this line, everything goes well
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(500, 500));
mainFrame.setVisible(true);
// creates a dialog and set visible to true
JFrame anotherFrame = new JFrame();
anotherFrame.setVisible(true); // or if I comment this line, everything goes well too
// display a modal dialog
JOptionPane.showMessageDialog(mainFrame, "A message");
}
But, if create another JFrame (or another JDialog), the modal dialog
still prevent me to interact with the main frame, but now the modal
dialog is not always on top and go underneath the main frame when I
click on it.
not true at all, both are not accesible untill JOptioPane is visible
JOptionPane or JDialod.setModal(true) block mouse or key events to the alll windows invoked from currnet JVM
there must be something else that isn't clear from your question, rest of code, minor could be Java version and Native OS
code for Java6 (winxp), works for me on Win7 / Java7(x.x_011)
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main {
private JFrame mainFrame = new JFrame();
private JFrame anotherFrame = new JFrame();
public Main() {
mainFrame.setUndecorated(true);
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(100, 60));
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
anotherFrame.setVisible(true);
anotherFrame.setLocation(110, 0);
anotherFrame.setSize(new Dimension(100, 60));
anotherFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(mainFrame, "A message");
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Main main = new Main();
}
});
}
}

Change JPanel contents in JDialog

I'm trying to add components to a JDialog after it has been created and displayed. Nothing I try makes the changes actually update to the screen, and I've read and applied every question I could find related to this.
This example code creates a modal JDialog showing the word "test". I cannot get it to display "test2". Almost exactly the same code but with a JFrame instead of a JDialog behaves as I expect, so I don't understand. I'm new to Java and especially to swing.
import javax.swing.*;
public class DialogTester {
public static void main(String[] args) {
new DialogTester();
}
public DialogTester() {
JFrame jframe = new JFrame();
jframe.setVisible(true);
JDialog jdialog = new JDialog(jframe,true);
JPanel jpanel = new JPanel();
jpanel.add(new JLabel("test"));
jdialog.add(jpanel);
jdialog.setVisible(true);
jpanel.add(new JLabel("test2"));
jpanel.revalidate();
jdialog.getContentPane().validate();
jdialog.pack();
}
}
I also tried calling
jdialog.repaint();
which did nothing.
You created a modal dialog. So, as soon as you call setVisible(true), the following instructions wait for the dialog to be closed to be executed.
Put the code adding a label before the dialog is made visible, or put it in an event handler called after the dialog is shown, for example, when you click on a button in this dialog.

Java - Swing GUI Window Order

In the following code, why is the "My Application" window the window at the front? The main method constructs this window first, right? So shouldn't it be at the back when the "My Window" JFrame is produced?
public class MyApp extends JFrame {
public MyApp() {
super();
setSize(300,600);
setTitle("My Application");
setVisible(true);
}
public static void main(String[] args) {
MyApp application = new MyApp();
JFrame window = new JFrame();
window.setSize(600,300);
window.setTitle("My Window");
window.setVisible(true);
}
}
This is simply undefined behavior; there's no guarantee that either window will always be in front. When I ran your code (Mac OS X, Java 6), "My Window" came up in front, but I have no doubt you're correctly describing what happens on your system.
1) you can't move one JFrame toFront() over another JFrame
2) don't create more than one JFrame, there are another issues with this Top-Level Container as toFront, toBack
3) you have look at JDialog or JWindow as another Window
with parent to the JFrame
with setModal if required
with ModalityTypes is required

Categories

Resources