给Tab增加事件

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

1,增加双击事件:

tabbedPane_22.addMouseListener(new MouseAdapter() {
        	private int heightLevel=0;
			@Override
			public void mouseReleased(MouseEvent e) {
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
			}
			
			
			@Override
			public void mouseClicked(MouseEvent e) {
				JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
				int selectedIndex = tabbedPane.getSelectedIndex();
                //当前选择的标签页
                Component selectedTab= tabbedPane.getComponentAt(selectedIndex);
                if(!ValueWidget.isNullOrEmpty(selectedTab)&&jsonFormattedPane==selectedTab){
                	
                	if(e.getClickCount()==2){
    					switch (heightLevel%3) {
						case 0:
							splitPane_out.setDividerLocation(getDividerHeight()[0]);
							break;
						case 1:
							splitPane_out.setDividerLocation(15);
							break;
						case 2:
							splitPane_inner.setDividerLocation(getDividerHeight()[0]);
    						splitPane_out.setDividerLocation(getDividerHeight()[1]);
    						RequestPanel.this.repaint();
							break;
						default:
							break;
						}
    					heightLevel++;
    				}
                	
                }
				
			}
		});

 

2,增加右键菜单

tabbedPane_2.addMouseListener(getMouseInputListener(tabbedPane_2, this));

 监听器:

public static MouseInputListener getMouseInputListener(final JTabbedPane tabbedPane
    		,final AutoTestPanel autoTestPanel) {
    	
        return new MouseInputListener() {
        	public  int count;
            public void mouseClicked(MouseEvent e) {
            	/*if(e.getClickCount()==1){
            		final int rowCount = jTable.getSelectedRow();
                	final int columnCount=jTable.getSelectedColumn();
            		int modifiers = e.getModifiers();
            		modifiers -= MouseEvent.BUTTON3_MASK;
                	modifiers |= MouseEvent.FOCUS_EVENT_MASK;
                	MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(),
    						e.getWhen(), modifiers, e.getX(), e.getY(),
    						2, false);
//                	processEvent(ne);
                	jTable.editCellAt(rowCount, columnCount,ne);
//                	CellEditor cellEditor=jTable.getCellEditor(rowCount, columnCount);
//                	cellEditor.shouldSelectCell(ne);
                	jTable.dispatchEvent(ne);
            	}*/
            	System.out.println("mouseClicked"+(count++));
                processEvent(e);
            	
            }
        	/***
             * in order to trigger Left-click the event
             */
            public void mousePressed(MouseEvent e) {
                processEvent(e);// is necessary!!!
                System.out.println("mousePressed"+(count++));
            }

            public void mouseReleased(MouseEvent e) {
            	
            	RequestPanel currentRequestPanel2=(RequestPanel)tabbedPane.getSelectedComponent();
            	if(ValueWidget.isNullOrEmpty(currentRequestPanel2)){
                	currentRequestPanel2=autoTestPanel.getCurrentRequestPanel();
                }
                if (e.getButton() == MouseEvent.BUTTON3) {// right click
//                	processEvent(e);
                	
					// System.out.println("row:" + rowCount);
                	//右键点击表格时,点击的单元格,正常情况返回的是String
//                	System.out.println("rowCount:"+rowCount);
//                	System.out.println("columnCount:"+columnCount);
					
                	
                    JPopupMenu popupmenu = new JPopupMenu();
                    JMenuItem runM = new JMenuItem(RequestPanel.ACTION_COMMAND_RUN);
                    JMenuItem cleanUp_runM = new JMenuItem("清空后再运行");
                    JMenuItem copyParameterM = new JMenuItem(RequestPanel.ACTION_COMMAND_COPY_REQUEST_PARAMETER);
                    JMenuItem copyResponseM = new JMenuItem(RequestPanel.ACTION_COMMAND_COPY_RESPONSE);
                    JMenuItem copyAccessTokenM = new JMenuItem("复制access_token");
                    JMenuItem cleanUpCellM = new JMenuItem("清空单元格");
                    cleanUpCellM.setActionCommand(MenuUtil2.ACTION_STR_CLEANUP_CELL);
                   
                    JMenuItem sendPostM = new JMenuItem("打开网页发送POST请求");
                    JMenuItem copyPanelM = new JMenuItem("复制请求panel");
                    JMenuItem deleteCurrentPanelM = new JMenuItem("删除当前请求panel");
                    
                    JMenuItem cleanResultM = new JMenuItem("清空结果");
                    JMenuItem copyRequestInfoM=new JMenuItem(RequestPanel.ACTION_COMMAND_COPY_REQUEST_INFO);
                    JMenuItem pasteRequestInfoM=new JMenuItem(RequestPanel.ACTION_COMMAND_PASTE_REQUEST_INFO);
                    
                    MyTableMenuListener yMenuActionListener = new MyTableMenuListener(null,SystemHWUtil.NEGATIVE_ONE
                    		,SystemHWUtil.NEGATIVE_ONE,currentRequestPanel2);
                    runM.addActionListener(yMenuActionListener);
                    cleanUp_runM.addActionListener(yMenuActionListener);
                    copyParameterM.addActionListener(yMenuActionListener);
                    copyResponseM.addActionListener(yMenuActionListener);
                    copyAccessTokenM.addActionListener(yMenuActionListener);
                    cleanResultM.addActionListener(yMenuActionListener);
                    copyRequestInfoM.addActionListener(yMenuActionListener);
                    pasteRequestInfoM.addActionListener(yMenuActionListener);
                    cleanUpCellM.addActionListener(yMenuActionListener);
                    sendPostM.addActionListener(yMenuActionListener);
                    copyPanelM.addActionListener(yMenuActionListener);
                    deleteCurrentPanelM.addActionListener(yMenuActionListener);
                    
                    popupmenu.add(cleanUp_runM);
                    popupmenu.add(runM);
                    popupmenu.add(copyParameterM);
                    popupmenu.add(copyResponseM);
                    popupmenu.add(copyAccessTokenM);
                    popupmenu.add(cleanUpCellM);
                    popupmenu.add(sendPostM);
                    popupmenu.add(copyPanelM);
                    popupmenu.add(deleteCurrentPanelM);
                    popupmenu.add(cleanResultM);
                    popupmenu.add(copyRequestInfoM);
                    popupmenu.add(pasteRequestInfoM);
                    popupmenu.show(e.getComponent(), e.getX(), e.getY());
//                    processEvent(e);
                }/*else if (e.getButton() == MouseEvent.BUTTON1&& e.getClickCount()==1){
                	System.out.println("左键");
                	int modifiers = e.getModifiers();
                	modifiers |= MouseEvent.FOCUS_EVENT_MASK;
                	MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(),
							e.getWhen(), modifiers, e.getX(), e.getY(),
							2, false);
                	
//                	processEvent(ne);
//                	jTable.editCellAt(rowCount, columnCount,ne);
//                	CellEditor cellEditor=jTable.getCellEditor(rowCount, columnCount);
//                	cellEditor.shouldSelectCell(ne);
                	jTable.dispatchEvent(ne);
                }*/
                System.out.println("mouseReleased"+(count++));
            }

            public void mouseEntered(MouseEvent e) {
                processEvent(e);
            }

            public void mouseExited(MouseEvent e) {
                processEvent(e);
            }

            public void mouseDragged(MouseEvent e) {
                processEvent(e);
            }

            public void mouseMoved(MouseEvent e) {
                processEvent(e);
            }

            private void processEvent(MouseEvent e) {
                // Right-click on
                if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
                    // System.out.println(e.getModifiers());
                    // System.out.println("Right-click on");
                    int modifiers = e.getModifiers();
                    modifiers -= MouseEvent.BUTTON3_MASK;
                    modifiers |= MouseEvent.BUTTON1_MASK;
                    MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(),
                            e.getWhen(), modifiers, e.getX(), e.getY(),
                            e.getClickCount(), false);
                    tabbedPane.dispatchEvent(ne);// in order to trigger Left-click
                    // the event
                }
            }
        };
    }

 

 

 

你可能感兴趣的:(JTabbedPane双击,JTabbedPane菜单)