C
Composer
My main class extends JFrame.
It includes some user input fields and a Go button:
JButton goButton = new JButton("Go");
// This Runnable allows the actionListener to give work to the
awt thread.
final Runnable doIt = new Runnable()
{ public void run()
{ createResults();
}
};
goButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent ae)
{ SwingUtilities.invokeLater(doIt);
}
});
So far, so good.
The user enters input, presses "Go", and createResults() gets
executed.
Within createResults() a lot of work gets done, in loops.
Each loop iteration tests the "stopping" variable.
I display a window (a JFrame, but maybe that's a bad choice?)
which includes a JProgressBar and also a "Stop" button:
progressStopButton = new JButton("Stop");
// This Runnable allows the actionListener to give work to the
awt thread.
final Runnable stopIt = new Runnable()
{ public void run()
{ stopping = true;
JOptionPane.showMessageDialog(null, "You pressed the
button.", "Ya",
JOptionPane.INFORMATION_MESSAGE);
}
};
progressStopButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent ae)
{ SwingUtilities.invokeLater(stopIt);
}
});
This does NOT work.
For one thing, the Stop button itself doesn't even appear unless
I repaint it after showing the JFrame:
progressStopButton.update(progressStopButton.getGraphics());
I can live with that. But the more serious problem is that
pressing the Stop button does nothing. It acts as though it's
disabled,
although it is enabled.
Is the problem that I am invoking a Runnable within a Runnable?
Do I need to use sleep()?
Is the problem that I create the progress bar and button in a new
JFrame?
Should I really have to repaint the Stop button after creating it?
Is it necessary to rewrite my code to have 2 or 3 threads?
Sorry for being so inexpert in threads and interrupts.
I had thought that the logic for the Stop button could use the same
approach as the logic for the Go button.
Thanks for any help.
It includes some user input fields and a Go button:
JButton goButton = new JButton("Go");
// This Runnable allows the actionListener to give work to the
awt thread.
final Runnable doIt = new Runnable()
{ public void run()
{ createResults();
}
};
goButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent ae)
{ SwingUtilities.invokeLater(doIt);
}
});
So far, so good.
The user enters input, presses "Go", and createResults() gets
executed.
Within createResults() a lot of work gets done, in loops.
Each loop iteration tests the "stopping" variable.
I display a window (a JFrame, but maybe that's a bad choice?)
which includes a JProgressBar and also a "Stop" button:
progressStopButton = new JButton("Stop");
// This Runnable allows the actionListener to give work to the
awt thread.
final Runnable stopIt = new Runnable()
{ public void run()
{ stopping = true;
JOptionPane.showMessageDialog(null, "You pressed the
button.", "Ya",
JOptionPane.INFORMATION_MESSAGE);
}
};
progressStopButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent ae)
{ SwingUtilities.invokeLater(stopIt);
}
});
This does NOT work.
For one thing, the Stop button itself doesn't even appear unless
I repaint it after showing the JFrame:
progressStopButton.update(progressStopButton.getGraphics());
I can live with that. But the more serious problem is that
pressing the Stop button does nothing. It acts as though it's
disabled,
although it is enabled.
Is the problem that I am invoking a Runnable within a Runnable?
Do I need to use sleep()?
Is the problem that I create the progress bar and button in a new
JFrame?
Should I really have to repaint the Stop button after creating it?
Is it necessary to rewrite my code to have 2 or 3 threads?
Sorry for being so inexpert in threads and interrupts.
I had thought that the logic for the Stop button could use the same
approach as the logic for the Go button.
Thanks for any help.