Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating swing components correctly?

I'm new to swing, any help appreciated.

In this piece of code I'm turning a card over face up, if it turns out that they don't match I want them to turn back face down again.

At the moment what is happening: 1. when clicked the first card turns over 2. when a second card is clicked either of two things happen (a) if they are the same they both stay up which is what I want (b) if they are not the same I never see the 2nd card at all as it immediately re-displays the back of the card (and the back of the previous card also as defined in my method).

I thought putting in the sleep timer might keep the 2nd card displayed for a period of time before turning back over but it does not.

I attempted to use contentPane.revalidate(); & contentPane.repaint(); but it doesn't change anything.

I have put in some console outputs:

Console output:
Card: 0 set
Card: 6 set
Sleeping now
Card: 6 unset
Card: 0 unset

Above is the resulting console output when clicking two card which do not match

@Override
public void actionPerformed(ActionEvent e) 
{
    String buttonPressed = e.getActionCommand();
    int pos = Integer.valueOf(buttonPressed);
    action = Control.model.ReceiveCardsTurned(pos);

    keypadArray[pos].setIcon(myIcons[pos]);     
    System.out.println("Card: "+pos+" set");
    currentTime.setText("" + Control.model.time);
    currentScore.setText("" + Control.model.score);

    //contentPane.revalidate();
    //contentPane.repaint();        

    if(Control.model.twoCardsTurned == false)
    {
        if (action == "unturn") 
        {
            System.out.println("Sleeping now");

            try 
            {
                Thread.sleep(1000);
            }

            catch (InterruptedException e1) 
            {
                e1.printStackTrace();
            }

            keypadArray[pos].setIcon(back);
            keypadArray[Control.model.lastCard].setIcon(back);
            System.out.println("Card: "+pos+" unset");
            System.out.println("Card: "+Control.model.lastCard+" unset");
        }
    }
}
like image 276
Ron Avatar asked Dec 14 '12 00:12

Ron


People also ask

How do you update Swing component from a thread other than EDT?

You can use invokeAndWait() and invokeLater() to update a Swing component from any arbitrary thread.

Which package is used for improving Swing components?

By far the most widely-used package is javax. swing . In fact, almost all the Swing components, as well as several utility classes, are located inside this package. The only exceptions are borders and support classes for the trees, tables, and text-based components.

Why Swing components have J in front of them?

The J identifies all Swing components. Swing used to be marketed as Java Foundation Classes, before it became an integral part of the JDK. +1: It also made it less of a headache to include both AWT classes and Swing classes in the same source file.

What is Swing components explain in detail with example?

Swing components are the basic building blocks of an application. We know that Swing is a GUI widget toolkit for Java. Every application has some basic interactive interface for the user. For example, a button, check-box, radio-button, text-field, etc. These together form the components in Swing.


1 Answers

You can't sleep in the event dispatch thread, because your GUI will freeze. You have to use Swing Timer. For background tasks you'll probably have to worry about in the future, take a look at SwingWorker.

like image 128
Jan Krakora Avatar answered Oct 01 '22 01:10

Jan Krakora