JMenu won't close when clicking elsewhere - java

I'm trying to make a swing gui but currently experiencing trouble with making JMenus "close" when clicking elsewhere.
If a JMenu is selected/open i want it to close when clicking somewhere else on the gui but instead it remains open (most of the time) and the only way for me to close it is to click on the selected JMenu again.
I've searched for a solution but only seem to find bug reports on this matter.
Any help is appreciated, thanks.

Ok i found the problem last night.
One of the ActionListeners for the JMenuItems had an initializer for a JDialog in the contructor, i found it when i was trying to recreate the problem.
Thanks for replying.

I have never seen this problem.
Read the section from the Swing tutorial on How to Use Menus for working examples.
Compare your code to the working code to see what is different.
If you still can't find the problem then post your SSCCE that demonstrates the problem because there is no way we can guess what you are doing different from the working examples.

Related

My NetBeans is showing my GUI chars cut and disappear

Every time I run certain GUI interface excercises using swing the program works perfectly but something is wrong with what I suspect might be the jdk or something as it also happens like this on Eclypse.
It first appears to show correctly but with mouse over or when entering data in a textfield it all shows distorted.
Has anyone encountered these problems?
I managed to fix the problem re-installing the drivers to my graphics card!
Thank you.
Hope this helps anyone in the future with the same issue

How to stop JDialog from requesting focus every time?

I wrote a data plotter application a while ago, which has an autoreload feature. Both load and reload action cause a JDialog with a cancel button to pop up. In autoreload mode this happens every several seconds, with the JDialog window requesting focus every time.
I tried calling setFocusableWindowState(false) on the JDialog instance, but that did not make any difference. Could it be because it's a JDialog rather than some other subclass of Window?
UPDATE: setFocusableWindowState(false) does work, I was just stupidly running the wrong jar file. Long story. Anyway, I'm not deleting the question because when I did a little bit of research before posting it, I saw a few other people having trouble with similar issues. This is the solution :)
You have to put these two statements, in the constructor of your class extending JDialog
setFocusableWindowState(false);
setFocusable(false);
So when each time the JDialog appears, the parent container will still have focus on whatever it had.
setFocusableWindowstate() does work, after all. It was my own stupid mistake: I was running the wrong jar file, i.e. not the one with the fix. Thanks to everyone for their suggestions.

Netbeans Removing JPanel does not remove from source

I made a JTabbedPane using Netbeans 7.4. After adding a few things, I decided I didn't want all of the tabs I have created anymore. I then simply compied the JPanel inside of the JTabbedPane onto the same JFrame, and deleted the now unused JTabbedPane. Thing is, all the variables used are still being initialised in source code, even though they do not show up in the Design Navigator or on the JFrame in design mode.
I know I can simply edit this out with a external editor, but I am afraid that the Form data will be corrupted. (This has happened to me before and I lost the ability to edit using Design Mode.)
I used the inspector and FindBugs, but I failed to remove it from there.
"Why am I using Netbeans!?" - A question I won't be asking myself again after I sort this out.
Your first real problem is you are using the Design Mode. Netbeans isn't the problem as is is more than capable of working by itself with code only (I will always only use code).
It is really easy to break a program with a incorrect move within design view.
There isn't really an easy way to go about fixing it, although IMHO sift through the code and find what you need to remove. Make a copy of the .java file first so you have a backup if it breaks!

How to tab through components?

My Swing program has a lot of fields that the user has to fill in in order to make a query to a DB.
The problem is that when I press tab in one field, the focus is not moving anywhere at all.
All the info I read about the focus subsystem makes me think any program should do that by default, hence I'm asking, because my program doesn't do it.
I'm using netbeans and also tried setting nextFocusableComponent on the components but with no luck.
Any tips?
Not sure if there are other part of your code that could be causing you issues. But here is a reference from oracle that shows you how to set tab focuses and next focus. In this it also document other things you need to setup before it will work.
http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html
I suggest you post portion of your code so other can see if there are other underlining issues.
Also there is another thread on stackoverflow about this issue: Java tab order: How to set Tab order in java swing table
Have a look on the FocusTraversalPolicy class.
Hopefully this will help you: http://www.java2s.com/Code/Java/Swing-JFC/FocusTraversalExample.htm

Can't Edit JFrame Form after NetBeansUpdate

Lately, I've been working on a project in NetBeans using the GUI editor that's built in. Before I noticed that it generated an XML ".form" file that didn't appear in the Project Explorer Pane which makes sense. Earlier I was working on the form in the "Design" tab when it notified me about 15 updates. I just updated without reading anything which was probably a bad idea but when I restarted the IDE, it showed my GUI ".class" file and ".form" file separately in the Project Explorer and I couldn't switch between "Source" and "Design". I also noticed that the generated code that was usually not editable was now editable.
P.S. I'm able to create a new frame just fine and the design editor still works with new frame
I have encounter the same problem and I have solved it.
The key in this problem, I think, is particular plugins for JFrame in Netbeans are not active after updating, so we only need to activate them. The easiest way to achieve this is create a new JFrame class, so in this progress, NetBeans can activate all relevant plugins for us. Finally, restart NetBeans, then everything would be fine.
Thank you very much for all of you that you give me some idea and clues in this situation:)
Work on a similar problem led me to this discussion concerning Guarded blocks inside form Java source file. I'm not sure it's related to your situation, but it may help you recover.
If you are trying to recover the lost state of the backing xml for the form I don't know what to tell you.
This has happened to me, but I tend to highly componentize the forms (break up the forms into little pieces), which makes this not such a big deal. Have you tried the NetBeans forums? You might get better luck there:
http://forums.netbeans.org/
Nevermind, simple solution.
I finally decided that, after plenty of tinkering, to restart the IDE which I should have though of first. The Java SE Plugin must have crashed or something, anyway it's fixed.
Thanks for the help!
Or just right click on the corresponding .form file and select open. The Design tab/editor reestablishes.

Categories

Resources