CheckBox change of state when X Button is pressed - java

I have got this problem:
I have JMenu, which consists of CheckBox. If that is checked (ItemEvent.selected), it opens a new JFrame, when unchecked (ItemEvent.Deselected) I dispose JFrame. Everything works OK. But when i push the "x" button on the top of the window, which is set as JFrame.DISPOSE_ON_CLOSE, I need to uncheck the checkbox in the JMenu.
Right now it's like this: You check, then program opens new window, which you close with "x" button, then you open menu again and checkbox is checked, but window is already close.
I tried to add some clickListener, but it!s getting destroyed with closing the frame.
Any advice will be welcomed :)

As the question hasn't been put very well, its hard to determine exactly what your issue is. However... If you want to automatically uncheck the box when the window has closed, you could use a window listener on your JFrame.
frame.addWindowListener( new WindowAdapter() {
#Override
public void windowClosing(WindowEvent we) {
//Uncheck the box
}
} );

Related

How to detect if user clicks on window title bar or any other part of window border in Java

I have implemented a pop-up numeric keypad using the Swing Popup class. I have a button associated with a JTextField that opens the numeric keypad when the user clicks on it, and then when/if the JTextField loses focus the Popup closes. It generally works well, except that occasionally I get artifacts that are "left over" from a Popup after it has been hidden. Sometimes the artifact is an image of the Components that were shown in the Popup, but more often it is a "black hole" of sorts that obscures anything else displayed in the same area of the screen in which the Popup had been, which can only be remedied by shutting down the application and the JVM.
The problem is difficult to reproduce, but it seems to manifest when the user manipulates the base Window while the Popup is open, such as by moving or resizing it. My thought is to simply hide the Popup when anything like that happens, which I can do with a combination of a WindowListener and a ComponentListener. However, I'd like to take it one step further and hide the Popup as soon as the user so much as clicks on the window title bar or another portion of its frame, even before they move, resize or iconify it. JComboBox popups in fact work this way. However, I have been unable to find any kind of mechanism by which I can be notified that a user has clicked on the window title bar. I've looked at the JComboBox and related code and can't figure out from that how it's working this magic, either. Is there any other kind of listener I could use to get this kind of notification?
I have implemented a pop-up numeric keypad using the Swing Popup class.
Well post your code demonstrating the implementation and problem when you post a question.
I don't know exactly what you are doing but you might be able to use a JPopupMenu. This will close when you click on the frame title bar with no FocusListener or any additional logic.
will be deleted, just code for testing,
private boolean _myWindowFocusLost = false;
.
_xxXxx.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {//Invoked when a component gains the keyboard focus.
if (e.getOppositeComponent() != null) {
if (e.getOppositeComponent() instanceof JComponent) {
JComponent opposite = (JComponent) e.getOppositeComponent();
if ((opposite.getTopLevelAncestor() != _myPopupWindow) && (!_myWindowFocusLost)) {
_myWindowFocusLost = false;
}
}
}
}
});

Open New Window And Close Parent

When my program launches, a window (aka "StartWindow") opens that has 3 options: New Game/Load Game/Exit. When New Game or Load Game is clicked (and after some input), the game window (aka "GameWindow") will open so the user can play.
When the GameWindow opens, I would like to have the StartWindow close. And when the GameWindow closes, the StartWindow will open until the actual "EXIT" button is clicked.
Is there a way to do this? Right now, the only way I can achieve something similar to this is having a boolean called "gameRunning". When this is true, the buttons on StartWindow will have no action when clicked. Opposite when false.
For example purposes, suppose that each window has 3 buttons, a text field, and nothing else.
Use setVisible(false) method on parent before opening any child window. When child window closes call setVisible(true). It will solve your problem

JButton needs to be clicked twice after JDialog

I've been struggling with some problem while creating my app based on Swing. I have a main JFrame which consists of:
JMenu
JPanel containing a JButton and a JLabel
JButton is linked with ActionListener. Clicking JMenu (MenuListener) brings up a JDialog with some form. The problem is, when the JDialog is closed (it doesn't make difference whether I do it with dispose() or rather showVisible(false)) I need to click the JButton two times before it triggers for the first time. From now it normally works with one click.
Every time the JDialog is in front, the problem appears.
PS. The JDialog is set to be modal, with JFrame as parent.
It sounds like a focus issue.
The first click restores focus to the app and the second clicks the button. Typically, I have seen this when the JDialog has the wrong parent and focus can not be returned.
Thank you for your answers.
I have considered posting some code, but it involves 4 classes so will be quite long.
I have also tried things with focus before, but nothing helped. What is interesting: if I display the JDialog by new myDialog.showVisible(true) it behaves like I've described. But if I use construction like this:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDialog.setVisible(true);
}
});
it closes normally and parent frame doesn't need to be clicked before responding, but on the other hand the displayed Dialog needs so. Additonally, what I do not understand, after opening the Dialog cursor is placed in the text field and I can write normally, but to click some button on it I must click once on the Dialog, only second and next clicks behave like I want to.
PS. Closing the dialog like in the second included example changes nothing.

Bad event on java panel

I have a java panel with 4 buttons. When I click on of these buttons, a new frame appears and the first is hidden with setVisibile(false).
On that new window, I have another button, but when i click it, I got the event corresponding to the fourth button of the first window. Clicking the button again does the trick, but of course this is not acceptable.
Am I missing something? I just show the frames with
nameOfTheFrame.setVisible(true);
and I have MouseListeners on every button.
The code of the last button is simply:
System.exit(0);
EDIT
Sample code:
private void btn_joinGamePressed(java.awt.event.MouseEvent evt) {
GraphicsTools.getInstance().getCreateGame().setVisible(false);
GraphicsTools.getInstance().getMainPanel().setVisible(false);
GraphicsTools.getInstance().getRegistration().setVisible(true);
}
GraphicsTools is a Singleton.
EDIT 2
Some more informations.
I noticed that on MAC OS works fine. The problem happens only on Linux and Windows.
This must be happening because of your mouse listeners. May be it is identifying the old button in your first click which is in the same location of new button (It is just my guess).
Change the mouse listeners to action listeners. For a button, it is sufficient if you have action listener.
Try this.
Try calling revalidate() on the frames as you change their viability.
Edit:
It could be something with the creation of the frames. Make sure you are calling 'pack()` on the frames.

JWindow alway on top not getting focus events

I have a jwindow(set to be always on top) that you can click to get a pop menu. If the user right clicks the window it shows the pop menu but then if the user clicks any other window(such as firefox) pop menu does not disappear.
I tried to fix it by adding FocusListener on the jwindow, i implemented FocusListener and override
public void focusGained(FocusEvent e) {
System.out.println("gain" );
}
public void focusLost(FocusEvent e) {
System.out.println("lost" );
}
but there event never get called. i also tried the following,
addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
System.out.println("gain 2" );
}
});
this event also not called.
All this jwindows has is a single JLabel with a picture on it.
From memory JWindow's do not receive focus/window events.
You are suppose to call setFocusableWindowState(true) on a JWindow to allow it to be focusable. But that "still" is not enough. The JWindow must also have focusable Components and I'm still not able to get it to work.
Using JFrame setUndecorated() seems the best choice.
To be focusable, a JWindow needs to be created with a parent Frame, like new JWindow(parentFrame). Do that and I think you should find it will automatically get the focus when you set it to visible.
Not really sure what you are trying to do. If you are trying to hide the popup manually then you should probably use a WindowListener and handle the windowDeactivated event.
If you really want to display a popup menu, you should be using JPopupMenu, not implementing it yourself.

Categories