简体   繁体   中英

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:

  1. Read the section from the Swing tutorial on Bringing up a Popup Menu for a working example.

  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM