dvigal
dvigal

Reputation: 165

Show a popup menu when a mouse cursor in a cell of JTable

I need such behaviour: When is a mouse cursor pointing to a JTable cell - will need show pop up menu or something like that. Without clicking of the mouse on the cell. Any suggestions?

Upvotes: 0

Views: 1311

Answers (2)

dvigal
dvigal

Reputation: 165

Thank guys for your attention. I need to change data in cell but do it such that there was only one mouse click! One from variants it how i have spoken above. I have pop up menu with the two buttons: "access" and "disallow". And that i came up with such solution ...

public class PopupMenuMouseMotionListener implements MouseMotionListener {
    private JXTreeTable jxTreeTable;
    private TreeTableModel model;
    private JPopupMenu popupMenu;
    private boolean inCell = false;
    private Rectangle r = null;
    private boolean visible = false;
    private long startTime = 0L;
    private Component[] buttons;

    PopupMenuMouseMotionListener(JXTreeTable jxTreeTable, TreeTableModel model, JPopupMenu popupMenu) {
        this.jxTreeTable = jxTreeTable;
        this.model       = model;
        this.popupMenu   = popupMenu;
        this.buttons     = popupMenu.getComponents();
    }

    public void mouseDragged(MouseEvent event) {
        //Does nothing
    }

    public void mouseMoved(MouseEvent event) {
        Point point = event.getPoint();
        int row = jxTreeTable.rowAtPoint(point);
        int column = jxTreeTable.columnAtPoint(point);
        double time = 0.0;
        if (column >= 1) {
            if (startTime == 0L) {
                startTime = System.currentTimeMillis();
            };
            long ms = System.currentTimeMillis() - startTime;
            time = ms / 1000 + ms % 1000; 
            if (r == null) {
                r = jxTreeTable.getCellRect(row, column, false);
            };
            inCell = true;
            Component c = event.getComponent();
            if (inCell && r.contains(point)) {
                if (time >= 100.50 && visible == false) {
                    popupMenu.show(c, r.x, r.y);
                    visible = true;
                }
            } else {
                popupMenu.setVisible(false);
                inCell = false;
                startTime = 0L;
                r = null;
                visible = false;
            }
        } else popupMenu.setVisible(false);
    }
}

What you think about it?

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

JB Nizet has a good point. OTOH, I prepared this example, and 'we have the technology'.

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

class TimesTable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JTable t = new JTable(new TimesTableModel());
                t.setDefaultRenderer(Object.class, new TimesTableRenderer());

                JOptionPane.showMessageDialog(null, t);
            }
        });
    }
}

class TimesTableRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(
        JTable table,
        Object value,
        boolean isSelected,
        boolean hasFocus,
        int row,
        int column) {

        Component c = super.getTableCellRendererComponent(
            table,value,isSelected,hasFocus,row,column);
        JComponent jc = (JComponent)c;
        jc.setToolTipText(
            (row+1) + "x" + (column+1) + "=" + ((row+1)*(column+1)));
        return jc;
    }
}

class TimesTableModel extends DefaultTableModel {
    @Override
    public int getColumnCount() {
        return 7;
    }

    @Override
    public int getRowCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int row, int column) {
        return new Integer((row+1)*(column+1));
    }
}

Upvotes: 3

Related Questions