[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

how to make a check box text blinking in java swing

I have a question regarding check box in Java Swing, I have a text associated with check box, I have to make this text blinking ones we select the check box, do  we have any built in method to blink or we should do it in some other way ??
0
dhulipala
Asked:
dhulipala
  • 13
  • 7
  • 3
  • +1
1 Solution
 
hoomanvCommented:
we should do it in some other way
run a thread that will sets and resets the background or border color of the checkbox two or three times after it is checked.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Not sure, try the code here:

http://ronblatt.tripod.com/VennDiagram.java
0
 
CEHJCommented:
Use a Swing Timer and after the first click start it, changing the caption from "" to whatever

http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
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!

 
hoomanvCommented:
from this package: ftp://pirlftp.lpl.arizona.edu/pub/Java/PIRL-1.7.7.tar.gz
you can find a ready to use Blinker (source code) for any swing component
documentation can be find here: http://pirlwww.lpl.arizona.edu/software/PIRL_Java_Packages/PIRL/Viewers/Blinker.html
0
 
dhulipalaAuthor Commented:
final javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener() {
                  public void actionPerformed(ActionEvent evt){
                         chkBox().repaint();
                        
                  }
            });

 chkBox().addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e){
                      if(e.getStateChange() == ItemEvent.SELECTED)
                            timer.start();
                      else
                            timer.stop();
                }
           });

I am using the above code, but its not refreshing the check box, what could be the reason ??
0
 
hoomanvCommented:
use the code I told you

// setup the blinker
Blinker blinker = new Blinker(checkbox, Color.RED, 5, 200, 200);

// register an item listener
checkbox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e){
            blinker.start();
      }
});

works fine
0
 
CEHJCommented:
>>chkBox().repaint();

Not sure why you're doing that. Surely you want to change the text?

if ("".equals(cb.getText())) {
    cb.setText("Hello");
}
else {
    cb.setText("");
}
0
 
hoomanvCommented:
also make sure to change the components's text back its default text

              if(e.getStateChange() == ItemEvent.SELECTED)
                        timer.start();
                   else {
                        checkbox.setText("default text");
                        timer.stop();
                   }
              }
0
 
hoomanvCommented:
timer.stop();
checkbox.setText("default text");

is more reliable
0
 
dhulipalaAuthor Commented:
i don't want to make any change to the text but I just want to blink the existing text
0
 
hoomanvCommented:
you can set the foreground = background to make the text disappeared
the checkbox should be opaque in this case
0
 
CEHJCommented:
>>i don't want to make any change to the text but I just want to blink the existing text

What i posted *will* blink the text
0
 
hoomanvCommented:
this way

      if (checkbox.getForeground().equals(checkbox.getBackground())) {
            checkbox.setForeground(Color.BLACK);
      }
      else {
            checkbox.setForeground(checkbox.getBackground());
      }
0
 
dhulipalaAuthor Commented:
ones i set foreground = background text is disappearing but its not coming back again, I have even set opaque to true.
0
 
hoomanvCommented:
try my last comment

also checkbox.getForeground() == checkbox.getBackground() is better than using equals
and also make sure to reset back the fore color after timer.stop();
0
 
dhulipalaAuthor Commented:
ChkBox().setForeground(ChkBox().getBackground()); this is the actual command which I am using,

if I reset the fore color after timer.stop(), i am able to see the text only after I deselect the check box, this is not what i need, any more ideas ??
0
 
hoomanvCommented:
you are doing somthing wrong
try this code

     if (checkbox.getForeground().equals(checkbox.getBackground())) {
          checkbox.setForeground(Color.BLACK);
          System.out.println("BLACK");
     }
     else {
          checkbox.setForeground(checkbox.getBackground());
          System.out.println("WHITE");
     }

the BLACK - WHITE will be printed periodically
is it true ?
0
 
hoomanvCommented:
> i am able to see the text only after I deselect the check box
you'll see the text secondly (since you've set the interval to 1000 milisec)
0
 
dhulipalaAuthor Commented:
Great its working, I have one more last question, based on some condition i will check the check box programmatically, behavior of the timer is different if I select the check box through program and if I select it manually its different, what could be the reason behind this
0
 
hoomanvCommented:
> if I select the check box through program
how do you select it ?

checkbox.dispatchEvent(new ItemEvent(checkbox, ItemEvent.SELECTED, item, true/false));
I think it works
0
 
dhulipalaAuthor Commented:
i am doing using chkbox().setSelected(true), during this time also the event will be called and timer will start, (am I correct) in this scenario its refreshing very fast I don't know why ?
0
 
hoomanvCommented:
> in this scenario its refreshing very fast
may be there are more than one timer invloved in blinking the checkbox at the same time
0
 
dhulipalaAuthor Commented:
no there is only one timer which I have created recently
0
 
hoomanvCommented:
its hard to suggest without seeing the code
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 13
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now