Java – JList with custom renderer

I have a JList that displays multiple jpanels, and I created a custom renderer that returns the new JPanel

thank you

Solution

This is JList (and jtabel, JCombo@R_236_2419 @Normal behavior

The JPanel returned by your custom renderer was not added to the swing hierarchy JList only uses its paint method to paint the renderer in the correct position The renderer is just a marker. What you see in JList is not a component, but an image of the component

This is an effective way to display many components on the screen without the overhead of actually instantiating components Note that your renderer can always return the same instance (even better)

For more information, see swing tutorial

If you want JList entries to look like real components, you can do the following First, use JTable instead of JList JTable with one column and no header is roughly the same as JList Why use JTable? Because JTable provides an editor The editor is registered on JTable, just like renderers When a user clicks on a JTable cell, an editor usually appears The editor is superimposed on the renderer, and this time it is a real component If the renderer and editor components are the same, the user feels that the JTable unit is a real component

Swing tutorial has examples of this technology

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
分享
二维码
< <上一篇
下一篇>>