为了保住LZ的蛋蛋,帮忙用Netbeans写了个例子。重点看下监听方法jTree1MousePressed,其他的代码都是拖拖拽拽出来的/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * NewJFrame.java * * Created on 2010-6-12, 20:54:26 */package study;import java.awt.event.MouseEvent; import javax.swing.tree.TreePath;/** * * @author Administrator */ public class NewJFrame extends javax.swing.JFrame { /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPopupMenu1 = new javax.swing.JPopupMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jScrollPane1 = new javax.swing.JScrollPane(); jTree1 = new javax.swing.JTree(); jMenuItem1.setText("测试"); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jPopupMenu1.add(jMenuItem1); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTree1.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { jTree1MousePressed(evt); } }); jScrollPane1.setViewportView(jTree1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE) ); pack(); }// </editor-fold> private void jTree1MousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here: if (evt.getButton() == MouseEvent.BUTTON3) { TreePath path = jTree1.getClosestPathForLocation(evt.getX(), evt.getY()); jTree1.setSelectionPath(path); jPopupMenu1.show(jTree1, evt.getX(), evt.getY()); } else { jPopupMenu1.setVisible(false); } } private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JMenuItem jMenuItem1; private javax.swing.JPopupMenu jPopupMenu1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTree jTree1; // End of variables declaration}
郁闷 这问题蛋疼纠结了几天 让节点得到焦点也不行
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//*
* NewJFrame.java
*
* Created on 2010-6-12, 20:54:26
*/package study;import java.awt.event.MouseEvent;
import javax.swing.tree.TreePath;/**
*
* @author Administrator
*/
public class NewJFrame extends javax.swing.JFrame { /** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jPopupMenu1 = new javax.swing.JPopupMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree(); jMenuItem1.setText("测试");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jPopupMenu1.add(jMenuItem1); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jTree1MousePressed(evt);
}
});
jScrollPane1.setViewportView(jTree1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
); pack();
}// </editor-fold> private void jTree1MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if (evt.getButton() == MouseEvent.BUTTON3)
{
TreePath path = jTree1.getClosestPathForLocation(evt.getX(), evt.getY());
jTree1.setSelectionPath(path);
jPopupMenu1.show(jTree1, evt.getX(), evt.getY());
}
else
{
jPopupMenu1.setVisible(false);
}
} private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
} // Variables declaration - do not modify
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration}