1

I have a JTable in my Java Swing application and everything works fine in general. I have added a MouseListener on my JTable, so whenever I try right clicking on a row in the table, I can capture the event and run a method. However, what I'd like to do is to

  • Select the cell
  • Get it's value
  • Then call the method.

To do this, I must currently left click on the row, then also do a right click on it. Is it possible to select the row/cell right away with only a single click of the right mouse button?

Here is my code so far:

public class MyMouseAdapterTableArticoli extends MouseAdapter {
    public void mouseClicked(MouseEvent me) {
        JTable t = (JTable)me.getSource();
        JMenuItem menuItem;
        rowPopUp = t.rowAtPoint(me.getPoint());
        if ((me.getClickCount() == 2) && (me.getButton() == MouseEvent.BUTTON1)) {
            pulisciTableArtRappre();
            if((listaMagazzino != null) && (listaMagazzino.size() > 0)) {
                pulisciTableArtMagazzino();
            }
            popolaCampi(rowPopUp);
        } else if (me.getButton() == MouseEvent.BUTTON3) {
            JPopupMenu popup = new JPopupMenu();
            menuItem = new JMenuItem("Mostra Prezzi di Acquisto");
            menuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mostraPrezziAcquisto(rowPopUp); 
                }//fine metodoVoid
            });//fine actionlistener 
            popup.add(menuItem);

            MouseListener popupListener = new PopupListener(popup);
            table.addMouseListener(popupListener);

        }//fine else e if
    }

public void mousePressed(MouseEvent e) {
    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);
    }
}
Gilsha
  • 14,431
  • 3
  • 32
  • 47
bircastri
  • 2,169
  • 13
  • 50
  • 119

1 Answers1

3
table.addMouseListener( new MouseAdapter()
{
    public void mousePressed(MouseEvent e)
    {
        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);
    }
});

Edit:

Create a simple example when learning a new concept. For example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        JTable table = new JTable(15, 5);
        add( new JScrollPane(table) );

        table.addMouseListener( new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                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);
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE(), BorderLayout.NORTH);
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • [maybe getComponentPopupMenu is right and correct way rather than, pieces by Jeanette in my question](http://stackoverflow.com/a/7427779/714968) – mKorbel Apr 17 '15 at 19:03
  • @mKorbel, I prefer using listeners, instead of extending classes with custom code. This code could be implemented as a separate class then you could add to any JTable you wish. – camickr Apr 17 '15 at 19:23
  • I have insert your code in my code but, the prbolem is the same. If I try to do right click on my table, now tha row is selected and it is ok, but this code: table.rowAtPoint(me.getPoint()); the result is not ok. For access to wor, I must do left click on the column and then right click. – bircastri Apr 17 '15 at 21:27
  • @bircastri, you need to add the MouseListener to the table when you create the table. See edit. – camickr Apr 17 '15 at 23:57
  • I add MouseListener in this mode: table.addMouseListener(new MyMouseAdapterTableArticoli()); – bircastri Apr 18 '15 at 10:44
  • I gave you a working example. Did you download the example and test it? Did it work? So I proved the concept works, so it is up to you to add the code in your program. I don't know the structure of your program so I can't tell you exactly where to put the code. – camickr Apr 18 '15 at 14:00