aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to make JComboBox flash (red color)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to make JComboBox flash (red color)?" Watch "How to make JComboBox flash (red color)?" New topic
Author

How to make JComboBox flash (red color)?

Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
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
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

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


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
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?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jan 21, 2009
Posts: 35
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?
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
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

Joined: Jan 21, 2009
Posts: 35
That sound super complicated. Can you lead me a bit further than that?
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
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

Joined: Dec 13, 2008
Posts: 112
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

Joined: Jan 21, 2009
Posts: 35
This should not be this hard !!! ohhhh java, this language is definitely not for noob!!! gosh
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make JComboBox flash (red color)?