I'm lost how to do it with a JTable.

This code is not running

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

class PopupListener extends MouseAdapter {
    JPopupMenu m_pmnPopup;
    
    public PopupListener(JPopupMenu pmnPopup) {
        m_pmnPopup = pmnPopup;
    }
    
    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
    }

    void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            m_pmnPopup.show(e.getComponent(),
                        e.getX(), e.getY());
        }
    }
}

class MyTableColumn extends TableColumn {
    public MyTableColumn(int modelIndex) {
        super(modelIndex);
        JPopupMenu pmnPopupHeader = new JPopupMenu();
        pmnPopupHeader.add(new JMenuItem("header1"));
        pmnPopupHeader.add(new JMenuItem("header2"));
        pmnPopupHeader.add(new JMenuItem("header3"));
        // ((Component)getHeaderRenderer()).addMouseListener(new
PopupListener(pmnPopupHeader));

        JPopupMenu pmnPopupCell = new JPopupMenu();
        pmnPopupCell.add(new JMenuItem("cell1"));
        pmnPopupCell.add(new JMenuItem("cell2"));
        pmnPopupCell.add(new JMenuItem("cell3"));
        // ((Component)getCellRenderer()).addMouseListener(new
PopupListener(pmnPopupCell));
    }
}

public class Main {
    static JFrame m_frame;
    static final int NUMBER_OF_COLUMNS = 100;
    public static void main(String[] ARGS){        
        m_frame = new JFrame("Teste");
        m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        m_frame.getContentPane().setLayout(new BorderLayout());
            
        JTable table = new JTable();
        table.setModel(new AbstractTableModel(){
            public int getRowCount() {                    
                return 2047;
            }

            public int getColumnCount() {
                return NUMBER_OF_COLUMNS;
            }
                
            public Object getValueAt(int arg0, int arg1) {
                return new Integer(arg0*arg1);
            }});

        table.setAutoCreateColumnsFromModel(false);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        for (int nColumn = 0; nColumn < NUMBER_OF_COLUMNS; nColumn++) {
            table.addColumn(new MyTableColumn(nColumn));   
        }
                
        JScrollPane scrollPane = new JScrollPane(table);
            
        m_frame.getContentPane().add(scrollPane, BorderLayout.SOUTH);

        JLabel label = new JLabel("XXX");
        JPopupMenu pmnPopup = new JPopupMenu();
        pmnPopup.add(new JMenuItem("item1"));
        pmnPopup.add(new JMenuItem("item2"));
        pmnPopup.add(new JMenuItem("item3"));
        label.addMouseListener(new PopupListener(pmnPopup));

        m_frame.getContentPane().add(label, BorderLayout.NORTH);
            
        m_frame.pack();
        m_frame.setVisible(true);
    }
}

For the JLabel works but for the JTable don't.

In the Cell case I'have to know the Row as well.

Thanks
Marcos

-----Original Message-----
From: Meikel Bisping [mailto:[EMAIL PROTECTED]
Sent: Wednesday, January 28, 2004 1:03 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Re: right click open one menu


Try  JPopupMenu
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html


[EMAIL PROTECTED] schrieb am 28.01.2004:
>I need to create one menu with some options if right click in one
>component.
>
>
>Is this possible?
>
>Thanks
>MArcos
>_______________________________________________
>Swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/swing
>
>
_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to