R
Rajarshi Guha
Hi,
I have a JTable in which one of the columns will be custom component
which is a subclass of JPanel. This custom component can handle mouse
events.
I implemented a TableCellRenderer and the final table shows my custom
component. However I want the component to be able to handle mosue
movement and clicks. Thus I did the following:
1) disable row and column selections on the JTable
2) Added a MouseListener via addMouseListener()
Now, the MouseListener function forwards the event to the component in
which the cursor is currently present and dispatches the event to the
component itself:
static class JmolPanelJTableMouseListener implements MouseListener {
private JTable _table;
public JmolPanelJTableMouseListener(JTable table) {
_table = table;
}
private void forwardEvent(MouseEvent e) {
TableColumnModel columnModel = _table.getColumnModel();
int column = columnModel.getColumnIndexAtX(e.getX());
int row = e.getY() / structureCellHeight;
Object value;
JmolPanel jmp;
MouseEvent ev;
if (column != 0) return;
System.out.println(row);
value = _table.getValueAt(row, column);
if(!(value instanceof JmolPanel))
return;
jmp = (JmolPanel)value;
ev = (MouseEvent)SwingUtilities.convertMouseEvent(_table, e, jmp);
jmp.dispatchEvent(ev);
}
However the component does not appear to recieve those events (it displays
a molecular structure and clicking and dragging the mouse should rotate
the structure)
Is this the correct way to forward events from a JTable to a specific
component in the table?
If not - could somebody point me to the correct way to handle this?
Thanks
Rajarshi
I have a JTable in which one of the columns will be custom component
which is a subclass of JPanel. This custom component can handle mouse
events.
I implemented a TableCellRenderer and the final table shows my custom
component. However I want the component to be able to handle mosue
movement and clicks. Thus I did the following:
1) disable row and column selections on the JTable
2) Added a MouseListener via addMouseListener()
Now, the MouseListener function forwards the event to the component in
which the cursor is currently present and dispatches the event to the
component itself:
static class JmolPanelJTableMouseListener implements MouseListener {
private JTable _table;
public JmolPanelJTableMouseListener(JTable table) {
_table = table;
}
private void forwardEvent(MouseEvent e) {
TableColumnModel columnModel = _table.getColumnModel();
int column = columnModel.getColumnIndexAtX(e.getX());
int row = e.getY() / structureCellHeight;
Object value;
JmolPanel jmp;
MouseEvent ev;
if (column != 0) return;
System.out.println(row);
value = _table.getValueAt(row, column);
if(!(value instanceof JmolPanel))
return;
jmp = (JmolPanel)value;
ev = (MouseEvent)SwingUtilities.convertMouseEvent(_table, e, jmp);
jmp.dispatchEvent(ev);
}
However the component does not appear to recieve those events (it displays
a molecular structure and clicking and dragging the mouse should rotate
the structure)
Is this the correct way to forward events from a JTable to a specific
component in the table?
If not - could somebody point me to the correct way to handle this?
Thanks
Rajarshi