Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

java - JComboBox determine if Items are/aren't visible in drop-down list

I tried to determime for every Items if are or not visible in the JViewPort from JComboBox drop-down list

(my Friday OT)

EDIT: I don't want to implements MouseListener for Repeats events to System.out.print(...)

isn't possible pass JComboBox with JList, declared by JCombo#Model by using SwingUtilities http://download.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html , but this APi is out of my...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ItemVisibleRecCombo extends JFrame {

    private static final long serialVersionUID = 1L;
    private JComboBox fontsBox;

    public ItemVisibleRecCombo() {
        String[] numbers = {"one", "two", "three", "four", "five", "six", "seven"};
        fontsBox = new JComboBox(numbers);
        fontsBox.setSelectedItem(0);
        fontsBox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    manItemInCombo();
                }
            }
        });
        fontsBox.setModel(new DefaultComboBoxModel(numbers));
        fontsBox.setMaximumRowCount(3);
        add(fontsBox, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(400, 60));
        setLocation(200, 105);
        pack();
        setVisible(true);
    }

    private void manItemInCombo() {
        if (fontsBox.getItemCount() > 0) {
            final Object comp = fontsBox.getUI().getAccessibleChild(fontsBox, 0);
            if ((comp instanceof JPopupMenu)) {
                final JList list = new JList(fontsBox.getModel());
                final JPopupMenu popup = (JPopupMenu) comp;
                final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
                final JViewport viewport = scrollPane.getViewport();
                final Rectangle rect = popup.getVisibleRect();
                Point pt = viewport.getViewPosition();
                for (int i = 0; i < list.getModel().getSize(); i++) {
                    pt = list.indexToLocation(i);
                    System.out.print(pt + " - ");
                    rect.setLocation(rect.x - pt.x, rect.y - pt.y);
                    System.out.println(new Rectangle(viewport.getExtentSize()).contains(rect));
                }
            }
        }
    }

    public static void main(String arg[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ItemVisibleRecCombo ivrc = new ItemVisibleRecCombo();
            }
        });
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basically, you'r looking for list.locationToIndex (if I understood you correctly), something like

    Accessible a = fontsBox.getUI().getAccessibleChild(fontsBox, 0);
    if (a instanceof javax.swing.plaf.basic.ComboPopup) {
        JList list = ((javax.swing.plaf.basic.ComboPopup)a).getList();
        Rectangle rect = list.getVisibleRect();
        int first = list.locationToIndex(rect.getLocation());
        // similar for last, at the lower edge of the visible rect, left as exercise <g>
        // Edit: as of @Boro's comment, last is easier calculated with maxRowCount
        int last = first + fontsBox.getMaximumRowCount() - 1;
        ....

BTW, yet another property that's not passed on to the list: would have expected

   list.getVisibleRowCount() == combo.getMaximumRowCount()   

To answer the question: all items between first/last, inclusively, are visible, all items above first and below last not visible ;-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...