How do I make a JPopupMenu appear when a JCheckBox is clicked? - java

I have a JCheckBox in a program (labeled "Use MiniTimer") that, when right-clicked, shows a JPopupMenu with options "Show on Close", "Show on Minimize", "Show on Close or Minimize", and "Do not use MiniTimer". How can I make this JPopupMnu appear below the JCheckBox when it is left-clicked, too?
Note that I tried setting the actionPerformed method of the JCheckBox to miniTimerPopupMenu.setVisible(true);, but that merel makes the JPopupMenu appear in the top-left corner of the screen, and even then, it will not register any interactions with it. Does anyone have any experience or suggestions they would like to share?

Read the section from the Swing tutorial on Bringing Up a Popup Menu for a working example. The tutorial uses popup.show(...). Don't know if that is the difference.
If you need more help post your SSCCE (http://sscce.org) that demonstrates the problem.

I think you should set the location of the miniTimerPopupMenu using the setLocation() method, I think the following code does the trick
miniTimerPopupMenu.setLocation((int)jCheckBox.getLocation().getX(),(int)jCheckBox.getLocation().getY()-10);
miniTimerPopupMenu.setVisible(true);
Then you can play with y and x location of the top popupmenu.
Hope this works

Related

How to make JTextFields clickable?

I posted a similar question a year ago, but it was not really well written and I didn't get an answer I could work with. Now I stand in front of the same problem. I got a JPanel (my content pane), where a MouseListener is implemented.
Everywhere I click, I get the exact coordinates of my mouse click. Except my JTextField components. When I click on those, the MouseEvent isn't even triggered. H
ow do I do this, so my mouse clicks on those will also call the mouse event?
Tried: setEnable(false) and setHighlighter(null)
Sorry thought I fixed the X/Y problem.
The X/Y problem simply means you are telling us what your attempted solution is without telling us what your requirement is. We can't suggest a different approach if we don't know what you are trying to do.
I want to open a menu,
Now we know what the requirement is.
The solution is to add the MouseListener to the text field, not the panel. If you have the same popup of the panel and the text field, then you still need to add the listener to both the panel and the text field.
You can do this in one of two ways:
Read the section from the Swing tutorial on Bringing up a Popup Menu for a working example.
Note the above tutorial is a little old, you can also check out the setComonentPopuMenu(...) method of the JComponent class. This approach will create the listener for you.

How to have Button click show combo options

I apologise if this is a bit vague, I know exactly what I want but I am finding it hard to describe/google.
What I have is a swing GUI with a JTable with some connection information and status' in for several connections. In the bottom row I have a "plus" icon JButton and which can add a connection.
There are two different connection types, so what I want is to click on the button, and then the two options appear next to the button in order to select one before moving on.
It would be a similar look to if you had right-clicked on the button, the options would be "above" the table in a list/combo box style, slightly to one side of the button.
Can anyone help me with this?
Thank you very much in advance.
Rob
We've managed to solve the issue using JPopupMenu, which nicely provides what we wanted.
See http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#popup

GUI components of a memory game

I am working on a homework so i am not asking for code, i am trying to make this by myself. by the way, i am stuck again with the GUI part and have little problems with code part. first things is about button size and image size. i didnt use methods for size of buttons just set the image as an icon for the button. but as you see below, buttons dont fit the image.
second thing is how can i first disable the icon and when user presses to button it will reveal the icon ?. and how can i embed 8 pictures in a loop? can i create an array for images... i appreciate if you can help me. and thanks anyway : )
I'd start with How to Use Buttons. JToggleButton works well for this, as you can change the Icon in an ItemListener based on the button's selected state. Examples may be found here and here.

RadioButtons in JDialog in Swing?

I need to display a dialog box which contains radio buttons; and when I select the appropriate radio button, the dialog box should disappear?
Try to use TaskDialog framework. It helps to accomplish just the things you ask for in few lines of code. For your case using Command Links is the best solution.
Radio buttons are possible but not a better solution from usability point of view.
In your radio button listener, use setVisible(false), as discussed in the articles How to Use Radio Buttons and Creating and Showing Simple Dialogs.
I agree with the others that having a "dialog disappear" on radio button click is not a very good UI design. Users generally expect that a "dialog disappears" when they select a button at the bottom (e.g. OK, Cancel, Yes, No, etc.).
In any event, if I am to assume that by "dialog disappears" you mean the window closes, then the way to do that is to call dispose on the dialog.
Also, you may want to consider using JOptionPane.

How to make a mouseEvent from a Component to be recognized in a subcomponent?

I have a JTabPane, and i have added a MoustListener to it(for tab's title).
When i press right click, a popup menu is created.
i need to make it invisible when i press the mouse button in any place of window. how can i do this??
(the MouseListener is applied only for tab's title.)
i need to make it invisible when i
press the mouse button in any place of
window. how can i do this??
This is the default behavour of a JPopupMenu so you don't have to do anything special.
Read the JPopupMenu API and you will find a link to the Swing tutorial on "How to Use Menus". The tutorial contains a working example of using a popup menu. Compare your code with the tutorial to see whats different. We can't help you because we don't know what your code is like.
If you need more help post your SSCCE.
in the good old days what I did to solve this problem was to register a mouse listener with ALL the components.
you can write a fairly simple function that recursively traverses a top level container and do it.
this was with Java 1.1, so maybe there is a better option today.
One way off the top of my head would be to grab the coordinates of the clicks, and then have another method to determine if the clicks are on the tab, or inside of the content area of the tabs.

Categories