Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2004

Tip: Looking for answers? Try searching our database.

JTable, custom components and event handling

Thread view: 
Rajarshi Guha - 25 May 2004 22:57 GMT
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
Manish Hatwalne - 26 May 2004 06:51 GMT
Haven't read it completely, but I think you need a cell editor along with
the cell renderer.

HTH,
- Manish

> Hi,
>   I have a JTable in which one of the columns will be custom component
[quoted text clipped - 50 lines]
> Thanks
> Rajarshi
minsc - 26 May 2004 11:33 GMT
Hi!

> 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.
Rajarshi Guha - 26 May 2004 13:31 GMT
> Hi!
>
[quoted text clipped - 7 lines]
> 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
minsc - 26 May 2004 18:57 GMT
>>Hi!
>>
[quoted text clipped - 12 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.