Launch a file = launch program associated with given file and automatically open that file on start of the program.
Let's say I run IntelliJ IDEA, I run my code and main window (modal) of my program shows up. My program is in the foreground.
Then I launch a .pdf file (for now it means AcroReader will be executed) from my program. AA will show up in front of IntelliJ but behind my program.
Question
I would like AA (it is just an example here of course) to be shown in front of my program, not behind. How to do it?
Please note, this does not mean I would like to move my program to the background!
For launching files I use
java.awt.Desktop.getDesktop().open(new java.io.File(filepath));
My GUI is done in Swing.
Update 1
To rule out, any influence of custom widgets, events, and so on, I put simply JButton at the bottom of my window (JDialog) -- it is in Scala, but this piece is similar to Java syntax:
var dlg = new javax.swing.JDialog(null,"test",Dialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
var button = new JButton("Select Me");
var actionListener = new ActionListener() {
def actionPerformed( actionEvent : ActionEvent) = {
java.awt.Desktop.getDesktop().open(new java.io.File("test.pdf"))
}
};
button.addActionListener(actionListener);
dlg.add(button, BorderLayout.SOUTH);
dlg.setSize(300, 100);
dlg.setVisible(true);
Once clicked, AA is shown behind my app. Since it takes several seconds to run AA I also tried to click the button, and move the mouse away from my window. Exactly the same behaviour.
I also noted that AA is shown at the same relative position to my window, top left corner of AA is near bottom right corner of my app.
You may try something like this. On my machine (Ubuntu 10.4 LTS with Gnome2) it gives the evince (pdf-viewer) in the front, and if I close/hide evince - JDialog is placed back to the front.
On windows it may be very different, since actually without "dlg.toBack();" invocation behavior is the same.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class OpenFileTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
final JDialog dlg = new javax.swing.JDialog(null, "test", JDialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JButton button = new JButton("Select Me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
java.awt.Desktop.getDesktop().open(
new java.io.File("/home/user/Downloads/jfreechart-1.0.13-US.pdf"));
dlg.toBack();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
});
dlg.add(button);
dlg.setVisible(true);
}
});
}
}
Related
is it possible in java or other programing language to open 2 popup windows with one click and one of them to be closed automatically after a certain time?
Thank you in advance!
yes in thing in the programing can be done
i show the 2 popup menu and you can close one of them after time by using timer or thread and you can change their size by using size method
i am not going to write all code because you ask if you can open 2 popup windows with one click or not
i iust open the 2 popup windows with one click
package experiments;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CreateDialogFromOptionPane {
public static void main(final String[] args) {
final JFrame parent = new JFrame();
JButton button = new JButton();
button.setText("Click me to show dialog!");
parent.add(button);
parent.pack();
parent.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
new JFrame().setVisible(true);
new JFrame().setVisible(true);
}
});
}
}
A JButton has a different appearance when rolled over. That appearance is different from the "selected" appearance.
I want to display my button "as if" it was rolled over, so that user understands that if he hits the Return key, that button will be triggered.
The problem is not the same as setting the default button, because I am in a situation where I really want to get the user to understand that although he wouldn't expect it, if he hits enter that button will be activated. More details below for those who want some. Setting button as default would make button the default one, but wouldn't be significantly signaling to the user.
In my case the strong enough signal is the appearance that the button has when it is rolled over.
How to do that ?
More details on the situation, for those who want some :
I have a list of buttons representing options, and a text box at the top, which acts as a filter on the buttons
when filter is such that only one option remains, hitting return directly clicks that option's button
in reality user would have had to select the button with tab or arrow, and then hit enter.
since that shortcut is not obvious I want to signal it to user
Based on your question, what you "really" want, is the JRootPane#setDefaultButton, which will highlight the button, in a OS specific manner and if the user presses the default "action" key (Enter in most cases) will call it's ActionListener
For example...
The "normal" button is just a plain old JButton, the Hacked sets the rollOver to enabled and Default has been set as the default button for the JRootPane
As you can see, you're suggest fix does nothing on MacOS, don't know what it might do on other platforms
I suggest you have a look at How to Use Root Panes for more details
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton asDefault = new JButton("Default");
public TestPane() {
JButton hack = new JButton("Hacked");
hack.getModel().setRollover(true);
hack.setRolloverEnabled(true);
asDefault.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Defaulted");
}
});
add(new JButton("Normal"));
add(hack);
add(asDefault);
}
#Override
public void addNotify() {
super.addNotify();
JRootPane rootPane = SwingUtilities.getRootPane(this);
if (rootPane != null) {
rootPane.setDefaultButton(asDefault);
}
}
}
}
So using button.getModel().setRollover(true); doesn't work on all platforms and on those platforms it does work on, I suspect the user will simply need to move the mouse through it to return it to normal
button.getModel().setRollover(true);
So, I'm just working on a little game, which works quite fine, except for the GUI. Basically, I need to modify the GUI when clicking a button. I realize that I have to run my code on the EDT to do so, using this code:
EventQueue.invokeLater(new Runnable () {
#Override
public void run() {
// some code
}
});
I just don't now which part of my code is concerned by this. The part where I create the GUI (the constructor of my class)? Or only the part where i modify the values (in that case Listener.actionPerformed())? Actually I tested bot of this, neither worked.
Now what I want to know is how do I modify the following code to update the Button when i click it? Do I have to embed parts of it in the code above or am I completely wrong?
package edttest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class EDTtest {
public static void main(String[] args) {
GUI gui = new GUI ();
}
private static class GUI extends JFrame {
int x;
public GUI () {
x = 0;
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JButton button = new JButton (String.valueOf(x));
button.addActionListener(new Listener ());
JLabel label = new JLabel (String.valueOf(x));
add (label, BorderLayout.NORTH);
add (button);
pack();
setVisible (true);
}
private class Listener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
x++;
System.out.println (x);
}
}
}
}
Whether or not you execute this code on the EDT will do nothing to your label. It is not because you increment x that the label will update itself. You need to call label#setText with the updated value.
Concerning your question around the EDT. All access/modifications/creation/... of Swing components should happen on the EDT. This means you should wrap the contents of your main method in an SwingUtilities#invoke.... Every event that is triggered through the UI (e.g. the click on a button) will already be processed on the EDT. So no need to explicitly schedule a Runnable on the EDT in your listener.
When in doubt, you can always check whether you are on the EDT by using EventQueue#isDispatchThread.
I would also suggest to read the Concurrency in Swing tutorial
I am using JWindow in my project to display a UI that is undecorated and also doesn't appear in the task bar. But, the JWindow always seems to be on top of all other windows. I tried setting the setAlwaysOnTop to false, but it didn't seem to help.
Here's the code that can reproduce the problem :
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
public class Test extends JWindow implements ActionListener {
public Test() {
setSize(300, 300);
setLocationRelativeTo(null);
setAlwaysOnTop(false);
JButton myButton = new JButton("Click Here");
myButton.addActionListener(this);
getContentPane().add(myButton);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Click Here"))
JOptionPane.showMessageDialog(this, "This dialog box appears behind the JWindow!");
}
}
My OS is Linux and I'm using the Oracle JDK 6. Also, while I was testing my app on Windows, I was using JDialog for the UI and it was working fine. But, in Linux JDialog seems to appear in the task bar.
Any help as to how to solve this?
After you set the visibility of the window to True, you send it to the back like this:
setVisible(true);
toBack();
If, later, you want to bring it to the top of the stacking order, you simply call:
toFront();
More details here:
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toBack()
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toFront()
I have a game that uses a JFrame that displays the game info. The window updates whenever a player sends a move object to the server. It works perfectly fine for any number of move objects. However once the 3nd turn starts it hits a wall and here is what happens:
The Jframe completely stops responding to left and right mouse clicks (it makes a windows ding sound when you try to click)
The JFrame still responds to mouse scrolls and keyboard inputs
The JFrame vanishes from the alt-tab program list.
NO error message or stack trace.
Using souts it appears that the code reaches all points of necessary code properly
I can't even click the "X" Window button or right-click close on the task bar
The 3rd turn object is structurally identical to previous turn objects
what on earth can cause a program to do this??
The event dispatch thread has thrown an exception. It is automatically restarted, but your program remains in the state you describe. See also How can I catch Event Dispatch Thread (EDT) exceptions and this answer.
Addendum: How uncaught exceptions are handled and Uncaught exceptions in GUI applications may be helpful. Also check for empty exception handlers.
Addendum: Here's an example.
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/** #see https://stackoverflow.com/a/9935287/230513 */
public class Fail extends JPanel {
private static final JLabel label = new JLabel(
"12345678901234567890", JLabel.CENTER);
public Fail() {
this.setLayout(new GridLayout(0, 1));
this.add(label);
this.add(new JButton(new AbstractAction("Kill me, now!") {
#Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
b.setText(String.valueOf(1 / 0));
}
}));
new Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(System.nanoTime()));
}
}).start();
}
private void display() {
JFrame f = new JFrame("Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Fail().display();
}
});
}
}
Check if your frame class do not overrides isEnabled() method.
I spent couple of hours searching for exception but the responce was pretty trivial: I have implemented interface with such method.