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

倾然丶 夕夏残阳落幕 提交于 2019-12-02 04:48:07

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 );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!