Java Swing: JList with ListCellRenderer selected item different height

前端 未结 7 1597
花落未央
花落未央 2020-12-17 05:27

I\'m making a custom ListCellRenderer. I know that you can have different dimensions for each individual cell. But now I want to have a different dimension for the selected

7条回答
  •  一整个雨季
    2020-12-17 05:49

    this is a simple solution:

    public class VariableHeightListUI extends BasicListUI {
    
      @Override
      public void paint(Graphics g, JComponent c) {
        updateLayoutState();
        super.paint(g, c); 
      }
    }
    

    of course you need write your own implementation of ListCellRenderer, and according to different selection state of list element, you can set different prefer height of returned Component.

    Only one issue need to go on is : when you select an element of List FIRST time, not draw correctly. but after then, all work well.

    hope this can help you.

提交回复
热议问题