Java – get components from JList by clicking the location

How do I get components from JList by clicking on a location?

I have my own list cell renderer, I insert some panels and labels Now I want to get, for example, the tab the user clicks

I tried the method list getComponentAt(evt.getPoint()); But it only returns the entire JList

Solution

I haven't tested this, but the basics are

>Use jlist#locationtoindex (point) to get the index of the element and the given point. > Gets the "element" at the specified index (using JList's #getmodel#getelementat (int)). > Use jlist#getcellrenderer to get listcellrenderer. > Render the element and get its component representation > set the boundary of the renderer to the desired cell boundary > convert the original point to components context > use getcomponentat on the renderer

Maybe, like

int index = list.locationToIndex(p);
Object value = list.getModel().getElementAt(int);
Component comp = listCellRenderer.getListCellRendererComponent(list,value,index,true,true);
comp.setBounds(list.getCellBounds(index,index));
Point contextPoint = SwingUtilities.convertPoint(list,p,comp);
Component child = comp.getComponentAt(contextPoint);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>