制作"呼叫方法"等待"返回"数据的

时间:2014-11-10 10:50:57

标签: java swing methods callback actionlistener

我有2个类,一个类是JFrame MainUIHolder.java ),另一个类是JDialog EditValuationsDialog.java )。 MainUIHolder可以在按钮点击事件中调用EditValuationsDialog

打开EditValuationsDialog后,用户可以在其字段中输入数据并按“添加”按钮。好的,这是现在的问题。用户按“添加”按钮后,EditValuationsDialog应通知MainUIHolder

以下是代码。

MainUIHolder

  Action edit = new AbstractAction()
    {
          public void actionPerformed(ActionEvent e)
          {
              JTable table = (JTable)e.getSource();
              int rowNum = Integer.valueOf(e.getActionCommand());
              Object valueAt = table.getModel().getValueAt(rowNum, 0);

              EditValuationsDialog edit = new EditValuationsDialog(null,true);
              edit.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
              edit.setTitle("Edit Valuations");
              edit.setClientName(portfolioViewClientName.getText());
edit.setPortfolioType(portfolioViewInvestmentTypeCombo.getSelectedItem().toString());
              edit.setPortfolioId(id);
                                                                          edit.setOngoingValuationsId(Integer.parseInt(String.valueOf(valueAt)));
              edit.setLocationRelativeTo(table);
               edit.setVisible(true);

               //CATCH THE CALL FROM EditValuationsDialog HERE!!!!//

             }
  };

EditValuationsDialog

//Action Listeners
    private class AddBtnAction implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e) 
        {
           if(someCondition)
           {
              return String / int to MainUIHolder (See where I want to catch it in MainUIHolder)
           }
          else
          {
            do nothing
          }

        }
    }

在我的代码中,我已经指出应该在哪里生成对MainUIHolder的调用,以及我必须在MainUIHolder中捕获该调用的位置。我该怎么回电话呢?

3 个答案:

答案 0 :(得分:2)

创建对话框模态(setModal(true))。然后在关闭对话框后执行dialog.setVisible(true)之后的代码。

BTW最好将MainUIHolder JFrame实例作为对话框的父级传递。

答案 1 :(得分:2)

你可以......

向显示对话框的static添加EditValuationsDialog方法,评估结果并返回您期望的值...

public void actionPerformed(ActionEvent e)
{
    int result = EditValuationsDialog.showDialog();
}

public class EditValuationsDialog ... {
    //...
    private int result = -1;
    //...
    public int getResult() {
        return result;
    }
    //...
    public static int showDialog(Component source, int rowNum, Object valueAt) {

        EditValuationsDialog edit = null;
        Window parent = SwingUtilities.windowFor(source);
        if (parent instanceof Frame) {
            edit = new EditValuationsDialog((Frame)parent,true);
        } else if (parent instanceof Dialog) {
            edit = new EditValuationsDialog((Dialog)parent,true);
        } else {
            edit = new EditValuationsDialog(null,true);
        }

        edit.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        edit.setTitle("Edit Valuations");
        edit.setClientName(portfolioViewClientName.getText());
        edit.setPortfolioType(portfolioViewInvestmentTypeCombo.getSelectedItem().toString());
        edit.setPortfolioId(id);
        edit.setOngoingValuationsId(Integer.parseInt(String.valueOf(valueAt)));
        edit.setLocationRelativeTo(source);
        edit.setVisible(true);

        return edit.getResult();
    }
    //...
    private class AddBtnAction implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            if(someCondition)
            {
                result = 0;
            }
            else
            {
                result = 1;
            }
            EditValuationsDialog.this.dispose();
        }
    }
}

或者你可以......

只需直接评估上述示例中getResult()的结果......

旁注:因为我不喜欢从JDialog这样的顶级容器扩展,所以我倾向于创建一些static {{1}的面板/组件方法,例如登录面板的东西。这意味着我可以重新使用其他面板,但是我可以在需要时弹出对话框。我还不时使用showDialog来展示这些面板,但这取决于可用操作的复杂性......

答案 2 :(得分:0)

您可以在EditValuationsDialog中添加如下界面:

Interface EditValuationsDialogInterface {
public void onAddClicked(addedVlue);

} 

然后按原样添加:

edit.setOnAddButtonCallback(new EditValuationsDialogInterface () {

@Override
onAddClicked(addedVlue){
//DO SOMETHING
}
});

在您的EditValuationsDialog添加按钮点击通话中添加以下内容:

onAddButtonClickedCallback.onAddClicked(retunrValue);

这允许您直接链接回原始调用类。