How to have Button click show combo options - java

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

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.

Suggestion regarding which layout to use while implementing GUI in java

I have been working on my assignment and I need a help regarding which layout to choose while designing the label combo box and buttons using java GUI. I want things to be done in a particular manner where in one horizontal line I need labels followed by combo box followed by button in second horizontal line I want the same things with one extra button and so on for third and fourth horizontal line. Actually my concern is that I am very much new to GUI and I need a suggestion which layout to choose and how to implement it
thanks in advance
I would go with GroupLayout (use NetBeans, it has a very good GUI builder, that makes UI creation easy). If you want to try more sophisticated solution, you may check JGoodies or MigLayout.

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.

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

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

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