JTable的右击弹出菜单试验


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class TBTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(400,300);
        String[][] data = new String[][]{{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}, {"10", "11", "12"}};
        String[] names = new String[]{"数值1", "数值2", "数值3"};
        final JTable table = new JTable(data, names);
        
       // DefaultCellEditor cellEdit = new DefaultCellEditor(new JTextField());
        //cellEdit.setClickCountToStart(2);//双击后使选择的格子可编辑
        table.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                if (SwingUtilities.isRightMouseButton(me)) {
                  final int row = table.rowAtPoint(me.getPoint());
                    System.out.println("row:"+row);
                    if(row!=-1){
                    final int column = table.columnAtPoint(me.getPoint());
                    
                    final JPopupMenu popup = new JPopupMenu();
                      JMenuItem select = new JMenuItem("选择");
                      popup.add(select);
                      popup.add(new JSeparator());
                      JMenuItem edit = new JMenuItem("编辑");
                      popup.add(edit);
 JMenuItem calcel = new JMenuItem("取消");
 popup.add(new JSeparator());
                      popup.add(calcel);


                   select.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                     System.out.println("选择");
 
                     table.setRowSelectionInterval(row, row); //高亮选择指定的行
                    }
                   });
                  
                   edit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                     System.out.println("编辑");
                     table.clearSelection(); //清除高亮选择状态
                     table.editCellAt(row, column); //设置某列为可编辑
                    }
                   });
                  
                   calcel.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                     System.out.println("取消");
                     popup.setVisible(false);
                    }
                   });
                 
                 popup.show(me.getComponent(), me.getX(), me.getY());
                    }
                }
            }
        });
        JScrollPane scrollPane = new JScrollPane(table);
        frame.setTitle("JTable右键测试");
        frame.add(scrollPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值