I'm currently having difficulty on adding mouse-over effect to all cells of particular column. The problem is when adding mouse listener to table, the cell-inner component doesn't get mouse event while cursor is hovering over that cell. Could you please tell me where is problem?
If I recall correctly, the cell widgets are only temporarily used to paint themselves over the correct area but i don't think they are active othewise. You should try listening to the JTable and then you can find back which cells have been clicked with rowAtPoint and columnAtPoint().
Related
I need your help guys.
I am currently working on a simple JTable in Java with 3 Columns and 128 rows. Now there is one problem i cannot solve at the moment. The thing i want is that when I click on a single cell it changes its background color to Green.
So i need to implement a MouseListener which reacts to the selected cell and sets its background to green. Sort of a "CellListener" I mean.
I've tried so many things and searched many blogs etc. but none of them gave me a satisying answer.
Do you have any tips for me?
I doubt you will find a direct link between you being able to click on the table and it respond. Because JTables aren't going to listen out for it like that. If you want to do this. You will need an intermediate step. I would suggest that you use the mouse listener to get the mouse location on the screen when the user clicks. And then check IF that location is the same as a particular cell of the JTable.
I'm using a TableViewer to create a table but whenever I hover with the mouse over a cell the whole row gets highlighted but I consider this more confusing than helpful.
What I want is either to highlight nothing when simply hovering or to highlight only the cell the mouse is currently over.
I've looked into several code snippets but I never found something that does what I want.
So how can I achieve the above described behaviour?
I'm new to swing and I faced my first serious problem, here goes:
I have a JPanel with JTable and lots of checkboxes below the table. I'm trying to align the checkboxes below the table with checkbox located in the first table column. The problem is - this has to be done when model data changes.
Immediatly after the fireTableDataChanged() gets triggered in JTable there is no way to get the screen location of its custom cell renderer component (which in my case is JCheckBox).
As far as I understand this happens because JTable gets redrawn asynchronously. Whenever I try to get the location the IllegalComponentStateException gets thrown.
Any ideas on this are highly appreciated.
You shouldn't do it manually. You should use the appropriate layoutManager. Check this link for the different Layouts:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
This might help you to get screen location of row and column.
Rectangle rect= cartTable.getCellRect(row,column,true);
//cartTable-Jtable object name
int x=Double.valueOf(rect.getX()).intValue();
int y=Double.valueOf(rect.getY()).intValue();
This is weird - maybe it has a really simple solution, but I have spent two days on it so far! I have a JList in a scroll pane that I want to tab up and down in (and/or use up and down arrows).
I want to position the cursor at the first entry, and colour it, when the JList is displayed. The only way I've found to do this is to select the first entry, then request focus for the JList from inside the cell renderer - in the code that colours the selected row.
This works, but it changes the JTextField immediately below the scroll pane to non-editable. Remove the request focus from the cell renderer code, and the JTextField becomes editable again - but then I have to click on the JList to use the arrows on it or tab through it. Of course, I may be using totally the wrong tools. Help would be much appreciated...
I have just read that only one component can be focusable at a time - but why won't setFocusable() + requestFocusInWindow() change the focus to where I need it? I assume this is why the JTextField is not editable, even though it was set using setEditable(true)...? Are there other requirements that have to be in place before you can make a field focusable/editable?
There are a lot of discussions on web on how to have hyperlinks in swing and JTable, e.g. HyperLink in JTable Cell.
The approach above is problematic because it only knows which cell the mouse is in, not the exactly text it is on, which means:
Can not handle multiple hyperlinks in the same cell;
Can not make the mouse cursor showing intuitively. Whenever the mouse is in the cell that has hyperlink, the mouse will become hand shape, even when the mouse points to some normal text or even emtpy area.
Another approach is to display JEditorPane in the cell but is also problematic because JTable only uses the JComponent returned by cell renderer to draw, I don't think the object will be sent any events. Because the default renderer will re-use the component for every cell, so it doesn't make any sense to have it handle any events.
So I wonder what's the best way to achieve the above effect.
Another approach is to display JEditorPane in the cell but is also problematic because JTable only uses the JComponent returned by cell renderer to draw, I don't think the object will be sent any events.
Try placing the cell in edit mode every time the cell gains focus. Then the editor should be display which is a real component and it should reaceive all the events. Something like:
JTable table = new JTable(...)
{
// Place cell in edit mode when it 'gains focus'
public void changeSelection(int row, int column, boolean toggle, boolean extend)
{
super.changeSelection(row, column, toggle, extend);
if (editCellAt(row, column))
{
Component editor = getEditorComponent();
editor.requestFocusInWindow();
}
}
};
I would customize the code to only edit cell for the specific column.