获取所选元素在JList中的位置

问题描述 投票:0回答:2

是否可以获取 JList 中所选元素的位置?我想让它把 JFrame 放置在单击的选项下方。

java swing jlist
2个回答
6
投票

您会想要使用

JList#getCellBounds

int selectedIndex = list.getSelectedIndex();
Rectangle bounds = list.getCellBounds(selectedIndex, selectedIndex);

这将为您提供所选项目在

JList
上下文中的位置,您需要将其转换为屏幕空间...

Point p = bounds.getLocation();
SwingUtilities.convertPointToScreen(p, list);

您可能还想看看 多个 JFrame 的使用:好还是坏实践?

例如...

import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class List {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new List();
    }
    
    public List() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                
                final JFrame selected = new JFrame("Selected");
                selected.add(new JLabel("Here I am"));
                selected.pack();
                
                DefaultListModel model = new DefaultListModel();
                for (int index = 0; index < 1000; index++) {
                    model.addElement("#" + index);
                }
                final JList list = new JList(model);
                list.addListSelectionListener(new ListSelectionListener() {
                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        int index = list.getSelectedIndex();
                        Rectangle bounds = list.getCellBounds(index, index);
                        Point p = bounds.getLocation();
                        p.y += bounds.height;
                        SwingUtilities.convertPointToScreen(p, list);
                        p.x -= selected.getWidth();
                        selected.setLocation(p);
                        selected.setVisible(true);
                    }
                });
                
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(list));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
    
}

3
投票

可以使用indexToLocation()

JList
方法,例如:

import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestFrame extends JFrame {

    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setVisible(true);
    }

    private void init() {
        final JPopupMenu menu = new JPopupMenu("test");
        menu.add(new JMenuItem("1"));
        final JList<String> list = new JList<String>(new String[]{"1","2","3"});
        list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int selectedIndex = list.getSelectedIndex();
                if(selectedIndex != -1){
                    Point indexToLocation = list.indexToLocation(selectedIndex);
                    Rectangle cellBounds = list.getCellBounds(selectedIndex, selectedIndex);
                    menu.show(list, indexToLocation.x, indexToLocation.y+cellBounds.height);
                }
            }
        });

        add(list);
    }

    public static void main(String... strings) {
        new TestFrame();
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.