【问题标题】:Get selected element's location in JList获取选定元素在 JList 中的位置
【发布时间】:2014-06-05 11:17:04
【问题描述】:

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

【问题讨论】:

    标签: java swing jlist


    【解决方案1】:

    你会想要使用JList#getCellBounds

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

    这将为您提供JList 上下文中所选项目的位置,您需要将其转换为屏幕空间...

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

    您可能还想看看The Use of Multiple JFrames: Good or Bad Practice?

    例如...

    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);
                }
            });
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      可以使用JListindexToLocation()方法,例如:

      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();
          }
      
      }
      

      【讨论】:

      • 有趣,但它不提供所选项目的高度,这意味着您的 JPopupMenu 将覆盖所选项目...据我所知,这不是 OP 想要的...虽然学到了一些东西;)
      • @MadProgrammer ,你是对的,但我认为 OP 可以更改 menu.show(list, indexToLocation.x, indexToLocation.y); 以在其他位置显示
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多