Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Changing colors of items in list dynamically based on external parameter

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    College Park, MD
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Changing colors of items in list dynamically based on external parameter

    My requirement:

    I have a JList with the list of some store names.
    There is a function that checks the online connectivity of the store or the current state of the store.
    Based on the return value of the function, I need to highlight those store names correspondingly.

    I understand that the JList has a one-dimensional array model. Hence, I cannot save an extra column of data ie. store name, status in the model.

    Is there a way I can use that external parameter 'status' in my ListCellRenderer to change the colors of items in my list?

    Please give me some pointers.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Changing colors of items in list dynamically based on external parameter

    You could store the Store Objects directly in the JList. Then in the renderer, render the store's name and use whatever other Store functions you need to figure out what the cell should look like.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    College Park, MD
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing colors of items in list dynamically based on external parameter

    Thanks!! it worked!! Somehow it didn't strike me that I could use Object array within the renderer.
    Thank you
    Last edited by prathyusha; October 12th, 2011 at 03:22 PM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Changing colors of items in list dynamically based on external parameter

    Quote Originally Posted by prathyusha View Post
    Thanks!! it worked!! Somehow it didn't strike me that I could use Object array within the renderer.
    Thank you
    No problem, glad you got it sorted.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Location
    College Park, MD
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing colors of items in list dynamically based on external parameter

    I got the color rendering done by using an array of objects for every list item.

    Now, I have to retrieve a list item based on the selected value. My list model contains 3 columns. But when I retrieve using Object obj = listModel.getselectedItem(index), it returns an Object which is of type Object[3] . However, when I try to access obj[0], i get an error saying it is not an array of objects. The return type of getSelectedItem is an Object. So, I am stuck with this conversion now.

    Any ideas?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Changing colors of items in list dynamically based on external parameter

    Can't you just cast it? If not, we're going to need to see an SSCCE that demonstrates what you mean.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How are the colors in Swing's UIManager used?
    By JerryH in forum AWT / Java Swing
    Replies: 0
    Last Post: January 27th, 2011, 10:50 PM
  2. java grid and colors/graphics
    By ajmukon in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 25th, 2011, 03:38 PM
  3. misalignment of list items
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: March 15th, 2010, 08:33 AM
  4. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM