毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> java技术 >> 正文

JTable怎么给选中行添加监听器

更新时间:2012-11-20:  来源:毕业论文

[swing]需要实现的效果是这样的:当右键单击被选中行时会有个JPopupMenu弹出。右键单击没有选中的行不会弹出。。
监听双击的demo在二楼,谁帮我改改,非常感谢!

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel;       public class JTableDemo extends JFrame{     private JTable tbl;     private DefaultTableModel tm;     private JPopupMenu[] pms = new JPopupMenu[2];     public JTableDemo() {         String[][] data = { {"R1C1", "R1C2"}, {"R2C1", "R2C2"} };         String[] title = {"Column1", "Column2"};         tm = new DefaultTableModel(data, title){             @Override            public boolean isCellEditable(int row, int column){                 return false;             }         };         tbl = new JTable(tm);         tbl.addMouseListener(new MouseAdapter() {             @Override            public void mouseClicked(MouseEvent e) {                 int sr;                 if ((sr = tbl.getSelectedRow()) == -1) {                     return;                 }                 if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {                             pms[sr].show(tbl, e.getX(), e.getY());                 }             }         });           JScrollPane jsp = new JScrollPane();         jsp.setViewportView(tbl);           JPanel pan = new JPanel();         pan.setOpaque(true);         this.setContentPane(pan);         pan.setLayout(new BorderLayout());         pan.add(jsp, BorderLayout.CENTER);           JPopupMenu pm = new JPopupMenu();         pm.add(new JMenuItem("Row1"));         pms[0] = pm;         pm = new JPopupMenu();         pm.add(new JMenuItem("Row2"));         pms[1] = pm;     }       private static void createAndShowGui() {         JTableDemo td = new JTableDemo();         td.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         td.setMinimumSize(new Dimension(500, 400));         td.setLocationRelativeTo(null);         td.setVisible(true);     }     public static void main(String[] args) {         SwingUtilities.invokeLater(new Runnable() {             @Override            public void run() {                 createAndShowGui();             }         });     } }

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel;   public class JTableDemo extends JFrame {     private static final long serialVersionUID = 1L;     private JTable tbl;     private DefaultTableModel tm;     private JPopupMenu[] pms = new JPopupMenu[2];       public JTableDemo() {         String[][] data = { { "R1C1", "R1C2" }, { "R2C1", "R2C2" } };         String[] title = { "Column1", "Column2" };         tm = new DefaultTableModel(data, title) {             private static final long serialVersionUID = 1L;               @Override            public boolean isCellEditable(int row, int column) {                 return false;             }         };         tbl = new JTable(tm);         tbl.addMouseListener(new MouseAdapter() {             @Override            public void mouseClicked(MouseEvent e) {                 int sr; //              if ((sr = tbl.getSelectedRow()) == -1) { //                  return; //              }                 if (e.getButton() == MouseEvent.BUTTON3) {                     pms[tbl.rowAtPoint(e.getPoint())].show(tbl, e.getX(), e.getY());                 }             }         });           JScrollPane jsp = new JScrollPane();         jsp.setViewportView(tbl);           JPanel pan = new JPanel();         pan.setOpaque(true);         this.setContentPane(pan);         pan.setLayout(new BorderLayout());         pan.add(jsp, BorderLayout.CENTER);           JPopupMenu pm = new JPopupMenu();         pm.add(new JMenuItem("Row1"));         pms[0] = pm;         pm = new JPopupMenu();         pm.add(new JMenuItem("Row2"));         pms[1] = pm;     }       private static void createAndShowGui() {         JTableDemo td = new JTableDemo();         td.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         td.setMinimumSize(new Dimension(500, 400));         td.setLocationRelativeTo(null);         td.setVisible(true);     }       public static void main(String[] args) {         SwingUtilities.invokeLater(new Runnable() {             @Override            public void run() {                 createAndShowGui();             }         });     } }

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。