我有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的调用。我该怎么做回电工作?

最佳答案

你可以...

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 showDialog方法创建一些面板/组件,例如,类似于登录面板的东西。这意味着我可以在其他地方重复使用面板,但是为我提供了在需要时能够弹出对话框的便利。我还不时使用JOptionPane来显示这些面板,但这取决于可用操作的复杂性...

09-11 09:07