mouseListener - Implements vs addMouseListener - java

I have a JTable and I want to detect a single click and double click on a row. I recognize I need to create a mouseListener. However I have seen examples that extend a JTable and implement a mouseListener as part of the JTable definition, as well as create just a mouse listener and then simply addMouseListener to the JTable. What is the behavior difference and which is the preferred implementation if any? TIA.

Related

How to add a control panel on a JTable row on MouseOver (hover)

I have a non editable table, on which I'd like to display a row contextual panel (likely a JPanel). Somewhat like Gmail is doing: when moving the mouse over mail rows there's a simple tool bar showing up on that specific row.
Like in gmail the action of controls I'd like to display won't edit the values, instead they will use the value in the row to perform some offer work.
I have played with the following :
TableCellRenderer, the display mostly works, but it has limitations:
the component is only used for rendering, so one cannot use it to simply add multiple buttons
it requires another column
for the hovering behavior (ie display on row only when the mouse is hovering the row) it requires collaboration with the table's MouseListener
TableCellEditor, my table is not editable so the cell editor is neither called
it also requires a specific column
it also requires collaboration with the table's MouseListener
MouseMotionListener can be used to display a popup for certain coordinates
the popup feels like it's the right component for this
there's quite some code to handle the popup lifecycle (closing it when the mouse move out of the row, don't re-open a popup if there is already one open)
tool tips: as far as I am aware the swing tooltips do not allow to have control components like buttons, etc
I did related question and answer on stack overflow. But they all require to add a column to display and use these swing components.
Given that you have posted no code, this question is a bit broad.
Nevertheless, the way to do it would be to stick a JPanel in a JPopupMenu. You need to create a listener on your GUI to know when and where the JPopupMenu should appear
--- Edit ---
I think you have to add JMenus to a JPopupMenu, and what I suggested about adding a JPanel won't work cleanly. You can either use JPopupMenu, or use a JWindow and put your JPanel in that.

Custom JButton with JTextArea component inside

I extended JButton so it would fit my needs. I have one JTextArea and two JLabel components inside of my new class. The problem is, that I cannot click through JTextArea. So, when mouse is in JTextArea bounds, the button is not responding. There is no problem with labels.
As on screen. There are four buttons. Each one is a separate yellowish rectangle. When mouse is over JTextArea's gray rectangle, I cannot press the button. I need JTextArea because it supports multiple lines. Is there any option to make it not intercept the mouse?
It would be ok if I could attach ActionListener to JTextArea, but I can't. It cannot have this kind of listener.
You look to be trying to use a JButton in a very non-button way, including having it hold a JTextArea, and not looking at all like a button. If you want a clickable area that is not an identifiable JButton, then consider using a MouseListener instead. You would likely have to add the same MouseListener to the container JPanel and the JTextArea.
Take a look at Concepts: Editors and Renderers to understand the difference between a renderer and an editor.
A renderer is a "rubber stamp" of the component, it is simply "painted" onto the surface of the JTable and is not a real life component
You would need to implement a custom editor, which could translate the trigger event (in your case the MouseEvent) into a local context.
Take a look at TableCellEditor#isCellEdtiable which is probably the closest you will get to the source of the event which might trigger the cell to become editable.
JButton could be seeded with a HTML String (`"This is some text"), which would be capable of supporting multiple lines and line wrapping as well
Having said all that, you might want to seriously reconsider your design...

JButton in JTable doesnt display animation when clicked

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.

Adding multiple icons with separate tooltips in JTable

I need to show multiple icons in a single column in JTable. I am using an modified Icon implementation.
Class IconCollection implements Icon {
Icon[] icons
}
Since it implements Icon interface , I am able to show multiple icons in single column. However I need to assign separate tooltips for each icon. Since JComponent has single setToolTipText() method, it is not possible.
Any suggestions?
Since the JTable has a decent implementation of getTooltipText which delegates to the component of the renderer, you can completely solve this in the renderer.
In the getTableCellRendererComponent method you can return any JComponent. If you would opt for a JPanel containing multiple icon instances with each a correct tooltip (instead of your big icon).
You could create a component to contain your icons and override getToolTipText(MouseEvent), basing the text returned on the relative position of the mouse.

Change value when mouse is clicked

I have JTextfield. Now I want to change value when in this component is mouse clicked. For example: score (2 big JTextField) and when I click to one of these field it increase the value from 0:0 to 1:0.
Should I implement MouseListener or there is some easy way how I can do this? In mouse listener I need override just one method mouseClick and other method will be empty.
And another question: when should I implement MouseListener? e.getButton() return always 1 for left button and 3 for right button?
Should I implement MouseListener or there is some easy way how I can do this? In mouse listner I need override just one method mouseClick and other method will be empty.
Use a MouseAdapter.
An abstract adapter class for receiving mouse events. The methods in this class are empty. .. Extend this class to create a MouseEvent (including drag and motion events) or/and MouseWheelEvent listener and override the methods for the events of interest.
Now I want to change value when in this component is mouse clicked
JTextComponents are Focusable, look for FocusListener
Implementing MouseListener on your class is one way to do it, but if you just want to react to clicks, it's easier to use an anonymous class extending MouseAdapter
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
// do your thing here
}
});
As for the second question, the API documentation quite nicely documents the return values of MouseEvent.getButton().

Categories