[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

checkboxes

I'm using checkboxes in my applet. When I clicked on a checkbox, it activated and display an object on a canvas. But when I clicked it again so that it is unticked, supposedly the object will be gone but it didn't. So, it just responded to one action.
Now how do I make it removes the object when I untick it and make it displays the object when I click it again?
0
suhani
Asked:
suhani
  • 4
  • 3
1 Solution
 
roykoCommented:
u will need to code in such a way that when u perform an event to
unitck the checkbox u will remove the object and refresh the
applet.So when u tick the checkbox again , u will display the object and refresh the applet
0
 
jpk041897Commented:
What type of object are you talking about? Canvas, component, lightweight comonent, graphics object written on a component?
0
 
suhaniAuthor Commented:
There are graphics object drawn on canvas.
Do you mind giving me an example of the coding.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jpk041897Commented:
The simplest approach would be to set up a semaphore. You can do this by defining a static class that contains a flag member variable (say a boolean).

If you instantiate this class in both your check box event handler and your paint() method, you could then code the paint() method to either display or not diplay the data depending on the value.

Since you did not post the code for your event handler or your paint method, and did not specify for which version of the JDK, I can only give you a rough idea:

static class Sem1{
   boolean flag;
}

//inside your event handler

...
Sem1 Semaphore = new Sem1();

// if uncheking

Semaphore.flag = false;
YourCanvas.paint();

// if checking

Semaphore.flag = true;
YourCanvas.paint();
...


// Inside your canvases paint method

...
Sem1 Semaphore = new Sem1();

if (Semaphore.flag){

// Your code do draw the data here

}
...
0
 
suhaniAuthor Commented:
When I try to compile it, it gave me this error message:
 Class or interface declaration expected.
static class Sem1 {

I've never heard od a static class. Could you please explain this to me? Anyway, I'm using JDK1.1 version.

This is my event handler method:
public void itemStateChanged(ItemEvent e) {
            Sem1 Semaphore = new Sem1();

            if(e.getItemSelectable() == win3.c6) {
                  Semaphore.flag = true;
                  panel2.maps.paint();
            }
            else {
                  Semaphore.flag = false;
            }      
      }

This is my paint method:
public void paint (){
            Sem1 Semaphore = new Sem1();

            if (Semaphore.flag) {
                  g = getGraphics();
                  g.drawRect(100, 150, 40, 40);
            }
      }

0
 
suhaniAuthor Commented:
When I try to compile it, it gave me this error message:
 Class or interface declaration expected.
static class Sem1 {

I've never heard od a static class. Could you please explain this to me? Anyway, I'm using JDK1.1 version.

This is my event handler method:
public void itemStateChanged(ItemEvent e) {
            Sem1 Semaphore = new Sem1();

            if(e.getItemSelectable() == win3.c6) {
                  Semaphore.flag = true;
                  panel2.maps.paint();
            }
            else {
                  Semaphore.flag = false;
            }      
      }

This is my paint method:
public void paint (){
            Sem1 Semaphore = new Sem1();

            if (Semaphore.flag) {
                  g = getGraphics();
                  g.drawRect(100, 150, 40, 40);
            }
      }

0
 
jpk041897Commented:
My appologies, I'm doing mixed coding in JAVA and C++ and got the syntax messed up.

The appropiate syntax is:

class Sem1{
       static boolean flag;
    }

Making a member of a class static makes the class static, I.e.: it insures that all instances of a class reside in the same address space, thus sharing the storage space and allowing the construction of a semaphore.

Besides this mistake on my part, your code looks prety good.
0
 
jpk041897Commented:
Ach...! Make that

public static void Sem1{
   static boolean flag;
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now