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.
Related
I'm having difficulty in getting resources that will help me with an issue. I want to add a button that will let me insert text anywhere in my JFrame so I can click and add a text
Before that, I have been using JTextField and JTextArea, but these are useful in a predefined space (I believe).
What I want is like the classic Add Text button in Paint ("A" button) in which when you click on it, you will have the text cursor (like an "I") and then click somewhere in the frame so I can actually add text in that area. Which I call "adding text dynamically". Unfortunately I don't have any code to show because I'm not sure how to build the code from scratch or maybe use a method in Swing, but I'm more than keen to explain better if this is too generic for you guys.
You're going to need to be familiar with Java graphics and associated methods, particularly drawString(), so start by reading the official Graphics Tutorial.
Then learn about MouseListeners, which will help you recognize when and where the user clicks in your JFrame.
Then learn about KeyListeners, which will allow you to detect when your user is typing. You may wish to use a key bind instead, but as described, your UI is relatively simple with only one focusable component, so keylisteners are probably OK.
This general strategy should help you take a flying leap at the problem:
detect mouseclick with mouselistener
draw an insertion bar
detect keypress with keylistener
use graphics.drawstring to draw the character specified by the keypress
redraw the insertion bar
Make sure your keylistener handles the enter key, so that you know when to erase the insertion bar. You're going to paint your JFrame in between each of those steps.
The result will be crude, to say the least, and you'll notice that you have to handle a lot of issues that crop up along the way before things start to look professional. For example, you'll have to deal with font metrics. Not every character is going to be the same width, so you have to calculate the X argument to your drawString() based on the charWidth of whatever character the user types. You might do well to start with a monospaced font for that reason.
like the title says. I'm trying to figure out how to keep the focus on the JFrame or perhaps the window. So that when I press F1 on the keyboard, it activates a method. And it should also do it when I'm typing in a textfield.
I have read through the "How to Use the Focus Subsystem" but can't find what I'm looking for. Or maybe I just don't know where to look.
I tried using contentPane.setFocusable(true); but it looses focus when I activate another component. So how do I get it to keep focus?
You may be trying to solve the wrong problem caused by using a KeyListener, which requires focus. Instead, use key bindings or setDefaultButton(), found in the frame's root pane.
Addendum: I tried key bindings, and solved it almost. It still does not work when a JTextField is selected. Do you maybe have a solution for that?
Depending on your needs, consider a DocumentListener or DocumentFilter. See this Q&A for more on the two.
Ive don everything using this guide http://www.cordinc.com/blog/2010/01/jbuttons-in-a-jtable.html
So one little problem remains. While when i click on the button event is fired and method is executed. The buttons animation does not work providing no visual feedback.
Im guessing its due to fact that in order to repaint JTable you need to call the models fireTableDataChanged() method.
Table Button Column shows another way to do this.
The example cited is less than satisfactory because TableCellRenderer shows a JButton, but the MouseListener ignores the button's ButtonModel. Implementing TableCellEditor, as shown here, is a the better approach. For animation convenience, you may want to look at the DefaultCellEditor, shown here, with a JComboBox having a single entry.
I have a JPanel (pNums) which contains another JPanel (pGrid). pGrid itself contains a grid (JLabel[][] in a GridLayout) of labels. There is a mouse listener which catches events from pGrid and does fairly important stuff with them (as in, the entire functionality of the program relies on the mouseClicked() event). This works perfectly, exactly the way I wanted it to... until I add tooltips to the labels.
As soon as I call JLabel.setToolTipText("SomeString") the listener stops reacting to events (I have tried most, if not all of the mouse events, none of them seem to be called).
I am sure that it is the tooltips by the way, commenting out the setToolTipText() completely fixes the problem. Of course, since I needed the tooltips, it also causes a whole host of other problems.
I've looked around and while I haven't found anything quite right, I get the impression that I just chose a really bad way to do what I wanted. But I also want to know for sure.
Can I get both the event and the tooltip or should I go back to the drawing board.
I think you might be able to "fix" this issue, with setting delay on tooltip appearance. But once it will appear user will have to click to hide it anyway.
http://docs.oracle.com/javase/7/docs/api/javax/swing/ToolTipManager.html
The reson for this might be, that tooltip itself needs mouse click, to hide.
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.