extends JList (custom tooltops) : JList « javax.swing « Java by API






extends JList (custom tooltops)

  
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ToolTipManager;

public class MainClass extends JList {

  SortedListModel model;

  Properties tipProps;

  public MainClass(Properties props) {
    model = new SortedListModel();
    setModel(model);
    ToolTipManager.sharedInstance().registerComponent(this);

    tipProps = props;
    addProperties(props);
  }

  private void addProperties(Properties props) {
    Enumeration names = props.propertyNames();
    while (names.hasMoreElements()) {
      model.add(names.nextElement());
    }
  }

  public String getToolTipText(MouseEvent event) {
    Point p = event.getPoint();
    int location = locationToIndex(p);
    String key = (String) model.getElementAt(location);
    String tip = tipProps.getProperty(key);
    return tip;
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Custom Tip Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    MainClass list = new MainClass(props);
    JScrollPane scrollPane = new JScrollPane(list);
    frame.add(scrollPane);
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}

class SortedListModel extends AbstractListModel {

  SortedSet<Object> model;

  public SortedListModel() {
    model = new TreeSet<Object>();
  }

  public int getSize() {
    return model.size();
  }

  public Object getElementAt(int index) {
    return model.toArray()[index];
  }

  public void add(Object element) {
    if (model.add(element)) {
      fireContentsChanged(this, 0, getSize());
    }
  }

  public void addAll(Object elements[]) {
    Collection<Object> c = Arrays.asList(elements);
    model.addAll(c);
    fireContentsChanged(this, 0, getSize());
  }

  public void clear() {
    model.clear();
    fireContentsChanged(this, 0, getSize());
  }

  public boolean contains(Object element) {
    return model.contains(element);
  }

  public Object firstElement() {
    return model.first();
  }

  public Iterator iterator() {
    return model.iterator();
  }

  public Object lastElement() {
    return model.last();
  }

  public boolean removeElement(Object element) {
    boolean removed = model.remove(element);
    if (removed) {
      fireContentsChanged(this, 0, getSize());
    }
    return removed;
  }
}

           
         
    
  








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.getSelectionBackground()
18.JList: getSelectionForeground()
19.JList: getVisibleRowCount()
20.JList: isSelectedIndex(int index)
21.JList: isSelectionEmpty()
22.JList: locationToIndex(Point p)
23.JList: removeSelectionInterval(int index0, int index1)
24.JList: setCellRenderer(ListCellRenderer cellRenderer)
25.JList: setDragEnabled(boolean b)
26.JList: setFixedCellHeight(int height)
27.JList: setFixedCellWidth(int width)
28.JList: setLayoutOrientation(int ori)
29.JList: setModel(ListModel model)
30.JList: setPrototypeCellValue(Object prototypeCellValue)
31.JList: setSelectedIndex(int index)
32.JList: setSelectedValue(Object anObject, boolean shouldScroll)
33.JList: setSelectionInterval(int anchor, int lead)
34.JList: setSelectionMode(int selectionMode)
35.JList: setVisibleRowCount(int count)