FileDocCategorySizeDatePackage
AWTBlockModels.javaAPI DocExample8955Mon Jan 09 11:02:02 GMT 2006None

AWTBlockModels

public class AWTBlockModels extends JFrame

Fields Summary
JButton
blockButton
JButton
dontBlockButton
JMenuItem
blockMenuItem
JMenuItem
dontBlockMenuItem
JMenuItem
quitMenuItem
JTextField
urlField
JTextArea
contentArea
static final String
DEFAULT_URL
Thread
loaderThread
JProgressBar
progressBar
Timer
progressBarUpdater
Constructors Summary
public AWTBlockModels()


       
        super ("AWT Thread Blocking");
        initMainLayout();
        initMenus();
        initActions();
    
Methods Summary
private voidinitActions()

        quitMenuItem.addActionListener (new QuitAction());
        BlockingLoadAction blocker = new BlockingLoadAction();
        blockButton.addActionListener (blocker);
        blockMenuItem.addActionListener (blocker);
        NonBlockingLoadAction nonBlocker = new NonBlockingLoadAction();
        dontBlockButton.addActionListener (nonBlocker);
        dontBlockMenuItem.addActionListener (nonBlocker);
    
private voidinitMainLayout()

        urlField = new JTextField (DEFAULT_URL, 60);
        JPanel topPanel = new JPanel ();
        topPanel.setLayout (new BoxLayout (topPanel, BoxLayout.Y_AXIS));
        topPanel.add (urlField);
        JPanel buttonPanel = new JPanel();
        blockButton = new JButton ("Load (blocking)");
        dontBlockButton = new JButton ("Load (non-blocking)");
        buttonPanel.add (blockButton);
        buttonPanel.add (dontBlockButton);
        topPanel.add (buttonPanel);
        contentArea = new JTextArea (25, 60);
        JScrollPane scroller = 
            new JScrollPane (contentArea,
                             ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add (topPanel, BorderLayout.NORTH);
        getContentPane().add (scroller, BorderLayout.CENTER);
        progressBar = new JProgressBar (0, 100);
        getContentPane().add (progressBar, BorderLayout.SOUTH);
    
private voidinitMenus()

        JMenuBar bar = new JMenuBar();
        JMenu fileMenu = new JMenu ("File");
        blockMenuItem = new JMenuItem ("Load (blocking)");
        dontBlockMenuItem = new JMenuItem ("Load (non-blocking)");
        fileMenu.add (blockMenuItem);
        fileMenu.add (dontBlockMenuItem);
        fileMenu.addSeparator();
        quitMenuItem = new JMenuItem ("Quit");
        fileMenu.add (quitMenuItem);
        bar.add (fileMenu);
        setJMenuBar (bar);
    
public static voidmain(java.lang.String[] args)

        AWTBlockModels awtbm = new AWTBlockModels();
        awtbm.pack();
        awtbm.setVisible (true);
    
private voidmakeProgressBarUpdaterFor(AWTBlockModels$NonBlockingURLDocument nbud)

        final NonBlockingURLDocument updatingDoc = nbud;
        updateProgressBar (0);
        ActionListener callback = new ActionListener() {
            public void actionPerformed (ActionEvent ev) {
                progressBar.setEnabled (true);
                int progress = (int) (updatingDoc.getProgress() * 100);
                updateProgressBar (progress);
                if (! updatingDoc.isAlive())
                    progressBarUpdater.stop();
            }
        };
        progressBarUpdater = new javax.swing.Timer (2000, callback);
        progressBarUpdater.start();
    
private voidupdateProgressBar(int progress)

        // System.out.println ("update progress bar: " + progress);
        if (progress > 0) {
            progressBar.setValue (progress);
        }
        else
            progressBar.setEnabled (false);