java swing jtable 右击_java swing 给Table增加右键菜单

给Table增加右键菜单

private MouseInputListener getMouseInputListener(final JTable jTable) {

return new MouseInputListener() {

public void mouseClicked(MouseEvent e) {

processEvent(e);

}

/***

* //in order to trigger Left-click the event

*/

public void mousePressed(MouseEvent e) {

processEvent(e);// is necessary!!!

}

public void mouseReleased(MouseEvent e) {

// processEvent(e);

if (e.getButton() == MouseEvent.BUTTON3) {// right click

JPopupMenu popupmenu = new JPopupMenu();

JMenuItem runM = new JMenuItem(ACTION_COMMAND_RUN);

JMenuItem copyParameterM = new JMenuItem(ACTION_COMMAND_COPY_REQUEST_PARAMETER);

JMenuItem copyResponseM = new JMenuItem(ACTION_COMMAND_COPY_RESPONSE);

//JMenuItem encodingM = new JMenuItem(ACTION_COMMAND_ENCODING);

// JMenuItem editM=new JMenuItem("edit");

MyMenuActionListener yMenuActionListener = new MyMenuActionListener();

runM.addActionListener(yMenuActionListener);

copyParameterM.addActionListener(yMenuActionListener);

copyResponseM.addActionListener(yMenuActionListener);

//encodingM.addActionListener(yMenuActionListener);

popupmenu.add(runM);

popupmenu.add(copyParameterM);

popupmenu.add(copyResponseM);

//popupmenu.add(encodingM);

popupmenu.show(e.getComponent(), e.getX(), e.getY());

}

}

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);

jTable.dispatchEvent(ne);// in order to trigger Left-click

// the event

}

}

};

}

class MyMenuActionListener implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

//System.out.println(command);

if (command.equalsIgnoreCase(ACTION_COMMAND_RUN)) {

System.out.println("run");

requestAction();

} else if (command.equalsIgnoreCase(ACTION_COMMAND_COPY_REQUEST_PARAMETER)) {

System.out.println("获取请求要素");

String requestBody = requestBodyDataTA.getText();

if (!ValueWidget.isNullOrEmpty(requestBody)) {

WindowUtil.setSysClipboardText(requestBody);

}

} else if (command.equalsIgnoreCase(ACTION_COMMAND_COPY_RESPONSE)) {

System.out.println("获取应答");

requestAction();

if (!ValueWidget.isNullOrEmpty(responseJsonResult)) {

WindowUtil.setSysClipboardText(responseJsonResult);

}

}

}

}

?调用:

final MouseInputListener mouseInputListener = getMouseInputListener(parameterTable_1);//

parameterTable_1.addMouseListener(mouseInputListener);

?

java swing jtable 右击_java swing 给Table增加右键菜单_第1张图片

?

说明:parameterTable_1的类型是:JTable

?

原文:http://hw1287789687.iteye.com/blog/2230780

你可能感兴趣的:(java,swing,jtable,右击)