I am trying to find a method that can close the active JFrame.
I am unable to use frame.dispose();, as I am declaring the action listener in a toolbar class and the frames I want to close are not static and are declared at runtime.
I have tried using:
java.awt.Window win[] = java.awt.Window.getWindows();
for(int i=0;i<win.length;i++){
win[i].dispose();
}
and whilst this does work, in certain circumstances it will close more than one window even though only 1 window appears to be open, so frames will flash open and closed many times depending on what actions the user has made.
For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles.
I am hoping someone will know of a more simple and reliable way of closing the active frame in the mould of acitveframe.dispose(); - which I now is not a real solution!!
What happens if you try to get the Window ancestor of the source of the action event? i.e.,
#Override
public void actionPerformed(ActionEvent actionEvent) {
Component comp = (Component) actionEvent.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose();
}
This won't work if the source is not a Component or if it is not contained within the top level Window of interest.
Regarding:
For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles.
I'll bet with a bit of effort you could create and post something that comes close.
I am hoping someone will know of a more simple and reliable way of closing the active frame
In your loop you can add:
if (window.isActive())
// do something
Or maybe a simpler approach is:
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
Also, assuming your active window is a JFrame, instead of using window.dispose(), I have used code like:
WindowEvent windowClosing = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
frame.dispatchEvent(windowClosing);
this will simulate the user clicking on the "Close" button which means that any WindowListener you added to the frame will also be executed. See Closing an Appplication for more information and ideas.
When you are declaring your JFrames, declre them as final if you cannot use static :
final JFrame f = new JFrame();
It would solve the problem.
Related
I al reading a code that I did not create and I have some trouble to understand how it is working and especially how action event ( like a click) are managed ?
So far I am undertanding that it creates a the first window will all buttons and boxes and the main end with a setVisible(true); and my window appears.
Then is ask my some file locations and I click on a button to go to the second window.
What I would like to understand it is how in java we managed this click event for the second action ? I mean I understand the use of button addActionListener the first time but my understanding ends with the second event (=I click on the button and it brings me to the second window and do the calculation) i.e. where/ how in the code this is handle and goes a scond time to the functions.
Knowing that at the end of the main I have some functions starting with
InterfaceDynamics.cardSwitch() and this function managed all events that can occur
public static void cardSwitch(final String _buttonKey, final String _Key, final JPanel _cardPanel,
Another related question (I guess related) it is the use of
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
which I really do not undertand (even if I already google it).
Sorry if I am not clear and for my bad understanding of java and window, Swing, Frame management and really thanks for any help.
Regards
I have a very basic experience with java and have 2 questions.
Question 1: I have a button that opens a new JFrame, which works
perfectly. On the second iJFrame I have a button which -should- make
the app hide (lose focus).
I have looked around and found that this is easily done with JFrameName".setFocusableWindowState(false
The problem is I can't seem to be able to name the current jframe JFrame I'm in, so I can't call the function. I usually call the JFrame I've made into view with this in the public main of my starting code :
JFrameName newframe = new JFrameName();
newframe.setVisible(true);
Where exactly do I declare the name of the JFrameName instance I've made in my JFrame class so iI can call the setFocusableWindowState function?
Question 2: The above question is done because I want to link a
keyboard shortcut to a button. this keyboard shortcut should then be
used - within another window, not the java application. my question:
can iI manually define keyboard shortcuts (for example
control+alt+delete) or (control+f1) within java so my program will
execute the button hits for me?
I'm not sure I understand the question wholly. But maybe this will help.
Instead of making a JFrame, since it is not a "final" class, you also have the option of creating a new class that extends JFrame. The new class can have additional methods. One of those methods could be one to allow you to tell the new JFrame the name of the JFrame that you want to return focus to.
As far as I know, it's possible to create a KeyListener that can tell if a shift or alt or control key are pressed. I'd have to research the details to know for sure. A good starting point should be the tutorial How to Write a Key Listener.
I have a button which -should- make the app hide (lose focus).
If you want the window to hide then normally you would use:
window.setVisible( false ).
This is the opposite of showing the window.
The setWindowFocusableState(false) will still keep the window visible, you just won't be able to make any component on the window have focus.
The problem is I can't seem to be able to name the current jframe JFrame I'm in
That information can be found by coding the following in the ActionListener of your button:
JButton button = (JButton)e.getSource();
Window window = SwingUtilities.windowForComponent( button );
window.setVisible( false );
I have three classes I am working with here. The first is an admin page where the user can select to either add, update or delete an employee on the system via a drop down box. When the user selects one of the three options from the combo box, a JFrame (employees) comes up with the necessary fields to perform their task and the admin frame will still be displayed behind it. On this frame there is a "Cancel" button. When they click the cancel button only this frame must close but keep the admin frame open still. The button is generated from a separate class (empClass) to be displayed on the employee frame. My problem now is I am struggling to get the button to dispose of the employee frame, but out of the several ways of trying this it cannot work. One way produced an error each time I ran the application, another method caused the application to crash/freeze whenever I try select an option to perform on the employee frame and the code I have currently implemented performs no action at all. I think the problem is a an issue with communicating with the forms but I am not entirely sure. Please help as I have been struggling with this for hours and the internet is providing absolutely nothing useful. Most resources refer to the dispose() method which I have tried in various ways but all the ways I have tried do not work, crash the application or cause errors to occur. Even the other questions similar to this on here have not helped me out at all.
I have tried calling the button from the employee frame to try and link the function to the "Cancel" button. Here is the code I have implemented in the empClass:
public void disposeof()
{
employees empp = new employees();
empp.dispose();
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt)
{
disposeof();
}
Here is the employee coding:
public class employees extends javax.swing.JFrame {
empClass ec = new empClass();
adminPage ap = new adminPage();
public employees() {
initComponents();
getContentPane().add(ec.getpanel());
this.add(ec.getpanel());
this.add(ec.lbltitle);
this.add(ec.cancel);
this.add(ec.bfunction);
this.add(ec.empList);
}
As you didn't provide the code of your JFrame so I guess problem is in the code of your JFrame. You might be setting setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE) for your employee class as it is static property so it will close all JFrames. You should set it setDefaultCloseOperation(employees.DISPOSE_ON_CLOSE) OR setDefaultCloseOperation(employees.HIDE_ON_CLOSE). And after that while triggering your event you can call empp.dispose(); OR setVisible(flase).
First of all only use one JFrame and use JDialogs for the underlying other windows you may want to see appearing. On JDialog, use setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE).
Another way you can do this is to hide the frame using setVisible(false).
I would like to make possible the navigation of frames in java.Whenever i close a frame the remaining frames which are also opened get closed;and the entire program stops.
Please help...
You probably used
//this will terminate or exit your application
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Maybe you want to use this instead,
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
for your reference go to this link
If you want to close only that one frame, you should do something like this: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
If you want to close all frames whenever a single frame closes you can do the following:
You could use a window listener and call System.exit(0); when the JFrame closes, or try setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); on each JFrame. That way your program would close all frames and end.
If you need to perform some tasks before application quits, you should probably use the window listener.
You can also do that graphicaly. right click on your frame and select properties and from there you can change that like the below picture.
If you were using swing palette. In frame properties choose default close operation as (Dispose). Follow as per the image given in this solution.
My problem was that I used a listener found on the basic tutorials :
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
I know it's dumb. I didn't see it, but some people might have done the same thing, so I'll just leave this here ;)
I have a small dialog frame that appears, and within this frame are a series of buttons and a textbox.
I need the frame to be able to detect when the user has put focus on something else on the screen (being: anything besides the frame and its components), so I can close down the frame.
Any advice on how to go about this? I've been trying at focus solutions for hours, to no solution!
Try using a WindowStateListener
The WindowEvent parameter it provides can tell you if the window has lost focus through the getNewState() method.
class MyFocusLostListener implements WindowStateListener {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == WindowEvent.WINDOW_LOST_FOCUS) {
e.getWindow().setVisible(false);
}
}
}
need the frame to be able to detect when the user has put focus on something else on the screen
Use a WindowListener and listen for windowDeactivated.
listen to property changes of the property "permanentFocusOwner" of the KeyboardFocusManager. On being notified, check if the new focusOwner is in the child hierarchy under the frame, if not - close the frame.
Edit: seeing the answers suggesting a Window/StateListener - they are better than mine for a top-level window :-) Listening to the keyboardFocusManager is a good approach for containers deeper down in the hierarchy, implemented f.i. in the CellEditorRemover of a JTable (to decide if a pending edit should be terminated)