开发者

Cancel button never gets enabled

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Thread;

public class Lthread extends JPanel{
   private JTextField acctTF;
   private JTextField pinTF;
   private JButton cancelB;
   private JLabel balanceL;
   private JButton searchB;

   public Lthread() {
    buildGUI();
    hookupEvents();
   }

  private void buildGUI() { 
    JLabel acctL=new JLabel("Account Number:");
    JLabel pinL=new JLabel("PIN:");
    acctTF=new JTextField(12);
    pinTF=new JTextField(4);

    JPanel dataEntryP=new JPanel();
    dataEntryP.setLayout(new FlowLayout(FlowLayout.CENTER));
    dataEntryP.add(acctL);
    dataEntryP.add(acctTF);
    dataEntryP.add(pinL);
    dataEntryP.add(pinTF);

    searchB=new JButton("Search");
    cancelB=new JButton("Cancel Search");
    cancelB.setEnabled(false);

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

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

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

    JPanel balanceP=new JPanel();
    balanceP.setLayout(new FlowLayout(FlowLayout.CENTER));
    balanceP.add(balanceL);

   JPanel northP=new JPanel();
   northP.setLayout(new GridLayout(-1,1,5,5));
   northP.add(dataEntryP);
   northP.add(buttonP);
   northP.add(balanceP);

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

 private void hookupEvents() {
    searchB.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae) {
          search(ae);
       }
    });


    cancelB.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae) {
          cancelSearch(ae);
       }
     });
  }
      private void search(ActionEvent ae) {
      searchB.setEnabled(false);
      cancelB.setEnabled(true);
      balanceL.setText("SEARCHING...");
      String acct=acctTF.getText();
      String pin=pinTF.getText();

      String bal=lookupBalance(acct,pin);
      setBalance(bal);
   }

     private String lookupBalance(String acct,String pin) {
       try {
           Thread.sleep(5000);
            return "1,234.56";
       } catch(Exception exc)  {
          return "search cancelled";
     }
    }

       private void setBalance(String newBalance) {
       balanceL.开发者_如何学编程setText(newBalance);
       cancelB.setEnabled(false);
      searchB.setEnabled(true);
     }

     private void cancelSearch(ActionEvent ae) {
        System.out.println("in cancelSearch");
     }

      public static void main(String args[]) {
        Lthread Lt=new Lthread();
        JFrame f=new JFrame("Balance Lookup-Can't cancel");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        f.setContentPane(Lt);
        f.setSize(400,150);
        f.setVisible(true);
       }
      }  

As soon as we click search,cancel should get enabled,but it does not get enabled, why?

Another thing is label balanceL should display SEARCHING. When search is clicked but that is also not displayed. Why?


You're doing all the work (well, sleeping) in the UI thread. That's stopping all the rest of the UI from updating, processing events etc.

You should offload your work (the searching/sleeping) to a background thread, using SwingWorker to marshall any UI calls back to the UI thread. (You can't touch UI components from a non-UI thread.)

A search for "swing threads" gives loads of hits for tutorials etc, if you want more information. In particular, you probably want to read the Java Tutorial trail on threading in Swing.


because the eventdispatcher thread is blocked in the search() function. it should never be blocked. use SwingUtilities.invokeLater().

for your problem change search method to

private void search(ActionEvent ae) {
        searchB.setEnabled(false);
        cancelB.setEnabled(true);
        balanceL.setText("SEARCHING...");
        final String acct=acctTF.getText();
        final String pin=pinTF.getText();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                String bal=lookupBalance(acct,pin);
                setBalance(bal);
            }
        });
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜