Java Code Examples for javax.swing.JList#locationToIndex()

The following examples show how to use javax.swing.JList#locationToIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractPageListPopupListener.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Show popup menu in response to a mouse event.
 * 
 * @param e Event.
 */
@Override
protected void showPopup(MouseEvent e) {

  // Retrieve information
  if (!(e.getComponent() instanceof JList)) {
    return;
  }
  JList tmpList = (JList) e.getComponent();
  int position = tmpList.locationToIndex(e.getPoint());
  if (position < 0) {
    return;
  }
  Object object = tmpList.getModel().getElementAt(position);
  if (!(object instanceof Page)) {
    return;
  }
  Page link = (Page) object;
  showPopup(tmpList, link, e.getX(), e.getY());
}
 
Example 2
Source File: JideCheckBoxListItemElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public static boolean clicksInCheckBox(Point e, JList list) {
    int index = list.locationToIndex(e);
    int hotspot = new JCheckBox().getPreferredSize().width;
    Rectangle bounds = list.getCellBounds(index, index);
    if (bounds != null) {
        if (list.getComponentOrientation().isLeftToRight()) {
            return e.getX() < bounds.x + hotspot;
        } else {
            return e.getX() > bounds.x + bounds.width - hotspot;
        }
    } else {
        return false;
    }
}
 
Example 3
Source File: ViewTooltips.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void showJList (JScrollPane view, Point pt) {
    JList list = (JList) view.getViewport().getView();
    Point p = SwingUtilities.convertPoint(view, pt.x, pt.y, list);
    int row = list.locationToIndex(p);
    if (row == -1) {
        hide();
        return;
    }
    Rectangle bds = list.getCellBounds(row, 
            row);
    //GetCellBounds returns a width that is the
    //full component width;  we want only what
    //the renderer really needs.
    ListCellRenderer ren = list.getCellRenderer();
    Dimension rendererSize = 
            ren.getListCellRendererComponent(list, 
            list.getModel().getElementAt(row), 
            row, false, false).getPreferredSize();
    
    bds.width = rendererSize.width;
    if (bds == null || !bds.contains(p)) {
        hide();
        return;
    }
    if (setCompAndRow (list, row)) {
        Rectangle visible = getShowingRect (view);
        Rectangle[] rects = getRects (bds, visible);
        if (rects.length > 0) {
            ensureOldPopupsHidden();
            painter.configure(
                    list.getModel().getElementAt(row), 
                    view, list, row);
            showPopups (rects, bds, visible, list, view);
        } else {
            hide();
        }
    }
}
 
Example 4
Source File: EventsBreakpointCustomizer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String getSelectedText(JList list, MouseEvent e) {
    int index = list.locationToIndex(e.getPoint());
    if (index >= 0) {
        int x = e.getX();
        if (x < checkBoxWidth) {
            return (String) list.getModel().getElementAt(index);
        }
    }
    return null;
}
 
Example 5
Source File: CheckListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    if (e.isPopupTrigger()) {
        return;
    }
    if (!(e.getSource() instanceof JList)) {
        return;
    }
    JList list = (JList) e.getSource();
    int index = list.locationToIndex(e.getPoint());
    if (index < 0) {
        return;
    }
    toggle(list.getModel().getElementAt(index));
}
 
Example 6
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * A variation of locationToIndex() which only returns an index if the
 * Point is within the actual bounds of a list item (not just in the cell)
 * and if the JList has the "List.isFileList" client property set.
 * Otherwise, this method returns -1.
 * This is used to make Windows {@literal L&F} JFileChooser act
 * like native dialogs.
 */
public static int loc2IndexFileList(JList<?> list, Point point) {
    int index = list.locationToIndex(point);
    if (index != -1) {
        Object bySize = list.getClientProperty("List.isFileList");
        if (bySize instanceof Boolean && ((Boolean)bySize).booleanValue() &&
            !pointIsInActualBounds(list, index, point)) {
            index = -1;
        }
    }
    return index;
}
 
Example 7
Source File: CommonSettingsDialog.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
    Object src = e.getSource();
    if (mouseDragging && (src instanceof JList)) {
        JList<?> srcList = (JList<?>) src;
        DefaultListModel<?> srcModel = (DefaultListModel<?>) srcList.getModel();
        int currentIndex = srcList.locationToIndex(e.getPoint());
        if (currentIndex != dragSourceIndex) {
            int dragTargetIndex = srcList.getSelectedIndex();
            moveElement(srcModel, dragSourceIndex, dragTargetIndex);
            dragSourceIndex = currentIndex;
        }
    }
}
 
Example 8
Source File: WeaponPanel.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
    removeListeners();
    
    Object src = e.getSource();
    // Check to see if we are in a state we care about
    if (!mouseDragging || !(src instanceof JList)) {
        return;
    }
    JList<?> srcList = (JList<?>) src;
    WeaponListModel srcModel = (WeaponListModel) srcList.getModel();
    int currentIndex = srcList.locationToIndex(e.getPoint());
    if (currentIndex != dragSourceIndex) {
        int dragTargetIndex = srcList.getSelectedIndex();
        Mounted weap1 = srcModel.getWeaponAt(dragSourceIndex);
        srcModel.swapIdx(dragSourceIndex, dragTargetIndex);
        dragSourceIndex = currentIndex;
        Entity ent = weap1.getEntity();
        // Is the sort order custom?
        int customId = Entity.WeaponSortOrder.CUSTOM.ordinal();
        // Update weap sort order drop down
        if (weapSortOrder.getSelectedIndex() != customId) {
            // Set the order to custom
            ent.setWeaponSortOrder(Entity.WeaponSortOrder.CUSTOM);
            weapSortOrder.setSelectedIndex(customId);
        }
        // Update custom order
        for (int i = 0; i < srcModel.getSize(); i++) {
            Mounted m = srcModel.getWeaponAt(i);
            ent.setCustomWeaponOrder(m, i);
        }
        if (unitDisplay.getClientGUI() != null) {
            unitDisplay.getClientGUI().getMenuBar()
                    .updateSaveWeaponOrderMenuItem();
        }
    }
    addListeners();
}
 
Example 9
Source File: FontBoxRenderer.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
private void manItemInCombo() {
  if (comboBox.getItemCount() > 0) {
    final Object comp = comboBox.getUI().getAccessibleChild(comboBox, 0);
    if ((comp instanceof JPopupMenu)) {
      final JList list = new JList(comboBox.getModel());
      final JPopupMenu popup = (JPopupMenu) comp;
      final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
      final JViewport viewport = scrollPane.getViewport();
      final Rectangle rect = popup.getVisibleRect();
      final Point pt = viewport.getViewPosition();
      row = list.locationToIndex(pt);
    }
  }
}
 
Example 10
Source File: PageListAnalyzeListener.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
  if (e.getButton() != MouseEvent.BUTTON1) {
    return;
  }
  if (e.getClickCount() != 2) {
    return;
  }
  if (!(e.getComponent() instanceof JList)) {
    return;
  }
  JList list = (JList) e.getComponent();
  int position = list.locationToIndex(e.getPoint());
  if (position < 0) {
    return;
  }
  Object object = list.getModel().getElementAt(position);
  if (!(object instanceof Page)) {
    return;
  }
  Page page = (Page) object;
  ArrayList<Page> knownPages = null;
  if ((pageWindow != null) && (pageWindow.getPage() != null)) {
    Page basePage = pageWindow.getPage();
    knownPages = new ArrayList<Page>(1);
    knownPages.add(basePage);
    for (Page backLink : basePage.getAllLinksToPage()) {
      PageRedirect redirects = backLink.getRedirects();
      if ((redirects != null) &&
          (redirects.isRedirect()) &&
          (Page.areSameTitle(basePage.getTitle(), redirects.getTitle()))) {
        knownPages.add(backLink);
      }
    }
  }
  OnePageAnalysisWindow.createAnalysisWindow(page.getTitle(), knownPages, wikipedia);
}
 
Example 11
Source File: ForwardLookupInit.java    From yeti with MIT License 5 votes vote down vote up
public void mouseClicked(MouseEvent event) {
    JList list = (JList) event.getSource();
    int index = list.locationToIndex(event.getPoint());
    CheckListItem item = (CheckListItem) list.getModel().getElementAt(index);
    item.setSelected(!item.isSelected());
    list.repaint(list.getCellBounds(index, index));
}
 
Example 12
Source File: JListJavaElement.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
public static int getIndexAt(JList list, Point point) {
    if (point == null)
        return list.getSelectedIndex();
    return list.locationToIndex(point);
}