13 February 2011

Java Swing/AWT  and Waiting for a Card
You need to use a "SwingWorker" which is a kind of Thread.

If not, your screen refreshes will freeze.


The main will need to be "invokeLater"
I wish Java AWT could do threads in a more simple fashion,
but this is how it is






    SwingWorker<String, String> waitForCard = null;
    String tap = "Tap";


    private void buttonPress(java.awt.event.ActionEvent evt)                          
    {                              


        jTextArea1.setBackground(Color.white);
        jButton1.setEnabled(false);
        jTextArea1.setText(tap);


        final String badRead = "badRead";
//SW SW SW SW SW SW SW SW SW SW SW SW SW SW SW SW 


        waitForCard = new SwingWorker<String, String>()
        {
            public String doInBackground()
            {
                do
                {
                    waitCard();  // waits for a card on the NFC card reader
                    if (this.isCancelled())
                           return("cancelled");
                }
                while ( rbal.contentEquals(badRead) );  // rbal example is Card BAlanc
                publish(rbal);   
 //publish an update, this will get added to a list and
// processed in bulk by the process() method at some point
/ ie if we want a sequence, eg clear the field after one second, do the work, publish
then "done" does the final clear


                waitCardGone();




                try {
                    Thread.sleep(1500);


                } catch (InterruptedException e) {
                    return("rupt");    // never seen?
                }


                return("blank");


            }
            @Override
            protected void process(List<String> chunks) {
                jTextArea1.setText(chunks.get(0));  // display your data
            }


            @Override
            protected void done()
            {
            jTextArea1.setText("");  // clear the text after 1.5 seconds
            jTextArea1.setBackground(mgreen);
            jButton1.setEnabled(true);


            } // done
        };    //waitForCard definition fin
//SW SW SW SW SW SW SW SW SW SW SW SW SW SW






        waitForCard.execute();


    }                         

----------------------------------------------------------------------------------

public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                Framek f = new Framek();
                f.setLocation(Uti.centrePoint(f));
                f.setTitle("Card Balance ");
                f.setVisible(true);
                f.initk();  // initialise stuff

             }
        });
    }

No comments:

Post a Comment