给Tab增加事件

tab对应的类是:javax.swing.JTabbedPane

1,增加双击事件:

Java代码   收藏代码
  1. tabbedPane_22.addMouseListener(new MouseAdapter() {  
  2.             private int heightLevel=0;  
  3.             @Override  
  4.             public void mouseReleased(MouseEvent e) {  
  5.             }  
  6.               
  7.             @Override  
  8.             public void mousePressed(MouseEvent e) {  
  9.             }  
  10.               
  11.               
  12.             @Override  
  13.             public void mouseClicked(MouseEvent e) {  
  14.                 JTabbedPane tabbedPane = (JTabbedPane) e.getSource();  
  15.                 int selectedIndex = tabbedPane.getSelectedIndex();  
  16.                 //当前选择的标签页  
  17.                 Component selectedTab= tabbedPane.getComponentAt(selectedIndex);  
  18.                 if(!ValueWidget.isNullOrEmpty(selectedTab)&&jsonFormattedPane==selectedTab){  
  19.                       
  20.                     if(e.getClickCount()==2){  
  21.                         switch (heightLevel%3) {  
  22.                         case 0:  
  23.                             splitPane_out.setDividerLocation(getDividerHeight()[0]);  
  24.                             break;  
  25.                         case 1:  
  26.                             splitPane_out.setDividerLocation(15);  
  27.                             break;  
  28.                         case 2:  
  29.                             splitPane_inner.setDividerLocation(getDividerHeight()[0]);  
  30.                             splitPane_out.setDividerLocation(getDividerHeight()[1]);  
  31.                             RequestPanel.this.repaint();  
  32.                             break;  
  33.                         default:  
  34.                             break;  
  35.                         }  
  36.                         heightLevel++;  
  37.                     }  
  38.                       
  39.                 }  
  40.                   
  41.             }  
  42.         });  

 

2,增加右键菜单

Java代码   收藏代码
  1. tabbedPane_2.addMouseListener(getMouseInputListener(tabbedPane_2, this));  

 监听器:

Java代码   收藏代码
  1. public static MouseInputListener getMouseInputListener(final JTabbedPane tabbedPane  
  2.             ,final AutoTestPanel autoTestPanel) {  
  3.           
  4.         return new MouseInputListener() {  
  5.             public  int count;  
  6.             public void mouseClicked(MouseEvent e) {  
  7.                 /*if(e.getClickCount()==1){ 
  8.                     final int rowCount = jTable.getSelectedRow(); 
  9.                     final int columnCount=jTable.getSelectedColumn(); 
  10.                     int modifiers = e.getModifiers(); 
  11.                     modifiers -= MouseEvent.BUTTON3_MASK; 
  12.                     modifiers |= MouseEvent.FOCUS_EVENT_MASK; 
  13.                     MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(), 
  14.                             e.getWhen(), modifiers, e.getX(), e.getY(), 
  15.                             2, false); 
  16. //                  processEvent(ne); 
  17.                     jTable.editCellAt(rowCount, columnCount,ne); 
  18. //                  CellEditor cellEditor=jTable.getCellEditor(rowCount, columnCount); 
  19. //                  cellEditor.shouldSelectCell(ne); 
  20.                     jTable.dispatchEvent(ne); 
  21.                 }*/  
  22.                 System.out.println("mouseClicked"+(count++));  
  23.                 processEvent(e);  
  24.                   
  25.             }  
  26.             /*** 
  27.              * in order to trigger Left-click the event 
  28.              */  
  29.             public void mousePressed(MouseEvent e) {  
  30.                 processEvent(e);// is necessary!!!  
  31.                 System.out.println("mousePressed"+(count++));  
  32.             }  
  33.   
  34.             public void mouseReleased(MouseEvent e) {  
  35.                   
  36.                 RequestPanel currentRequestPanel2=(RequestPanel)tabbedPane.getSelectedComponent();  
  37.                 if(ValueWidget.isNullOrEmpty(currentRequestPanel2)){  
  38.                     currentRequestPanel2=autoTestPanel.getCurrentRequestPanel();  
  39.                 }  
  40.                 if (e.getButton() == MouseEvent.BUTTON3) {// right click  
  41. //                  processEvent(e);  
  42.                       
  43.                     // System.out.println("row:" + rowCount);  
  44.                     //右键点击表格时,点击的单元格,正常情况返回的是String  
  45. //                  System.out.println("rowCount:"+rowCount);  
  46. //                  System.out.println("columnCount:"+columnCount);  
  47.                       
  48.                       
  49.                     JPopupMenu popupmenu = new JPopupMenu();  
  50.                     JMenuItem runM = new JMenuItem(RequestPanel.ACTION_COMMAND_RUN);  
  51.                     JMenuItem cleanUp_runM = new JMenuItem("清空后再运行");  
  52.                     JMenuItem copyParameterM = new JMenuItem(RequestPanel.ACTION_COMMAND_COPY_REQUEST_PARAMETER);  
  53.                     JMenuItem copyResponseM = new JMenuItem(RequestPanel.ACTION_COMMAND_COPY_RESPONSE);  
  54.                     JMenuItem copyAccessTokenM = new JMenuItem("复制access_token");  
  55.                     JMenuItem cleanUpCellM = new JMenuItem("清空单元格");  
  56.                     cleanUpCellM.setActionCommand(MenuUtil2.ACTION_STR_CLEANUP_CELL);  
  57.                      
  58.                     JMenuItem sendPostM = new JMenuItem("打开网页发送POST请求");  
  59.                     JMenuItem copyPanelM = new JMenuItem("复制请求panel");  
  60.                     JMenuItem deleteCurrentPanelM = new JMenuItem("删除当前请求panel");  
  61.                       
  62.                     JMenuItem cleanResultM = new JMenuItem("清空结果");  
  63.                     JMenuItem copyRequestInfoM=new JMenuItem(RequestPanel.ACTION_COMMAND_COPY_REQUEST_INFO);  
  64.                     JMenuItem pasteRequestInfoM=new JMenuItem(RequestPanel.ACTION_COMMAND_PASTE_REQUEST_INFO);  
  65.                       
  66.                     MyTableMenuListener yMenuActionListener = new MyTableMenuListener(null,SystemHWUtil.NEGATIVE_ONE  
  67.                             ,SystemHWUtil.NEGATIVE_ONE,currentRequestPanel2);  
  68.                     runM.addActionListener(yMenuActionListener);  
  69.                     cleanUp_runM.addActionListener(yMenuActionListener);  
  70.                     copyParameterM.addActionListener(yMenuActionListener);  
  71.                     copyResponseM.addActionListener(yMenuActionListener);  
  72.                     copyAccessTokenM.addActionListener(yMenuActionListener);  
  73.                     cleanResultM.addActionListener(yMenuActionListener);  
  74.                     copyRequestInfoM.addActionListener(yMenuActionListener);  
  75.                     pasteRequestInfoM.addActionListener(yMenuActionListener);  
  76.                     cleanUpCellM.addActionListener(yMenuActionListener);  
  77.                     sendPostM.addActionListener(yMenuActionListener);  
  78.                     copyPanelM.addActionListener(yMenuActionListener);  
  79.                     deleteCurrentPanelM.addActionListener(yMenuActionListener);  
  80.                       
  81.                     popupmenu.add(cleanUp_runM);  
  82.                     popupmenu.add(runM);  
  83.                     popupmenu.add(copyParameterM);  
  84.                     popupmenu.add(copyResponseM);  
  85.                     popupmenu.add(copyAccessTokenM);  
  86.                     popupmenu.add(cleanUpCellM);  
  87.                     popupmenu.add(sendPostM);  
  88.                     popupmenu.add(copyPanelM);  
  89.                     popupmenu.add(deleteCurrentPanelM);  
  90.                     popupmenu.add(cleanResultM);  
  91.                     popupmenu.add(copyRequestInfoM);  
  92.                     popupmenu.add(pasteRequestInfoM);  
  93.                     popupmenu.show(e.getComponent(), e.getX(), e.getY());  
  94. //                    processEvent(e);  
  95.                 }/*else if (e.getButton() == MouseEvent.BUTTON1&& e.getClickCount()==1){ 
  96.                     System.out.println("左键"); 
  97.                     int modifiers = e.getModifiers(); 
  98.                     modifiers |= MouseEvent.FOCUS_EVENT_MASK; 
  99.                     MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(), 
  100.                             e.getWhen(), modifiers, e.getX(), e.getY(), 
  101.                             2, false); 
  102.                      
  103. //                  processEvent(ne); 
  104. //                  jTable.editCellAt(rowCount, columnCount,ne); 
  105. //                  CellEditor cellEditor=jTable.getCellEditor(rowCount, columnCount); 
  106. //                  cellEditor.shouldSelectCell(ne); 
  107.                     jTable.dispatchEvent(ne); 
  108.                 }*/  
  109.                 System.out.println("mouseReleased"+(count++));  
  110.             }  
  111.   
  112.             public void mouseEntered(MouseEvent e) {  
  113.                 processEvent(e);  
  114.             }  
  115.   
  116.             public void mouseExited(MouseEvent e) {  
  117.                 processEvent(e);  
  118.             }  
  119.   
  120.             public void mouseDragged(MouseEvent e) {  
  121.                 processEvent(e);  
  122.             }  
  123.   
  124.             public void mouseMoved(MouseEvent e) {  
  125.                 processEvent(e);  
  126.             }  
  127.   
  128.             private void processEvent(MouseEvent e) {  
  129.                 // Right-click on  
  130.                 if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {  
  131.                     // System.out.println(e.getModifiers());  
  132.                     // System.out.println("Right-click on");  
  133.                     int modifiers = e.getModifiers();  
  134.                     modifiers -= MouseEvent.BUTTON3_MASK;  
  135.                     modifiers |= MouseEvent.BUTTON1_MASK;  
  136.                     MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(),  
  137.                             e.getWhen(), modifiers, e.getX(), e.getY(),  
  138.                             e.getClickCount(), false);  
  139.                     tabbedPane.dispatchEvent(ne);// in order to trigger Left-click  
  140.                     // the event  
  141.                 }  
  142.             }  
  143.         };  
  144.     }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值