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.
Related
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.
On a JPanel I have a few buttons and JScrollPane containing a JTable.
One of the buttons is custom made, I have overridden the paintComponent method to draw an image.
However, if I set the contentAreaFilled or isOpaque properties to false (so that the background is not displayed) whenever I roll over the button the content of the scroll pane is hidden (a grey image appears inside, instead of the table contents). I tried using different layouts for my panel, but got the same result. The only fix I found is setting rolloverEnabled to false for my custom button, but that is not really an option for me, as I want the button image to change at rollover.
The weird thing is, it only happens when I set one of those two properties mentioned above to false. The button acts perfectly if I do not hide it's background. Just out of curiosity, I tried the same on my other two buttons, which are just JButtons with nothing customized, except their size, and it had the same effect on the scroll pane, so I'm pretty sure it doesn't have anything to do with the implementation of the custom button. Has anyone encountered this problem or know anything that may help me with this?
I'm working on a minesweeper in Java with Swing and I figured it'd be a fast way to get "rid" of a button that was clicked by using
JButton.setEnabled(false); (with a proper icon too, of course).
But do I have to remove all the listeners connected to this button later or is it enough and I can just forget about the said button then?
You have 2 different questions, one in your title, and one in your description.
Is removing actionListener necessary when you disable the button?
As stated in the previous comments, no.
But do I have to remove all the listeners connected to this button later...
Yes, if you have other kinds of listeners. For example, a MouseListener will still fire if the button is disabled. Usually, there is no need for a MouseListener on JButton, but there may be in some corner cases. I'm not sure about the other types of listeners that can be added to a JButton.
Just wanted to clarify.
I've used
chkBox.setIcon();
chkBox.setSelectedIcon();
chkBox.setDisabledIcon();
chkBox.setDisabledSelectedIcon();
to set custom icons for my JCheckbox. But now, if the focus moves to one of the checkboxes, there is no border shown around them or anything else, which tells that the checkbox has focus.
Does anyone know, how to give some feedback when a customized checkbox has focus?
Thanks
Your problem definitely depends on Look and Feel (L&F) that you are using in your application (if you don't setup one - i guess you are using MetalLookAndFeel?).
Anyway, there might be a lot of solutions:
Check that your JCheckBox is actually focusable and focus painted. Be aware that some L&F might switch off focus painting - check checkBox.setFocusPainted() method.
If you are not satisfied with default focus painting - you might want to create your own CheckBoxUI that paints a better focus indicator. That requires some basic knowledge in UIs creation though.
If you want to paint focus indication straight on the check icon itself you can create your own Icon-based implementation that paints it together with current check state. I have posted a custom Icon example in other topic about state-dependant icon if you want to see a real example.
There might be other solutions but they depends on the L&F you are using...
You can use this ready-to use checkbox alternative:
http://codetoearn.blogspot.com/2013/01/swing-fantasy-checkbox-with-customized.html
I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().