java swing jtable screen location of cell renderer component - java

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();

Related

TableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex) is not updating the cell in JTable

I have a JTable in my code.
And whenever there is an update to any specific column in the row (cell basically), I will update the corresponding icon in that cell.
so I'm basically following these steps.
Step 1: I update the model.
Step2 2: I'm calling
tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
This works fine.
But problem comes when I drag and drop the columns from one position to another in Table header. And whenever there is an update to any specific cell, I follow the same steps as I mentioned before.
Problem: I'm not seeing the Icon painted. But if I bring the focus on top of that row in table it is painting the icon.
Observation: I see the tableRowIndex and tableColumnIndex are correct after dragging the columns.
Just for testing I added this piece of code in the problem scenario.
examTable.repaint(examTable.getCellRect(examTableRowIndex, examTableColumnIndex, true));
This is repainting the cell properly.
But this is not the right solution I guess. I tried to debug the code I didn't find much about the problem
I'm calling tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
That is wrong. You should never invoke the firXXX methods directly. That is the job of the TableModel to invoke the appropriate event when the data is changed.
I will update the corresponding icon in that cell.
All you need to do is invoke model.setValueAt(...) method to change the Icon and the model will notify the table that data has changed so the table can repaint itself.
examTable.repaint(...)
Again you should not need to manually invoke repaint on the table
But problem comes when I drag and drop the columns from one position to another in Table header.
Not sure why you need special code for this if you follow the advice from above. But if for some reason it is still necessary then you need to look at the convertColumnIndex...(...) methods to make sure you are using the proper index for your column.

Mouseover effect on JTable cell

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().

restore jTable focus and position after removing a row from table model

one question with the java jTable class. Actually I am not a Java programmer and just now using Java to design a GUI in Matlab. What I've done is:
A jTable is built into a Matlab GUI.
I used/called a RowFilter in jTable, which can make the jTable to show the filtering results.
Then from the results in this filtered view I used the removeRow method from table model to remove one or several selected rows.
The problem is that everytime if I remove a row, the table content refreshs itself as wanted, but the scroll bar jumps back to the beginning.
Does anyone know how to inhibit this jumping and keep the original view of jTable? Because this helps me not to have to scroll back to find the original position where I started the deleting.
Thank u for ur advice and help.
Invoke the table's scrollRectToVisible() method; pass it the Rectangle returned by getCellRect() for the desired row.

Multiline feature in an editable jtable cell

I want to have multiline feature for JTable cells. Wherein the cell automatically expands and shrinks itself when user writes or deletes text in/from cell. I have set columns width, but it doesnt automatically adjust itself according to user's input, rather text beyond the width are not seen at all, but i have a requirement to fix on the width. Can anyone help me with this, to have self adjusting and multiline feature for jtable cell. Would be very thankful.
Thanks in advance.
Several steps:
implement a custom renderer using a JTextArea as rendering component
implement a custom editor using a JTextArea inside a JScrollPane as editing component
add logic (aka: TableModelListener) which updates JTable's individual rowHeight based on the renderer's preferred size on receiving a change notification

Cell editor not exiting when row is deleted

I have a JTable with a several columns, one of which is has a custom renderer to display 3 buttons in a JPanel as well as a custom editor to allow them to be clickable. One of the buttons sends a delete command to our server for that row id then reloads the table data from the server by clearing the data model and loading data again. When this happens the cell with the 3 buttons continues to display (but not the rest of the row) until I click another button in another row even though the row is gone.
I've set putClientProperty("terminateEditOnFocusLost", Boolean.TRUE) but changing focus to another component does not help.
I've tried deleting the row itself before refreshing the data (even deleted all the rows) and made sure to call fireTableRowsDeleted().
I've also tried calling the cancelCellEditing() and stopCellEditing() functions of the TableCellEditor and even manually setting the editing row/column to another cell.
Any help would be greatly appreciated.
Ok, so I figured it out. I ended up calling removeEditor() on the table and that fixed it.
Thanks for the responses.
I've set putClientProperty("terminateEditOnFocusLost", Boolean.TRUE) but changing focus to another component does not help.
That method should be invoked when you create the table, not in the actionPerformed method when you click the button.
Ok, so I figured it out. I ended up calling removeEditor() on the table just before the data refresh and that fixed it.
Thanks for the responses.

Categories

Resources