JTable Right-Click popupmenu in Netbeans

15,270

Solution 1

Why do you rely on an IDE to generate code for you? What happens when you move to a different IDE and you have to learn how to do it for that ide? Learn how to write your own code then the IDE doesn't matter:

table.addMouseListener( new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

Solution 2

Hopefully I can answer it for Netbeans... and I hope this helps someone

  1. adding a popupmenu to the form (it goes in Other Components) call it jPopupMenu for example
  2. adding menuitems to the popupmenu
  3. go to properites of JTable (call it jTableDataOrSomething for example)
  4. click binding tab (or right click on jTable Bind > elements)
  5. set ComponentPopupMenu value to my called jPopupMenu

    Next steps,

  6. while in properties select Events and goto mouseReleased set it to your jTableDataOrSomething (or right click on table, Events > Mouse > mouseReleased)

    Netbeans creates an empty function and set the following code

    private void jTableDataOrSomethingMouseReleased(java.awt.event.MouseEvent evt) {
    if (evt.isPopupTrigger())
    {
        JTable source = (JTable)evt.getSource();
        int row = source.rowAtPoint( evt.getPoint() );
        int column = source.columnAtPoint( evt.getPoint() );
    
        if (!source.isRowSelected(row)) {
            source.changeSelection(row, column, false, false);
        }
        jPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
    }
    }
    
  7. create the menuitem action performed for EACH menu item

    Then in there you can use:

        int[] rows = jTableDataOrSomething.getSelectedRows();
        for (int row : rows) {
            boolean j = true;
            try {
                modelRow = jTableDataOrSomething.convertRowIndexToModel(row);
                //do something with the selected rows...
    

This takes multirow selection and considers sorting/filtering.

Finish the function with

    jTableDataOrSomething.repaint();

Enjoy

Share:
15,270

Related videos on Youtube

DRJTower
Author by

DRJTower

Updated on April 27, 2022

Comments

  • DRJTower
    DRJTower about 2 years

    I want to add a right click popupmenu to a JTable in NetBeans IDE (seems like a simple task... lol)

    I got it to partly work by

    1. adding a popupmenu to the form
    2. adding menuitems to the popupmenu
    3. go to properites of JTable
    4. click binding tab
    5. set ComponentPopupMenu value to my popupmenu

    But this only partly works. Now I when I right click on the Table the menu pops up, but the selected row in the JTable does not change. So in when the menuitem's actionPerformed is called I have no idea what row in the JTable was clicked on.

    How can I get this? or is there an easier way to do this in netbeans?

    I know there are others ways of doing this (in code), but I would prefer to use netbeans GUI builder.

    Has anyone ever done this before?

    Thanks for your help!

  • DRJTower
    DRJTower almost 14 years
    For almost anything except GUI's I would agree with you, but designing an interface in code is always going to take longer than designing the same interface by dragging dropping. Thanks for your code example though, these two lines actually solved my problem. JTable source = (JTable)e.getSource(); int row = source.rowAtPoint( e.getPoint() ); I did not know you could get a row from a point.