< prev index next >

src/java.desktop/share/classes/javax/swing/JList.java

Print this page




  31 import java.util.Locale;
  32 import java.util.ArrayList;
  33 import java.util.Collections;
  34 import java.util.List;
  35 
  36 import java.beans.JavaBean;
  37 import java.beans.BeanProperty;
  38 import java.beans.PropertyChangeEvent;
  39 import java.beans.PropertyChangeListener;
  40 import java.beans.Transient;
  41 
  42 import javax.swing.event.*;
  43 import javax.accessibility.*;
  44 import javax.swing.plaf.*;
  45 import javax.swing.text.Position;
  46 
  47 import java.io.ObjectOutputStream;
  48 import java.io.IOException;
  49 import java.io.Serializable;
  50 


  51 import sun.swing.SwingUtilities2;
  52 import sun.swing.SwingUtilities2.Section;
  53 import static sun.swing.SwingUtilities2.Section.*;
  54 
  55 /**
  56  * A component that displays a list of objects and allows the user to select
  57  * one or more items. A separate model, {@code ListModel}, maintains the
  58  * contents of the list.
  59  * <p>
  60  * It's easy to display an array or Vector of objects, using the {@code JList}
  61  * constructor that automatically builds a read-only {@code ListModel} instance
  62  * for you:
  63  * <pre>
  64  * {@code
  65  * // Create a JList that displays strings from an array
  66  *
  67  * String[] data = {"one", "two", "three", "four"};
  68  * JList<String> myList = new JList<String>(data);
  69  *
  70  * // Create a JList that displays the superclasses of JList.class, by


1535                 ListSelectionModel lsm = getSelectionModel();
1536                 Component rComponent = r.getListCellRendererComponent(
1537                            this, getModel().getElementAt(index), index,
1538                            lsm.isSelectedIndex(index),
1539                            (hasFocus() && (lsm.getLeadSelectionIndex() ==
1540                                            index)));
1541 
1542                 if(rComponent instanceof JComponent) {
1543                     MouseEvent      newEvent;
1544 
1545                     p.translate(-cellBounds.x, -cellBounds.y);
1546                     newEvent = new MouseEvent(rComponent, event.getID(),
1547                                               event.getWhen(),
1548                                               event.getModifiers(),
1549                                               p.x, p.y,
1550                                               event.getXOnScreen(),
1551                                               event.getYOnScreen(),
1552                                               event.getClickCount(),
1553                                               event.isPopupTrigger(),
1554                                               MouseEvent.NOBUTTON);




1555 
1556                     String tip = ((JComponent)rComponent).getToolTipText(
1557                                               newEvent);
1558 
1559                     if (tip != null) {
1560                         return tip;
1561                     }
1562                 }
1563             }
1564         }
1565         return super.getToolTipText();
1566     }
1567 
1568     /**
1569      * --- ListUI Delegations ---
1570      */
1571 
1572 
1573     /**
1574      * Returns the cell index closest to the given location in the list's




  31 import java.util.Locale;
  32 import java.util.ArrayList;
  33 import java.util.Collections;
  34 import java.util.List;
  35 
  36 import java.beans.JavaBean;
  37 import java.beans.BeanProperty;
  38 import java.beans.PropertyChangeEvent;
  39 import java.beans.PropertyChangeListener;
  40 import java.beans.Transient;
  41 
  42 import javax.swing.event.*;
  43 import javax.accessibility.*;
  44 import javax.swing.plaf.*;
  45 import javax.swing.text.Position;
  46 
  47 import java.io.ObjectOutputStream;
  48 import java.io.IOException;
  49 import java.io.Serializable;
  50 
  51 import sun.awt.AWTAccessor;
  52 import sun.awt.AWTAccessor.MouseEventAccessor;
  53 import sun.swing.SwingUtilities2;
  54 import sun.swing.SwingUtilities2.Section;
  55 import static sun.swing.SwingUtilities2.Section.*;
  56 
  57 /**
  58  * A component that displays a list of objects and allows the user to select
  59  * one or more items. A separate model, {@code ListModel}, maintains the
  60  * contents of the list.
  61  * <p>
  62  * It's easy to display an array or Vector of objects, using the {@code JList}
  63  * constructor that automatically builds a read-only {@code ListModel} instance
  64  * for you:
  65  * <pre>
  66  * {@code
  67  * // Create a JList that displays strings from an array
  68  *
  69  * String[] data = {"one", "two", "three", "four"};
  70  * JList<String> myList = new JList<String>(data);
  71  *
  72  * // Create a JList that displays the superclasses of JList.class, by


1537                 ListSelectionModel lsm = getSelectionModel();
1538                 Component rComponent = r.getListCellRendererComponent(
1539                            this, getModel().getElementAt(index), index,
1540                            lsm.isSelectedIndex(index),
1541                            (hasFocus() && (lsm.getLeadSelectionIndex() ==
1542                                            index)));
1543 
1544                 if(rComponent instanceof JComponent) {
1545                     MouseEvent      newEvent;
1546 
1547                     p.translate(-cellBounds.x, -cellBounds.y);
1548                     newEvent = new MouseEvent(rComponent, event.getID(),
1549                                               event.getWhen(),
1550                                               event.getModifiers(),
1551                                               p.x, p.y,
1552                                               event.getXOnScreen(),
1553                                               event.getYOnScreen(),
1554                                               event.getClickCount(),
1555                                               event.isPopupTrigger(),
1556                                               MouseEvent.NOBUTTON);
1557                     MouseEventAccessor meAccessor =
1558                         AWTAccessor.getMouseEventAccessor();
1559                     meAccessor.setCausedByTouchEvent(newEvent,
1560                         meAccessor.isCausedByTouchEvent(event));
1561 
1562                     String tip = ((JComponent)rComponent).getToolTipText(
1563                                               newEvent);
1564 
1565                     if (tip != null) {
1566                         return tip;
1567                     }
1568                 }
1569             }
1570         }
1571         return super.getToolTipText();
1572     }
1573 
1574     /**
1575      * --- ListUI Delegations ---
1576      */
1577 
1578 
1579     /**
1580      * Returns the cell index closest to the given location in the list's


< prev index next >