Stop creating a new dialog
I'm trying to create a Java Swing dialog, but I don't want the dialog to focus on what is currently focused. For example, if you are editing the word doc and another application creates a dialog, you should see the dialog, but the word doc must remain in focus so that you can continue editing.
I have tested the following code on Mac OSX and Ubuntu 12.04. On Mac, the code creates dialog boxes that don't take focus as I want. On Ubuntu, dialogs take focus and I cannot enter them while they are being created.
I really hope this is not the case for the operating system. Is there a way to achieve what I am trying to do on all platforms (or at least Ubuntu)?
import javax.swing.*;
import java.awt.Color;
import java.util.Random;
public class Focus {
private static Random r = new Random();
public static void main(String[] args) {
while(true) {
try {
Thread.sleep(500);
createObject(r.nextInt(1000), r.nextInt(600));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void createObject(int x, int y) {
JDialog testDialog = new JDialog();
testDialog.setSize(50, 50);
testDialog.setLocation(x, y);
testDialog.setAlwaysOnTop(true);
testDialog.setUndecorated(true);
testDialog.setFocusable(false);
testDialog.setEnabled(false);
testDialog.setVisible(true);
}
}
+3
source to share