我有一個JPopUpMenu添加到JTables中。我可以從彈出式管理中獲得我點擊過的表格。當我點擊菜單項時,如何將錶行傳遞給ItemMenu的ActionListener?如何將彈出菜單中的數據傳遞給actionlistener
mycode的:
package it.rex.view;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import it.rex.model.Interventi;
public class InterventiView extends JFrame {
private JPanel contentPane;
private JTable table;
private Interventi interventi;
public InterventiView(Interventi i) {
this.interventi = i;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(28, 56, 422, 153);
contentPane.add(scrollPane);
table = new JTable();
DefaultTableModel dtm = new DefaultTableModel(0, 0);
// add header of the table
String header[] = new String[] { "Id", "Descrizione", "Stato", "Data inizio", "Tipo", "Responsabile" };
// add header in table model
dtm.setColumnIdentifiers(header);
//set model into the table object
table.setModel(dtm);
// add row dynamically into the table
// for now only dummy data
for (int count = 1; count <= 3; count++) {
dtm.addRow(new Object[] { Integer.toString(count), "data", "data",
"data", "data", "data" });
}
table.getColumnModel().getColumn(2).setResizable(false);
scrollPane.setViewportView(table);
// prepare PopupMenu
JPopupMenu popupMenu = new JPopupMenu();
addPopup(table, popupMenu);
JMenuItem mntmAggiungiIntervento = new JMenuItem("Aggiungi Intervento");
popupMenu.add(mntmAggiungiIntervento);
JMenuItem mntmModificaIntervento = new JMenuItem("Modifica Intervento");
popupMenu.add(mntmModificaIntervento);
JSeparator separator = new JSeparator();
popupMenu.add(separator);
JMenuItem mntmEliminaIntervento = new JMenuItem("Elimina Intervento");
popupMenu.add(mntmEliminaIntervento);
JButton btnOk = new JButton("Ok");
btnOk.setBounds(262, 220, 89, 23);
contentPane.add(btnOk);
JButton btnAnnulla = new JButton("Annulla");
btnAnnulla.setBounds(361, 220, 89, 23);
contentPane.add(btnAnnulla);
//Manage action over popupMenu
mntmAggiungiIntervento.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Which row on JTable was selected?
System.out.println("Add " + arg0.getActionCommand());
}
});
mntmEliminaIntervento.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Which row on JTable was selected?
System.out.println("Delete");
}
});
mntmModificaIntervento.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Which row on JTable was selected?
System.out.println("Modify");
}
});
}
// Mouse events
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
// Identifico la riga
JTable table = ((JTable) component);
int tRaw = table.rowAtPoint(e.getPoint());
if (tRaw >= 0 && tRaw < table.getRowCount()) {
table.setRowSelectionInterval(tRaw, tRaw);
} else {
table.clearSelection();
}
System.out.println("Row: " + Integer.toString(tRaw+1));
}
});
}
}
看看[如何使用動作](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) ,基本上我會創建一個或多個具有對錶格的引用並知道觸發時該做什麼的操作。我還會考慮看一看'JComponent#setComponentPopupMenu',它將允許它控制並確定何時顯示彈出框 – MadProgrammer
避免使用'null'佈局,像素完美的佈局是現代UI設計中的一種幻想。影響組件的個體大小的因素太多,其中沒有一個可以控制。Swing旨在與佈局經理一起工作,放棄這些將導致無法結束的問題和問題,您將花費越來越多的時間來嘗試糾正。您可能想看看[爲什麼在SWING中使用空佈局不滿?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use- a-null-layout-in-swing)以獲得更多信息 – MadProgrammer
[作爲在彈出菜單中使用'Action'的示例](http://stackoverflow.com/questions/22622973/jtable-copy-and-paste-using -clipboard-and-abstract action/22623240#22623240) – MadProgrammer