Article View: borland.public.jbuilder.java.swing
Article #688Re: JTable & Popup Menu
From: John McGrath [TeamB]
Date: Wed, 19 Jan 2005 18:54
Date: Wed, 19 Jan 2005 18:54
91 lines
4321 bytes
4321 bytes
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:
<xn0dxfxyinzd1g400h@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!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>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,3d8097fef9530864 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit From: "John McGrath [TeamB]" <ng-b@JPMcGrath.net> Subject: Re: JTable & Popup Menu Newsgroups: borland.public.jbuilder.java.swing References: <41ed7406$1@newsgroups.borland.com> <41ed8f4b$1@newsgroups.borland.com> User-Agent: XanaNews/1.17.1.2 Message-ID: <xn0dxfxyinzd1g400h@newsgroups.borland.com> X-Ref: newsgroups.borland.com ~XNS:00000147 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 NNTP-Posting-Host: 141.151.11.96 Date: 19 Jan 2005 10:54:44 -0800 X-Trace: newsgroups.borland.com 1106160884 141.151.11.96 (19 Jan 2005 10:54:44 -0800) Lines: 91 X-Authenticated-User: jpmcgrath Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!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