19 July 2016

JPopupMenu Java Tạo menu hiển thị trên Table với Java swing gui Eclipse

Add event Handler -> mouseReleased
Java 2016
table.addMouseListener (new MouseAdapter () {
 
 @Override
 public void mouseReleased (MouseEvent e) {
  int r = table.rowAtPoint (e.getPoint ());
  if (r> = 0 && r <table.getRowCount ()) {
   table.setRowSelectionInterval (r, r);
  } Else {
   table.clearSelection ();
  }
  // Row index is found ...
  int rowIndex = table.getSelectedRow ();
  if (rowIndex <0)
   return;
  if (e.isPopupTrigger () && e.getComponent () instanceof JTable) {
   JPopupMenu popup = createPopUp (rowIndex, table);
   popup.show (e.getComponent (), e.getX (), e.getY ());
  }
 }
});
Method createPopUp()
Phương thức kiểu JPopUpMenu có 2 tham số
Java gui 2016
// JPopupMenu
  public JPopupMenu createPopUp (int rowIndex, JTable table) {
 JPopupMenu JPopupMenu popup = new ();
 JMenuItem delete = new JMenuItem ( "Delete");
 JMenuItem exit = new JMenuItem ( "Exit");
 delete.addActionListener (new ActionListener () {
  @Override
  public void actionPerformed (ActionEvent e) {
   JOptionPane.showMessageDialog (null, "Delete successfuly!");
  }
 });
 exit.addActionListener (new ActionListener () {
  @Override
  public void actionPerformed (ActionEvent e) { 
   JOptionPane.showMessageDialog (null, "Exit");
  }
 });
 popup.add (delete);
 popup.add (exit);
 return popup;
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang