• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to make JComboBox flash (red color)?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example, the user select "cat" from the list of JComboBox, it then fire up an event to load some texts in some of the text boxes. When the user change the content of one of the boxes, the JComboBox (with the selected item is "cat") flash (red color maybe) to indicated that the content of "cat" has been change. That the user need to save. How can I do that? I have done something similar in C# using Window Form Validated and Validating, but somehow it is much harder in java. I been looking around but nothing come close to what I want. Thanks in advance
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it is possible to make it "flash" so to speak. However, if you can change the color of the text for the JComboBox item then you may want to look into Timer class of swing. With Timer class, you can then change the color of the item in say 1sec interval making it look like flashing
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can I change the color of the text of the selected item only, because when I change the foreground(), it change the color of all the text.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to create a customer renderer and you'll need to update your list model and have the renderer deal with what item should be what color.
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I run into a problem, here is the quote from the website that you send me
"Be careful when implementing a custom model for a combo box. The JComboBox methods that change the items in the combo box's menu, such as insertItemAt, work only if the data model implements the MutableComboBoxModel interface (a subinterface of ComboBoxModel). "

This mean that if I implement custom render to the JComboBox, it will make the JComboBox become immutable, is this correct? Meaning that if I implement custom render, I cant insert item or delete item from the JComboBox List?

If this is the case, then custom render will not be the way for me to go? I constantly insert and delete item from the list. Is there another route to accomplish what I want?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thang Pham wrote:This mean that if I implement custom render to the JComboBox, it will make the JComboBox become immutable, is this correct? Meaning that if I implement custom render, I cant insert item or delete item from the JComboBox List?


I think that your confusing renderers with models. Please understand that they are two completely different things, and one shouldn't effect the other. Why not try playing with creating a custom renderer and seeing what happens? You've nothing to lose here.
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try to write some code as you said, and actually I got something, but I still have some question.

I can change the color (red color) of the item that I roll mouse over, however when I actually select the item, the color change back



any idea, how can I fix this?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may have to create some custom ComboBoxUI eg. extends from BasicComboBoxUI and add MouseListener to the list (JList) which is communicating with your ListCellRenderer. Something like that I think...
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sound super complicated. Can you lead me a bit further than that?
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if your code suppose to perform anything different than mine, but when I add the main class and run it, it performs exact like mine. That when I click on the down arrow button, a list pop out, then when I roll mouse over the items of the list, it changes the background color of that item to RED, but when I select that item, the list close up, but the color of the selected item changes back to light blue (the system original selection color). It is no longer RED anymore.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, in that moment I didn't realized that the isSelected argument performs the exact thing like the long code I wroted, heh...

or


For your requirements I think you have to override paintCurrentValue in UI class
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should not be this hard !!! ohhhh java, this language is definitely not for noob!!! gosh
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> then when I roll mouse over the items of the list, it changes the background color of that item to RED,
> but when I select that item, the list close up, but the color of the selected item changes back to light blue (the system original selection color).

seems you need a combination of

box.setBackground(Color.RED);
box.setForeground(Color.GREEN);
and
UIManager.put("ComboBox.selectionBackground",new javax.swing.plaf.ColorUIResource(Color.RED));
UIManager.put("ComboBox.selectionForeground",new javax.swing.plaf.ColorUIResource(Color.GREEN));
and
your renderer

the UIManager lines MUST be before you create the comboBox
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic