我正在做一个简单的JTable,当我双击表时(取决于索引),我创建了一个mouselistener来响应。我一切正常,问题是我不能调用我的mouselistener之外的方法。这是代码:
public class PendingAction extends AccountAbstractAction{
private class TransferTableModel extends AbstractTableModel {
private static final long serialVersionUID = 2497950520925208080L;
private List<Transfer> transfers;
public TransferTableModel(List<Transfer> transfers) {
this.transfers = new ArrayList<>(transfers);
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public String getColumnName(int column) {
String key = null;
switch (column) {
case 0:
key = "account.number";
break;
case 1:
key = "destination.account.number";
break;
case 2:
key = "amount";
break;
default:
assert false;
break;
}
return textManager.getText(key);
}
@Override
public int getRowCount() {
return transfers.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Transfer t = transfers.get(rowIndex);
Object val = null;
switch (columnIndex) {
case 0:
val = t.getAccount().getId().getNumber();
break;
case 1:
val = t.getDestinationAccount().getId().getNumber();
break;
case 2:
val = t.getAmount();
break;
default:
assert false;
break;
}
return val;
}
}
private static final long serialVersionUID = 5090183202921964451L;
private JDialog dialog;
private JTable transfers;
public PendingAction (BankGraphicInterface bankInterface,
TextManager textManager,
AccountOperationService accountOperationService){
super(bankInterface, textManager, accountOperationService);
super.putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
super.putValue(Action.NAME, textManager.getText("action.pending"));
}
public void close() {
dialog.dispose();
dialog = null;
}
@Override
public void execute() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel transactionsPanel = new JPanel();
transactionsPanel
.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
transfers = new JTable();
JScrollPane scrollPane = new JScrollPane(transfers,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
transactionsPanel.add(scrollPane);
showPending();
transfers.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
JTable table =(JTable) me.getSource();
Point p = me.getPoint();
int row = table.rowAtPoint(p);
if (me.getClickCount() == 2) {
Object[] options = { "ACEITAR", "RECUSAR", "FECHAR" };
int optionSelected = JOptionPane.showOptionDialog(null,"Aceitar transferência pendente?", "Pendente", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
switch(optionSelected) {
case 0:
this.acceptPending(row);
break;
case 1:
this.rejectPending(row);
break;
case 2:
break;
case JOptionPane.CLOSED_OPTION:
break;
}
}
}
private void rejectPending(int row) {
// TODO Auto-generated method stub
}
// HERE IS THE ERROR
// .................
// I try to access this.transfer (outside Mouse Listener)
// but I get an error trying to do this
private void acceptPending(int row) {
Object accountOrigin = this.transfers.getModel().getValueAt(row, 0);
Object accountDestin = this.transfers.getModel().getValueAt(row, 1);
Object ammount = this.transfers.getModel().getValueAt(row, 2);
int intAccountOrigin;
int intAccountDestin;
try {
intAccountOrigin = (int) accountOrigin;
intAccountDestin = (int) accountDestin;
} catch (ClassCastException exc) {
}
}
});
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel pane = new JPanel(new BorderLayout());
pane.add(mainPanel, BorderLayout.NORTH);
pane.add(transactionsPanel, BorderLayout.CENTER);
this.dialog = GUIUtils.INSTANCE.createDialog(bankInterface.getFrame(),
"action.pending", pane);
this.dialog.setVisible(true);
}
private void showPending() {
try {
List<Transfer> transfers = accountOperationService.getPending();
this.transfers.setModel(new TransferTableModel(transfers));
} catch (BusinessException be) {
GUIUtils.INSTANCE.showMessage(bankInterface.getFrame(),
be.getMessage(), be.getArgs(), JOptionPane.WARNING_MESSAGE);
log.warn(be);
} catch (Exception exc) {
GUIUtils.INSTANCE.handleUnexceptedError(bankInterface.getFrame(),
exc);
}
}
}
我在代码中突出显示了给我带来麻烦的部分。 基本上我不知道如何从MouseListener之外的类访问方法或属性。
答案 0 :(得分:2)
使用TransferTableModel.this
代替this
。
在内部类中,this
用于当前内部类实例。要访问容器实例,您应该使用容器类本身的.this
属性。