JList.getSelectionBackground() : JList « javax.swing « Java by API






JList.getSelectionBackground()

  
//
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

public class MainClass extends JFrame {

  MainClass(String s) {
    super(s);
    ListModel lm = new StaticListModel();
    JList list = new JList();
    list.setModel(lm);
    list.setCellRenderer(new MyCellRenderer());
    getContentPane().add(new JScrollPane(list));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  public static void main(String[] s) {
    MainClass l = new MainClass("ListModel");
    l.pack();
    l.setVisible(true);
  }

  class MyCellRenderer extends JLabel implements ListCellRenderer {
    public Component getListCellRendererComponent(JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) {
      Component c = null;
      if (value == null) {
        c = new JLabel("(null)");
      } else if (value instanceof Component) {
        c = (Component) value;
      } else {
        c = new JLabel(value.toString());
      }

      if (isSelected) {
        c.setBackground(list.getSelectionBackground());
        c.setForeground(list.getSelectionForeground());
      } else {
        c.setBackground(list.getBackground());
        c.setForeground(list.getForeground());
      }

      if (c instanceof JComponent) {
        ((JComponent) c).setOpaque(true);
      }

      return c;
    }
  }

  class StaticListModel implements ListModel {
    private final Object[] data = { "Hello", new Object(), new java.util.Date(),
        new JLabel("Hello world!"), this, };

    public Object getElementAt(int index) {
      return data[index];
    }

    public int getSize() {
      return data.length;
    }

    public void addListDataListener(ListDataListener ldl) {
    }

    public void removeListDataListener(ListDataListener ldl) {
    }
  }
}

           
         
    
  








Related examples in the same category

1.JList.VERTICAL_WRAP
2.new JList(Object[] listData)
3.new JList(Vector listData)
4.new JList(ListModel dataModel) (Share model with JComboBox)
5.new JScrollPane(JList list)
6.JList: addListSelectionListener(ListSelectionListener listener)
7.JList: addMouseListener(MouseListener lis)
8.JList: addSelectionInterval(int anchor, int lead)
9.JList: clearSelection()
10.JList: getFirstVisibleIndex()
11.JList: getLastVisibleIndex()
12.JList: getLayoutOrientation()
13.JList: getMaxSelectionIndex()
14.JList: getNextMatch(String prefix, int startIndex, Bias bias)
15.JList: getSelectedIndices()
16.JList: getSelectedValues()
17.JList: getSelectionForeground()
18.JList: getVisibleRowCount()
19.JList: isSelectedIndex(int index)
20.JList: isSelectionEmpty()
21.JList: locationToIndex(Point p)
22.JList: removeSelectionInterval(int index0, int index1)
23.JList: setCellRenderer(ListCellRenderer cellRenderer)
24.JList: setDragEnabled(boolean b)
25.JList: setFixedCellHeight(int height)
26.JList: setFixedCellWidth(int width)
27.JList: setLayoutOrientation(int ori)
28.JList: setModel(ListModel model)
29.JList: setPrototypeCellValue(Object prototypeCellValue)
30.JList: setSelectedIndex(int index)
31.JList: setSelectedValue(Object anObject, boolean shouldScroll)
32.JList: setSelectionInterval(int anchor, int lead)
33.JList: setSelectionMode(int selectionMode)
34.JList: setVisibleRowCount(int count)
35.extends JList (custom tooltops)