Article View: borland.public.jbuilder.java.swing
Article #689Re: JTable & Popup Menu
From: Erica Wheatcroft
Date: Wed, 19 Jan 2005 19:44
Date: Wed, 19 Jan 2005 19:44
97 lines
4732 bytes
4732 bytes
Thank you so much.
Erica
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message
news:xn0dxfxyinzd1g400h@newsgroups.borland.com...
> On 1/18/2005 at 5:35:55 PM, Erica Wheatcroft wrote:
>
>> So i changed the function to do the following and it works: but is this
>> they "correct" way to implement this behavior?
>
> That is pretty close to the way I have always done it. However, I do have
> a few comments:
>
>> private void editTable_MouseReleased(MouseEvent e)
>> {
>> // we only want a to do this if the event is a pop up trigger.
>> if(e.isPopupTrigger())
>> {
>
> I noticed that in your original code, you did this in a separate method
> that you called from the mouseReleased() handler, but here you have the
> code in-line. I think you want to put it in a separate method that you
> call from *both* the mousePressed() and mouseReleased() handlers. The
> reason for this is that the "popup trigger" event is not the same for all
> platforms, although it is always on mousePressed() or mouseReleased(). So
> if you run the code above on a platform where mousePressed() is the popup
> trigger (such as a Macintosh, I believe), your popup will not be shown.
>
>> int row = _structureMembersTable.rowAtPoint(p);
>
> Note that it is possible for the rowAtPoint() method to return -1, if the
> mouse is not pointing to a valid row, so you should check the return
> value.
>
>> int col = _structureMembersTable.columnAtPoint(p);
>
> Minor detail: You compute the column, but do not use it.
>
>> if(table.isRowSelected( row ) == false)
>> {
>> table.setRowSelectionInterval(row,row);
>> }
>
> Assuming the table is set up with single row selection, this should work,
> although there is really no reason to test for row selection first. If
> you just use:
>
> table.setRowSelectionInterval( row, row );
>
> there is less code and it is clear that a row will always be selected.
>
> If the table is set up with multiple row selection, you would probably
> want to change this to:
>
> table.addRowSelectionInterval( row, row );
>
>> table.setEditingRow( row );
>
> I do not think this belongs here. The "editingRow" and "editingColumn"
> properties are supposed to indicate which cell is currently being edited
> by a TableCellEditor. Since that is not happening, these properties
> should be left alone.
>
> In fact, I am not at all certain why there are public setters for these
> properties. If you look at the source, you will see that they only
> control which TableModel "cell" receives the value from the
> TableCellEditor.
>
> In summary, here is a complete example that I wrote a while back:
>
> public void mousePressed( MouseEvent event ) {
> handlePopup( event );
> }
>
> public void mouseReleased( MouseEvent event ) {
> handlePopup( event );
> }
>
> private void handlePopup( MouseEvent event ) {
> if ( event.isPopupTrigger() ) {
> int row = rowAtPoint( event.getPoint() );
> if ( row >= 0 ) {
> setRowSelectionInterval( row, row );
> JPopupMenu popup = getContextMenu();
> popup.show( event.getComponent(), event.getX(), event.getY() );
> }
> }
> }
>
> --
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
> http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<41eeb895$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.zanker.org!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<41ed7406$1@newsgroups.borland.com> <41ed8f4b$1@newsgroups.borland.com> <xn0dxfxyinzd1g400h@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,3d8097fef9530864 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Erica Wheatcroft" <erica.wheatcroft@gmail.com> From: "Erica Wheatcroft" <erica.wheatcroft@gmail.com> Newsgroups: borland.public.jbuilder.java.swing References: <41ed7406$1@newsgroups.borland.com> <41ed8f4b$1@newsgroups.borland.com> <xn0dxfxyinzd1g400h@newsgroups.borland.com> Subject: Re: JTable & Popup Menu Date: Wed, 19 Jan 2005 13:44:20 -0600 Lines: 99 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 206.127.10.170 Message-ID: <41eeb895$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1106163861 206.127.10.170 (19 Jan 2005 11:44:21 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.zanker.org!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail