L
Leo Breebaart
I have a control-flow issue I was hoping to get some advice on.
In my Swing GUI, I have a button. If I click on that button, I
want, via its associated Action, but, and this is the kicker:
*depending on a certain condition*, to show and process the
results of a modal JDialog *before* I perform the actual action.
But the action should always run. It's just the showing of the
dialog that is dependent on outside context.
How I would *like* to implement this:
public class MyAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
if (hasQuestionnaire)
showQuestionnaire();
doStuff();
}
private void showQuestionnaire()
{
final QuestionnaireDialog dialog = new QuestionnaireDialog();
dialog.setVisible(true);
dialog.getCancelButton().addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(false);
// process cancel
}
});
dialog.getSubmitButton().addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(false);
// process submit
}
});
}
}
This does not work, because the showQuestionnaire method will put
up the dialog without blocking, causing 'doStuff' to be executed
immediately afterwards, without waiting for the dialog to be
dismissed.
I can solve the issue by moving the doStuff() call into both of
the dialog's registered ActionListeners, *and* by placing the
MyAction's doStuff() call into an else-branch of the
if-statement, but all that multiplication of code (even if it's
just a single line) smells very wrong to me. It makes the actual
control flow a lot more difficult to follow.
Am I missing something simple? Does anybody have any advice on
how they would implement this cleanly?
Many thanks in advance,
In my Swing GUI, I have a button. If I click on that button, I
want, via its associated Action, but, and this is the kicker:
*depending on a certain condition*, to show and process the
results of a modal JDialog *before* I perform the actual action.
But the action should always run. It's just the showing of the
dialog that is dependent on outside context.
How I would *like* to implement this:
public class MyAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
if (hasQuestionnaire)
showQuestionnaire();
doStuff();
}
private void showQuestionnaire()
{
final QuestionnaireDialog dialog = new QuestionnaireDialog();
dialog.setVisible(true);
dialog.getCancelButton().addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(false);
// process cancel
}
});
dialog.getSubmitButton().addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(false);
// process submit
}
});
}
}
This does not work, because the showQuestionnaire method will put
up the dialog without blocking, causing 'doStuff' to be executed
immediately afterwards, without waiting for the dialog to be
dismissed.
I can solve the issue by moving the doStuff() call into both of
the dialog's registered ActionListeners, *and* by placing the
MyAction's doStuff() call into an else-branch of the
if-statement, but all that multiplication of code (even if it's
just a single line) smells very wrong to me. It makes the actual
control flow a lot more difficult to follow.
Am I missing something simple? Does anybody have any advice on
how they would implement this cleanly?
Many thanks in advance,