📄 basiccombopopup.java
字号:
protected ListSelectionListener createListSelectionListener() { return new ListSelectionHandler(); } /** * Creates ListDataListener. This method returns null, because * ListDataHandler class is obsolete and is no longer used. * * @return null */ protected ListDataListener createListDataListener() { return null; } /** * This method creates ListMouseListener to listen to mouse events occuring * in the combo box's item list. * * @return MouseListener to listen to mouse events occuring in the combo * box's items list. */ protected MouseListener createListMouseListener() { return new ListMouseHandler(); } /** * Creates ListMouseMotionlistener to listen to mouse motion events occuring * in the combo box's list. This listener is responsible for highlighting * items in the list when mouse is moved over them. * * @return MouseMotionListener that handles mouse motion events occuring in * the list of the combo box. */ protected MouseMotionListener createListMouseMotionListener() { return new ListMouseMotionHandler(); } /** * Creates PropertyChangeListener to handle changes in the JComboBox's bound * properties. * * @return PropertyChangeListener to handle changes in the JComboBox's bound * properties. */ protected PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); } /** * Creates new ItemListener that will listen to ItemEvents occuring in the * combo box. * * @return ItemListener to listen to ItemEvents occuring in the combo box. */ protected ItemListener createItemListener() { return new ItemHandler(); } /** * Creates JList that will be used to display items in the combo box. * * @return JList that will be used to display items in the combo box. */ protected JList createList() { JList l = new JList(comboBox.getModel()); l.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); return l; } /** * This method configures the list of comboBox's items by setting default * properties and installing listeners. */ protected void configureList() { list.setModel(comboBox.getModel()); list.setVisibleRowCount(comboBox.getMaximumRowCount()); installListListeners(); } /** * This method installs list listeners. */ protected void installListListeners() { // mouse listener listening to mouse events occuring in the // combo box's list of items. listMouseListener = createListMouseListener(); list.addMouseListener(listMouseListener); // mouse listener listening to mouse motion events occuring in the // combo box's list of items listMouseMotionListener = createListMouseMotionListener(); list.addMouseMotionListener(listMouseMotionListener); listSelectionListener = createListSelectionListener(); list.addListSelectionListener(listSelectionListener); } /** * This method creates scroll pane that will contain the list of comboBox's * items inside of it. * * @return JScrollPane */ protected JScrollPane createScroller() { return new JScrollPane(); } /** * This method configures scroll pane to contain list of comboBox's items */ protected void configureScroller() { scroller.setBorder(null); scroller.getViewport().setView(list); scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); } /** * This method configures popup menu that will be used to display Scrollpane * with list of items inside of it. */ protected void configurePopup() { setBorder(BorderFactory.createLineBorder(Color.BLACK)); // initialize list that will be used to display combo box's items this.list = createList(); ((JLabel) list.getCellRenderer()).setHorizontalAlignment(SwingConstants.LEFT); configureList(); // initialize scroller. Add list to the scroller. scroller = createScroller(); configureScroller(); // add scroller with list inside of it to JPopupMenu super.add(scroller); } /* * This method installs listeners that will listen to changes occuring * in the combo box. */ protected void installComboBoxListeners() { // mouse listener that listens to mouse event in combo box mouseListener = createMouseListener(); comboBox.addMouseListener(mouseListener); // mouse listener that listens to mouse dragging events in the combo box mouseMotionListener = createMouseMotionListener(); comboBox.addMouseMotionListener(mouseMotionListener); // item listener listenening to selection events in the combo box itemListener = createItemListener(); comboBox.addItemListener(itemListener); propertyChangeListener = createPropertyChangeListener(); comboBox.addPropertyChangeListener(propertyChangeListener); } /** * This method installs listeners that will listen to changes occuring in * the comb box's data model * * @param model data model for the combo box for which to install listeners */ protected void installComboBoxModelListeners(ComboBoxModel model) { // list data listener to listen for ListDataEvents in combo box. // This listener is now obsolete and nothing is done here listDataListener = createListDataListener(); comboBox.getModel().addListDataListener(listDataListener); } /** * DOCUMENT ME! */ protected void installKeyboardActions() { // FIXME: Need to implement } /** * This method always returns false to indicate that items in the combo box * list are not focus traversable. * * @return false */ public boolean isFocusTraversable() { return false; } /** * This method start scrolling combo box's list of items either up or down * depending on the specified 'direction' * * @param direction of the scrolling. */ protected void startAutoScrolling(int direction) { // FIXME: add timer isAutoScrolling = true; if (direction == SCROLL_UP) autoScrollUp(); else autoScrollDown(); } /** * This method stops scrolling the combo box's list of items */ protected void stopAutoScrolling() { // FIXME: add timer isAutoScrolling = false; } /** * This method scrolls up list of combo box's items up and highlights that * just became visible. */ protected void autoScrollUp() { // scroll up the scroll bar to make the item above visible JScrollBar scrollbar = scroller.getVerticalScrollBar(); int scrollToNext = list.getScrollableUnitIncrement(super.getBounds(), SwingConstants.VERTICAL, SCROLL_UP); scrollbar.setValue(scrollbar.getValue() - scrollToNext); // If we haven't reached the begging of the combo box's list of items, // then highlight next element above currently highlighted element if (list.getSelectedIndex() != 0) list.setSelectedIndex(list.getSelectedIndex() - 1); } /** * This method scrolls down list of combo box's and highlights item in the * list that just became visible. */ protected void autoScrollDown() { // scroll scrollbar down to make next item visible JScrollBar scrollbar = scroller.getVerticalScrollBar(); int scrollToNext = list.getScrollableUnitIncrement(super.getBounds(), SwingConstants.VERTICAL, SCROLL_DOWN); scrollbar.setValue(scrollbar.getValue() + scrollToNext); // If we haven't reached the end of the combo box's list of items // then highlight next element below currently highlighted element if (list.getSelectedIndex() + 1 != comboBox.getItemCount()) list.setSelectedIndex(list.getSelectedIndex() + 1); } /** * This method helps to delegate focus to the right component in the * JComboBox. If the comboBox is editable then focus is sent to * ComboBoxEditor, otherwise it is delegated to JComboBox. * * @param e MouseEvent */ protected void delegateFocus(MouseEvent e) { // FIXME: Need to implement } /** * This method displays combo box popup if the popup is not currently shown * on the screen and hides it if it is currently visible */ protected void togglePopup() { if (BasicComboPopup.this.isVisible()) hide(); else show(); } /** * DOCUMENT ME! * * @param e DOCUMENT ME! * * @return DOCUMENT ME! */ protected MouseEvent convertMouseEvent(MouseEvent e) { return null; } /** * Returns required height of the popup such that number of items visible in * it are equal to the maximum row count. By default * comboBox.maximumRowCount=8 * * @param maxRowCount number of maximum visible rows in the combo box's * popup list of items * * @return height of the popup required to fit number of items equal to * JComboBox.maximumRowCount. */ protected int getPopupHeightForRowCount(int maxRowCount) { int totalHeight = 0; ListCellRenderer rend = list.getCellRenderer(); if (comboBox.getItemCount() < maxRowCount) maxRowCount = comboBox.getItemCount(); for (int i = 0; i < maxRowCount; i++) { Component comp = rend.getListCellRendererComponent(list, comboBox.getModel() .getElementAt(i), -1, false, false); Dimension dim = comp.getPreferredSize(); totalHeight += dim.height; } return totalHeight; } /** * DOCUMENT ME! * * @param px DOCUMENT ME! * @param py DOCUMENT ME! * @param pw DOCUMENT ME! * @param ph DOCUMENT ME! * * @return DOCUMENT ME! */ protected Rectangle computePopupBounds(int px, int py, int pw, int ph) { return new Rectangle(px, py, pw, ph); } /** * This method changes the selection in the list to the item over which the * mouse is currently located. * * @param anEvent MouseEvent * @param shouldScroll DOCUMENT ME! */ protected void updateListBoxSelectionForEvent(MouseEvent anEvent, boolean shouldScroll) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -