Java: Can't apply Gridlayout to a Jscrollpane. Get Get java.lang.ClassCastException

后端 未结 1 1234
伪装坚强ぢ
伪装坚强ぢ 2021-01-24 18:55

I use a Gridlayout to place 4 elements in one Line. First I had a JPanel and everything worked fine. For the case that the number of lines get to big and I have to be able to sc

相关标签:
1条回答
  • 2021-01-24 19:18

    scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error

    You can't change the layout manager of a scrollpane.

    A JScrollPane has its own custom layout manager because it needs to manage the horizontal/vertical scrollbars as well as the row/column headers etc..

    Instead you add a panel that uses a GridLayout:

    JPanel panel = new JPanel( new GridLayout(0, 4) );
    panel.add( component1 );
    panel.add( component2 );
    panel.add( component3 );
    panel.add( component4 );
    JScrollPane = new JScrollPane( panel );
    
    0 讨论(0)
提交回复
热议问题