Using the exit button for executing a function [duplicate] - java

This question already has answers here:
Java Swing adding Action Listener for EXIT_ON_CLOSE
(4 answers)
Closed 3 years ago.
I have a problem and don't find a solution.
I have two different forms created with Java Swing and if I click a Button on the first form, than the second form is setVisible(true) and the first form is setVisible(false).
When the user now clicks the X on the top right hand corner the second form should be disposed and the first form should setVisible(true).
So how is it possible to execute code when clicking the X?

Have you tried adding a actionListener to the X?
Like it's done here:
Java Swing adding Action Listener for EXIT_ON_CLOSE

Related

How to add example text to a java JTextField? [duplicate]

This question already has answers here:
Java - placeholder on textfield
(3 answers)
java swing JTextField set PlaceHolder [duplicate]
(4 answers)
Closed 7 months ago.
What I'm trying to do.
I'm basically trying to add a search bar to my program, made out of a JTextArea.
What I would like to do, is add an "example" into the search bar (greyed out text, which disapears when the JTextArea is activated). I've attempted this by changing the text colour, and adding a mouseListener to setText("");. However, this means that every time I click on the bar, it empties, which isn't something I want. (Say I typed in a really long string and had to retype it because it was 1 character wrong).
JTextField searchBar = new JTextField("Search...");
searchBar.setForeground("Color.GRAY");
searchBar.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
searchBar.setForeground(Color.BLACK);
searchBar.setText("");
}
}
What I'd like to know
How can I make it so that, each time the frame is repainted, the search bar will display search... in grey, until clicked, after which it remains persistent.
I hope it's clear why I'm trying to do, any help is appreciated.
Thanks.

In swing, how do you add a hotkey without a menu item? [duplicate]

This question already has an answer here:
How to make shortcut to invisible menu item in Java
(1 answer)
Closed 5 years ago.
For most shortcuts, I add the action to the toplevel JMenu and provide the shortcut to the JMenuItem using setAccelerator(..). How do I make a global shortcut for an action without adding it to the menu?
This question already has an answer here, but I had trouble finding it because I was asking it differently.
As pointed out there, look for getInputMap / getActionMap in the Java keybindings tutorial.
I'll finish writing the question and mark it as a duplicate in case it helps to find it.

using javafx 8 (java 8 install) How to extend Label.setTooltip Display time to 20secs (while mouse is inside Tooltip) [duplicate]

This question already has answers here:
How to control the JavaFX Tooltip's delay?
(8 answers)
Closed 7 years ago.
I have tried Using
1)
import javax.swing.ToolTipManager;
ToolTipManager.sharedInstance().setDismissDelay(20000);
at various locations in *.java file (using NetBeans IDE)
2) Creating OnMouseEnter MouseEvent Handler Override routine including:
ToolTipManager.sharedInstance().setDismissDelay(20000);
Results: No Errors, Runs fine, Except Display Time is ALWAYS 5secs!!!
I would prefer NOT creating my own Popup Control, because I like all the Behavior of existing Tooltip, except Im trying to Display Several words in Tooltips that User Needs to take Long Time to Read. Please Help!!!!!
You're calling a Swing utility class, i.e. import javax.swing.ToolTipManager, this has no control over JavaFX functionality.
According to control JavaFX Tooltip delay, this is not supported and the subject of a change request. That question also contains a reflection based 'hack' and another workaround involving popups.

How to stop main when JFrame is closed? [duplicate]

This question already has answers here:
How to make JFrame exist independently without parent?
(2 answers)
Closed 8 years ago.
I display two JFrame objects at the end of my program. But when I close JFrames, main() thread does not stop. It waits for me to click on stop button (the red rectangle in the picture). But I want main to stop when I close all the JFrames. Is that possible?
I'd suggest writing a window closing listener:
Be careful about cleaning up after yourself. You could just call System.exit(), but perhaps a better solution would do resource cleanup.
The stop button and window listener could call the same method to ensure consistency.
You can set the default close operation when you initialize your frame using this command:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
According to the Javadocs, this will 'Exit the application using the System exit method' when you close the frame. I just tried this, and if you have two JFrames open, closing one will close all frames and stop the main() thread.

swing: make a window never lose focus [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Always on Top” Windows with Java
I am using JFrames
I have a window(ex-Accounts) which gets called from a button in MainMenu.
As long as Accounts is opened i want to forbid the user from accessing MainMenu(which should be visible) unless he closes the Accounts window.
This means that you need your window to be modal. JDialog can be modal, you can either mention this in the constructor like this:
new JDialog(parent, true);
or starting with Java 1.6, you can set the ModalityType:
new JDialog(parent, modalityType);

Categories