спасибо, но ваши предположения были неверны, здесь вывод, но я не могу проверить, содержит ли ViewPort Item или нет, нет, я должен вернуться к исходному объявлению, потому что это показывает первый видимый Item правильноphaaaa
РЕДАКТИРОВАТЬ @ Anthony Accioly за правильное предложение Changing the listener from ItemListener to ActionListener
неправильный выводВ ItemListener
1stIndex = 0, LastIndex = 2, SelectedItem1 = two, Value = one, two, three//ok
1stIndex = 0, LastIndex = 2, SelectedItem1 = three, Value = one, two, three//ok
1stIndex = 0, LastIndex = 2, SelectedItem1 = four, Value = one, two, three//wrong
1stIndex = 1, LastIndex = 3, SelectedItem1 = five, Value = two, three, four//wrong
1stIndex = 2, LastIndex = 4, SelectedItem1 = six, Value = three, four, five//wrong
1stIndex = 3, LastIndex = 5, SelectedItem1 = seven, Value = four, five, six//wrong
1stIndex = 4, LastIndex = 6, SelectedItem1 = six, Value = five, six, seven//ok
1stIndex = 4, LastIndex = 6, SelectedItem1 = five, Value = five, six, seven//ok
1stIndex = 4, LastIndex = 6, SelectedItem1 = four, Value = five, six, seven//wrong
1stIndex = 3, LastIndex = 5, SelectedItem1 = three, Value = four, five, six//wrong
1stIndex = 2, LastIndex = 4, SelectedItem1 = two, Value = three, four, five//wrong
1stIndex = 1, LastIndex = 3, SelectedItem1 = one, Value = two, three, four//wrong
и ожидаемый результат от ActionListener
1stIndex = 0, LastIndex = 2, SelectedItem1 = two, Value = one, two, three
1stIndex = 0, LastIndex = 2, SelectedItem1 = three, Value = one, two, three
1stIndex = 1, LastIndex = 3, SelectedItem1 = four, Value = two, three, four
1stIndex = 2, LastIndex = 4, SelectedItem1 = five, Value = three, four, five
1stIndex = 3, LastIndex = 5, SelectedItem1 = six, Value = four, five, six
1stIndex = 4, LastIndex = 6, SelectedItem1 = seven, Value = five, six, seven
1stIndex = 4, LastIndex = 6, SelectedItem1 = six, Value = five, six, seven
1stIndex = 4, LastIndex = 6, SelectedItem1 = five, Value = five, six, seven
1stIndex = 3, LastIndex = 5, SelectedItem1 = four, Value = four, five, six
1stIndex = 2, LastIndex = 4, SelectedItem1 = three, Value = three, four, five
1stIndex = 1, LastIndex = 3, SelectedItem1 = two, Value = two, three, four
1stIndex = 0, LastIndex = 2, SelectedItem1 = one, Value = one, two, three
Отредактированный код
import java.awt.*;
import java.awt.event.*;
import javax.accessibility.Accessible;
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.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
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 Accessible a = fontsBox.getUI().getAccessibleChild(fontsBox, 0);
if (a instanceof javax.swing.plaf.basic.ComboPopup) {
final JList list = ((javax.swing.plaf.basic.ComboPopup) a).getList();
final Rectangle rect = list.getVisibleRect();
final int first = list.locationToIndex(rect.getLocation());
final int last = first + fontsBox.getMaximumRowCount() - 1;
String selectedItem = fontsBox.getSelectedItem().toString();
System.out.println("1stIndex = " + first + ", LastIndex = "
+ last + ", SelectedItem1 = " + selectedItem
+ ", Value = " + fontsBox.getItemAt(first).toString()
+ ", " + fontsBox.getItemAt(first + 1).toString()
+ ", " + fontsBox.getItemAt(first + 2).toString());
}
}
}
public static void main(String arg[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ItemVisibleRecCombo ivrc = new ItemVisibleRecCombo();
}
});
}
}