Java - JList 单击

发布于 2025-01-01 17:03:22 字数 248 浏览 1 评论 0 原文

我有一个带有 JScrollPaneJList 。如果 JList 中只有一项内容,则由于 JScrollPane 的原因,将会出现空白空间。如果我单击 JList 中的任意位置,它将单击一个元素。有没有办法让它只在我实际单击该元素时才单击该元素?另外,我正在使用 JList 的自定义渲染器来添加图像(如果与之相关的话)。

谢谢

I have a JList with a JScrollPane. If I only have one thing in the JList, there will be empty space due to the JScrollPane. If I click anywhere in the JList, it will click the one element. Is there a way to make it only click the element if I actually click it? Also, I am using a custom renderer for the JList to add images, if that has anything to do with it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸印温柔 2025-01-08 17:03:22

您可以通过 locationToIndex

返回列表中最接近给定位置的单元格索引
坐标系。确定单元格是否确实包含
指定位置,将该点与单元格的边界进行比较,如下
由 getCellBounds 提供。如果模型是,此方法返回-1
空。

You can check the index that was clicked via locationToIndex.

Returns the cell index closest to the given location in the list's
coordinate system. To determine if the cell actually contains the
specified location, compare the point against the cell's bounds, as
provided by getCellBounds. This method returns -1 if the model is
empty.

随波逐流 2025-01-08 17:03:22

这可以帮助你。

public class JListOperations extends javax.swing.JFrame {
    private static final long serialVersionUID = 1L;
    private javax.swing.JScrollPane jsp = null;
    private javax.swing.JList<java.lang.String> jl = null;
    public JListOperations(){
        super();    
    }
    public void initialiseComponents(){
        jl = new javax.swing.JList<java.lang.String>(new java.lang.String[]{"Item 1"});
        /*
         * Add mouse listener which checks if the mouse click point is on the item. If not deselect the item  
         */
        jl.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mouseClicked(java.awt.event.MouseEvent mouseEvent){
                if (!jl.getCellBounds(jl.getSelectedIndex(), jl.getSelectedIndex()).contains(mouseEvent.getPoint())){
                    jl.removeSelectionInterval(jl.getSelectedIndex(), jl.getSelectedIndex());
                }
                java.lang.System.out.println(jl.getSelectedIndex());
            }
        });
        jsp = new javax.swing.JScrollPane(jl);
        getContentPane().add(jsp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(java.lang.String args[]){
        JListOperations jlopFrame = new JListOperations();
        jlopFrame.initialiseComponents();
        jlopFrame.pack();
        jlopFrame.setVisible(true);
    }
}

This could help you.

public class JListOperations extends javax.swing.JFrame {
    private static final long serialVersionUID = 1L;
    private javax.swing.JScrollPane jsp = null;
    private javax.swing.JList<java.lang.String> jl = null;
    public JListOperations(){
        super();    
    }
    public void initialiseComponents(){
        jl = new javax.swing.JList<java.lang.String>(new java.lang.String[]{"Item 1"});
        /*
         * Add mouse listener which checks if the mouse click point is on the item. If not deselect the item  
         */
        jl.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mouseClicked(java.awt.event.MouseEvent mouseEvent){
                if (!jl.getCellBounds(jl.getSelectedIndex(), jl.getSelectedIndex()).contains(mouseEvent.getPoint())){
                    jl.removeSelectionInterval(jl.getSelectedIndex(), jl.getSelectedIndex());
                }
                java.lang.System.out.println(jl.getSelectedIndex());
            }
        });
        jsp = new javax.swing.JScrollPane(jl);
        getContentPane().add(jsp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(java.lang.String args[]){
        JListOperations jlopFrame = new JListOperations();
        jlopFrame.initialiseComponents();
        jlopFrame.pack();
        jlopFrame.setVisible(true);
    }
}
扎心 2025-01-08 17:03:22

不确定这是否适合您的情况,但您可以尝试 List.isFileList 属性:

list.putClientProperty("List.isFileList", Boolean.TRUE);

这将强制 ListUI 确保该点位于列表项的实际边界内(请参阅 SwingUtilities2.loc2IndexFileList)。

但是,设置此属性可能会更改列表的外观。

Not sure if this fits your case but you can try List.isFileList property:

list.putClientProperty("List.isFileList", Boolean.TRUE);

This will force ListUI to make sure that the point is within the actual bounds of a list item(see SwingUtilities2.loc2IndexFileList).

However, setting this property may change the look and feel of your list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文