🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
5 messages
5 total messages Started by Erica Wheatcroft Tue, 18 Jan 2005 20:39
JTable & Popup Menu
#685
Author: Erica Wheatcroft
Date: Tue, 18 Jan 2005 20:39
35 lines
1744 bytes
Hi -

I have a JTable that I have created and what to have a popup menu
appear when the user right clicks on a row in the table

so i wrote the following code:

in jninit()

    table.addMouseListener(new MouseAdapter()
   {
     public void mouseReleased(MouseEvent e)
     {
       editTable(e);
     }
   });

...

private void editTable_MouseReleased(MouseEvent e)
  {
    if(e.isPopupTrigger())
    {
      _enumMembersTable.setEditingRow(_enumMembersTable.rowAtPoint(e.getPoint()));
      _membersPopupMenu.show(e.getComponent(), e.getX(), e.getY());
    }
 }

However when i run the app and try to right click on a row the row is
never selected. I have simular code for a jlist and it works great. What am 
i missing?

Thanks in advance.

Erica 
Re: JTable & Popup Menu
#686
Author: Erica Wheatcroft
Date: Tue, 18 Jan 2005 20:42
64 lines
2414 bytes
Sorry the code is wrong it should read the following:

 in jninit()
>
>    table.addMouseListener(new MouseAdapter()
>   {
>     public void mouseReleased(MouseEvent e)
>     {
>       editTable(e);
>     }
>   });
>
> ...
>
> private void editTable(MouseEvent e)
>  {
>    if(e.isPopupTrigger())
>    {
>      table.setEditingRow(table.rowAtPoint(e.getPoint()));
>      popupMenu.show(e.getComponent(), e.getX(), e.getY());
>    }
> }



"Erica Wheatcroft" <erica.wheatcroft@gmail.com> wrote in message 
news:41ed7406$1@newsgroups.borland.com...
> Hi -
>
> I have a JTable that I have created and what to have a popup menu
> appear when the user right clicks on a row in the table
>
> so i wrote the following code:
>
> in jninit()
>
>    table.addMouseListener(new MouseAdapter()
>   {
>     public void mouseReleased(MouseEvent e)
>     {
>       editTable(e);
>     }
>   });
>
> ...
>
> private void editTable_MouseReleased(MouseEvent e)
>  {
>    if(e.isPopupTrigger())
>    {
> 
> _enumMembersTable.setEditingRow(_enumMembersTable.rowAtPoint(e.getPoint()));
>      _membersPopupMenu.show(e.getComponent(), e.getX(), e.getY());
>    }
> }
>
> However when i run the app and try to right click on a row the row is
> never selected. I have simular code for a jlist and it works great. What 
> am i missing?
>
> Thanks in advance.
>
> Erica
> 
Re: JTable & Popup Menu
#687
Author: Erica Wheatcroft
Date: Tue, 18 Jan 2005 22:35
63 lines
2575 bytes
So i changed the function to do the following and it works: but is this they 
"correct" way to implement this behavior?

private void editTable_MouseReleased(MouseEvent e)
  {
    // we only want a to do this if the event is a pop up trigger.
     if(e.isPopupTrigger())
     {
       Point p = e.getPoint();
       int row = _structureMembersTable.rowAtPoint(p);
       int col = _structureMembersTable.columnAtPoint(p);

       if(table.isRowSelected( row ) == false)
       {
         table.setRowSelectionInterval(row,row);
       }
       table.setEditingRow( row );
       popupMenu.show( e.getComponent(), e.getX(), e.getY());
     }
  }




"Erica Wheatcroft" <erica.wheatcroft@gmail.com> wrote in message 
news:41ed7406$1@newsgroups.borland.com...
> Hi -
>
> I have a JTable that I have created and what to have a popup menu
> appear when the user right clicks on a row in the table
>
> so i wrote the following code:
>
> in jninit()
>
>    table.addMouseListener(new MouseAdapter()
>   {
>     public void mouseReleased(MouseEvent e)
>     {
>       editTable(e);
>     }
>   });
>
> ...
>
> private void editTable_MouseReleased(MouseEvent e)
>  {
>    if(e.isPopupTrigger())
>    {
> 
> _enumMembersTable.setEditingRow(_enumMembersTable.rowAtPoint(e.getPoint()));
>      _membersPopupMenu.show(e.getComponent(), e.getX(), e.getY());
>    }
> }
>
> However when i run the app and try to right click on a row the row is
> never selected. I have simular code for a jlist and it works great. What 
> am i missing?
>
> Thanks in advance.
>
> Erica
> 
Re: JTable & Popup Menu
#688
Author: John McGrath [TeamB]
Date: Wed, 19 Jan 2005 18:54
91 lines
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
Re: JTable & Popup Menu
#689
Author: Erica Wheatcroft
Date: Wed, 19 Jan 2005 19:44
97 lines
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 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads