JMenu closes after checking/unchecking JCheckBoxMenuItems
I added a JMenu to an existing JPopupMenu, and added a few JCheckBoxMenuItems, followed by a JButton to the JMenu. Currently, when I open the JMenu and check/uncheck a JCheckBoxMenuItem, the JMenu closes.
So, my question is, how do I get the JPopupMenu, JMenu and all the JCheckBoxMenuItems to stay open while I click multiple JCheckBoxMenuItems? I basically want everything to stay open until I push the JButton. I believe this is possible, but I don't know how to go about it. And I need help ASAP. Thanks!
# 1
hilook at this http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/Popup.htmlregardsAniruddha
# 2
Hi, I'm not using Swing Popup. I'm actually using JPopupMenu, JMenu and JCheckBoxMenuItem. Can I achieve what I want with those three?Thanks.
# 3
> Can I achieve what I want with those three?NoThe logic to close the popup when an item is selected is built into the UI of the component.
# 4
but I have actually heard that it's possible. Anyone have any ideas?
# 5
> but I have actually heard that it's possible
Of course anything is possible components behave the way they do because somebody coded it to do that. So you can obviously write your own code to have it do what you want. Thats why I said you need to write your own UI. There is no simple boolean variable like "keep popup open until I say so" that you can set.
You need to look at and rewrite the UI. Somewhere in the code a MouseListener will be added to the menu item so that when its clicked the items ActionListner is invoked and the popup is closed. You will need to change that behaviour and create you own custom component.
This question has been asked before in the forum. So maybe if you search the forum you can find the other postings. Maybe they will be more helpfull, but I doubt it.
# 6
Thanks! Yeah, that's actually what I've been doing. I add a MouseListener to my JCheckBoxMenuItem. Here's what the code looks like:
jCheckBoxMenuItem.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if(jCheckBoxMenuItem.getState()==true)
{
jCheckBoxMenuItem.setState(false);
jCheckBoxMenuItem.setVisible(true);
menu.setVisible(true); // this is the JMenu that contains this checkBoxMenuItem
}
else
{
jCheckBoxMenuItem.setState(true);
jCheckBoxMenuItem.setVisible(true);
menu.setVisible(true); // this is the JMenu that contains this checkBoxMenuItem
}
}
});
but what I do inside mousePressed doesn't work. I've also tried other things such as setting the isPopupVisble to true.What would u suggest?
# 7
> I add a MouseListener to my JCheckBoxMenuItem.
Maybe it would work but the MouseLIstener added by the UI is still there, so maybe it probably executes after your MouseListener. I would guess you need to remove the listener first, that why I say you need to change the UI and I'm not about to look at the code to see where it is done.
> What would u suggest?
A different design. Like poping up a "Properties Window" and allow the use to select all the items they want and then close the window.
Or, maybe instead of adding individual menu items to the menu you add a JPopupMenu to the menu that contains your menu items. I'm guessing the popup won't close. That what the first posting suggested you use a JPopupMenu.
But I don't have a solution.
# 8
great, thanks!
So, I followed your advice and added my JCheckBoxMenuItems to a JPopupMenu and then added the JPopupMenu to my original JMenu.
Now the issue I had before, regarding the JCheckBoxMenuItems collapsing, is gone. And now I have two other smaller issues:
1. When my mouse enters the JPopupMenu(containing the JCheckBoxMenuItems), my JMenu disappears. I've tried to solve this problem by adding a MouseListener to my JPopupMenu so that when mouse Enters a JCheckBoxMenuItem the original popupMenu (that contains this JMenu is set to Visible (setVisible(true); ). But this doesn't work properly, because now my JCheckBoxMenuItem is not highlighted, and the first item in the JMenu is highlighted, which is totally unrelated.
2. When I check/uncheck a JCheckBoxMenuItem the JPopupMenu containing it, goes behind the application's main frame.
Your help will be appreciated. Thanks!
# 9
Anyone?This is really urgent!
# 10
> This is really urgent!
Can't be that urgent, it took you 2 days to respond to the last suggestion.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 11
Hi, yes actually it is urgent. I'm doing this for work and it took me two days becuz it was the weekend and I didn't have access to code.
Anyways, I just created this SSCCE, as you had advised. As far as instructions are concerned, after launching the application, you need to right click on the JFrame and go from there. You will see that after selecting/unselecting one of the JCheckBoxMenuItems it goes behind the frame. I'm sorry if my instructions are confusing, all you need to do is right-click on the frame and you'll see what I'm talking about.
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class MyPopupTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final JFrame frame = new JFrame("My Frame");
frame.setSize(500,500);
final JPopupMenu mainPopup = new JPopupMenu();
final JMenu myMenu = new JMenu("My Menu");
final JPopupMenu itemsPopupMenu = new JPopupMenu();
JButton okButton = new JButton("OK");
JCheckBoxMenuItem item1 = new JCheckBoxMenuItem("item1", true);
JCheckBoxMenuItem item2 = new JCheckBoxMenuItem("item2", false);
JCheckBoxMenuItem item3 = new JCheckBoxMenuItem("item3", false);
frame.getContentPane().add(mainPopup);
mainPopup.add(myMenu);
myMenu.add(itemsPopupMenu);
itemsPopupMenu.add(item1);
itemsPopupMenu.add(item2);
itemsPopupMenu.add(item3);
itemsPopupMenu.add(okButton);
okButton.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
itemsPopupMenu.setVisible(false);
myMenu.setVisible(false);
}
});
myMenu.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e))
{
mainPopup.setLocation(e.getPoint());
mainPopup.setVisible(true);
}
}
public void mouseEntered(MouseEvent e)
{
itemsPopupMenu.setLocation(new Point(myMenu.getLocationOnScreen().x + mainPopup.getWidth(),myMenu.getLocationOnScreen().y));
itemsPopupMenu.setVisible(true);
}
});
frame.setVisible(true);
}
}
# 12
I don't know why you get the behaviour you do and I don't really understand your SSCCE. In general popup menus aren't added to containers they are just made visible. So maybe the problem is related to that.
Check out my SSCCE of use a popup menu:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=745393
While playing with the code I noticed that if you add a JCheckBox to the popup instead of a JCheckBoxMenuItem, then the popup stays open when you click on the check box. So maybe your solution is that simple.
# 13
Hi! Thanks so much. I'm about to try your solution to see if it works. Could you please also check my latest posting? I realized that the SSCCE has some faults. But the new one does not.Thanks!
# 14
Thanks for your posts. I actually figured out the problem today. So, it's fixed. Thanks again!