如何通过鼠标右键单击获取JTable单元格的值

时间:2015-04-17 15:56:01

标签: java swing jtable right-click

我的Java Swing应用程序中有JTable,一切正常。我在MouseListener上添加了JTable,所以每当我尝试右键单击表格中的一行时,我都可以捕获事件并运行一个方法。但是,我想做的是

  • 选择单元格
  • 获取它的价值
  • 然后调用方法。

要做到这一点,我现在必须左键单击该行,然后右键单击它。只需单击鼠标右键,是否可以立即选择行/单元格?

到目前为止,这是我的代码:

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);
    }
}

1 个答案:

答案 0 :(得分:2)

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);
    }
});

编辑:

在学习新概念时创建一个简单的例子。例如:

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();
            }
        });
    }
}