JTable, custom components and event handling

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
 
M

Manish Hatwalne

Haven't read it completely, but I think you need a cell editor along with
the cell renderer.

HTH,
- Manish
 
M

minsc

Hi!

Manish said:
Haven't read it completely, but I think you need a cell editor along with
the cell renderer.
If I understand the problem correctly, the problem is for the JTable to
forward _all_ mouse events to the cell. To invoke an editor, you still
need to doubleclick on a cell, which is hardcoded as far as I know..

I'm afraid you'd need to create an extension of JTable in order to
override this behaviour. If a better solution should arise, please share
it with me/us, cuz I could use it somewhere too :p

Greetings,
Pieter Bonne.
 
R

Rajarshi Guha

Hi!


If I understand the problem correctly, the problem is for the JTable to
forward _all_ mouse events to the cell. To invoke an editor, you still
need to doubleclick on a cell, which is hardcoded as far as I know..

I'm afraid you'd need to create an extension of JTable in order to
override this behaviour. If a better solution should arise, please share
it with me/us, cuz I could use it somewhere too :p

Thanks for the pointer - I implemented a TableCellEditor and it appears
that I dont need to double click, a single click on the cell results in my
component getting all subsequent mouse events
 
M

minsc

Rajarshi said:
Thanks for the pointer - I implemented a TableCellEditor and it appears
that I dont need to double click, a single click on the cell results in my
component getting all subsequent mouse events

That's just great! Thanks for telling me, I really thought it only
appeared on doubleclick.. Thanks for trying it :)

Greetz,
Pieter Bonne.
 
Joined
Jun 22, 2010
Messages
1
Reaction score
0
Rajarshi,
I am trying Swing newly, i am interested in this solution. I am want to see how you accomplished this. Do you have the solution so i can get an idea.
Thanks

Mallinarc

Rajarshi Guha said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top