Swing and Thread: cancel a lengthy operation : Swing Thread « Threads « Java

Swing and Thread: cancel a lengthy operation

Swing and Thread: cancel a lengthy operation

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BalanceLookupCantCancel extends JPanel {
  private JButton searchB = new JButton("Search");

  private JButton cancelB = new JButton("Cancel Search");

  private JLabel balanceL;

  public BalanceLookupCantCancel() {

  private void buildGUI() {

    JPanel innerButtonP = new JPanel();
    innerButtonP.setLayout(new GridLayout(1, -1, 5, 5));

    JPanel buttonP = new JPanel();
    buttonP.setLayout(new FlowLayout(FlowLayout.CENTER));

    JLabel balancePrefixL = new JLabel("Account Balance:");
    balanceL = new JLabel("BALANCE UNKNOWN");

    JPanel balanceP = new JPanel(new FlowLayout(FlowLayout.CENTER));

    JPanel northP = new JPanel();

    setLayout(new BorderLayout());
    add(northP, BorderLayout.NORTH);

  private void hookupEvents() {
    searchB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

    cancelB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

  private void search() {
    // better be called by event thread!
    balanceL.setText("SEARCHING ...");

    String bal = lookupBalance();

  private String lookupBalance() {
    try {
      // Simulate a lengthy search that takes 5 seconds
      // to communicate over the network.

      // result "retrieved", return it
      return "1,234.56";
    } catch (InterruptedException x) {
      return "SEARCH CANCELLED";

  private void setBalance(String newBalance) {
    // better be called by event thread!

  private void cancelSearch() {
    System.out.println("in cancelSearch()");
    // Here's where the code to cancel would go if this
    // could ever be called!

  public static void main(String[] args) {
    BalanceLookupCantCancel bl = new BalanceLookupCantCancel();

    JFrame f = new JFrame("Can't Cancel");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {

    f.setSize(400, 150);


Related examples in the same category

1.Is Event Dispatcher ThreadIs Event Dispatcher Thread
2.GUI clock
3.Race Conditions using Swing ComponentsRace Conditions using Swing Components
4.Eliminating race Conditions using Swing ComponentsEliminating race Conditions using Swing Components
5.Using the Runnable interfaceUsing the Runnable interface
6.Write your Beans this way so they can run in a multithreaded environmentWrite your Beans this way so they can run in a multithreaded environment
7.User interface responsiveness
8.InvokeExample: Swing and threadInvokeExample: Swing and thread
9.Counter: Swing and threadCounter: Swing and thread
10.Thread accuracy: Swing and threadsThread accuracy: Swing and threads
11.Swing and thread: invoke and waitSwing and thread: invoke and wait
12.Swing and threads: invoke laterSwing and threads: invoke later
13.Swing and threads: slideSwing and threads: slide
14.Swing and threads: scroll textSwing and threads: scroll text
15.Animation: Swing and threadAnimation: Swing and thread
16.Swing and Thread for length operationSwing and Thread for length operation
17.Swing and Thread: repaintSwing and Thread: repaint
18.Thread and Swing 1Thread and Swing 1
19.Thread and Swing 2Thread and Swing 2
20.Thread and Swing 3Thread and Swing 3
21.Swing Type Tester 10Swing Type Tester 10
22.Swing and Threading
23.SwingUtilities.invokeLater and swing thread
24.An abstract class to perform lengthy GUI-interacting tasks in a dedicated thread.
25.This program demonstrates that a thread that runs in parallel with the event dispatch thread can cause errors in Swing components