Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JList setting color of items

could you please help me, how to change color of items showed in JList?

I'm making an user JList where I can see online and offline users, and I need the offline users to have different colors than online users.

My code for creating users

final String [] strings=database.getUsers(myLogin);  

jList1.setModel(new javax.swing.AbstractListModel() {
     @Override
     public int getSize() { return strings.length; }
     @Override
     public Object getElementAt(int i) { return strings[i]; }
});
like image 456
Jano Nepovinne Bušfy Avatar asked Apr 20 '12 17:04

Jano Nepovinne Bušfy


People also ask

How do I change the color of a selected item in JComboBox?

We can also set the foreground and background color to JComboBox items by using setForeground() and setBackground() methods of a JComboBox class.

How do you add elements to a JList?

Now to add additional elements at any time during execution, use addElement() again: listModel. addElement(new item); and it will show up in the JList.

What is the purpose of JList in Java?

JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .

How do I remove a selected item from a JList in Java?

The JList component is backed by a list model. So the only recommended way to remove an item from the list view is to delete it from the model (and refresh the view). If he's using a DefaultListModel (and in all likelihood he is), then there's no need to refresh the view since this should be done automatically.


2 Answers

I hope this code will totally help you

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.util.Vector;

   import javax.swing.DefaultListCellRenderer;
   import javax.swing.JFrame;
   import javax.swing.JList;
   import javax.swing.JScrollPane;

   public class UserList {

 public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {

           @Override
           public void run() {
                JFrame f = new JFrame("Users");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(300, 300);

                JList list = new JList(new Vector<User>() {
                     {
                          add(new User("A", false));
                          add(new User("B", true));
                          add(new User("C", true));
                          add(new User("D", false));
                     }
                });

                list.setCellRenderer(new DefaultListCellRenderer() {

                     @Override
                     public Component getListCellRendererComponent(JList list, Object value, int index,
                               boolean isSelected, boolean cellHasFocus) {
                          Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                          if (value instanceof User) {
                               User nextUser = (User) value;
                               setText(nextUser.name);
                               if (nextUser.loggedIn) {
                                    setBackground(Color.GREEN);
                               } else {
                                    setBackground(Color.RED);
                               }
                               if (isSelected) {
                                    setBackground(getBackground().darker());
                               }
                          } else {
                               setText("whodat?");
                          }
                          return c;
                     }

                });
                f.add(new JScrollPane(list), BorderLayout.CENTER);
                f.setVisible(true);
           }
      });
 }

 static class User {
      String name = "NN";
      boolean loggedIn = false;

      public User(String name, boolean loggedIn) {
           this.name = name;
           this.loggedIn = loggedIn;
      }
 }

}

like image 134
Jar Yit Avatar answered Sep 22 '22 14:09

Jar Yit


  • I think that you have to read tutorial How to Use Lists, especially part Writing a Custom Cell Renderer, concept of Renderer is the same for JList, JTable or for JComboBox too

  • examples are here and on this forum here

like image 20
mKorbel Avatar answered Sep 24 '22 14:09

mKorbel