Java: Can't apply Gridlayout to a Jscrollpane.

2019-02-20 07:10发布

问题:

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 scroll down, I changed it a bit. Now I have my JPanel with one JScrollPane added on it. I used the same code, now I just add the elements to the viewport of the Jscrollpane, but now I get this exception Get java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout: at javax.swing.JScrollPane.setLayout(Unknown Source) and I dont know exactly why. Why shouldnt be Gridlayout's be unknown for Jscrollpane?

Here is the code:

    public objectDetails() {
    setTitle("LLI_MC_Solver");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    contentPane = new JPanel();
    contentPane.setLayout(new GridLayout());
    setBounds(100, 100, 510, 401);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setVisible(true);
    contentPane.setPreferredSize(new Dimension(500, 390));

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0), 2));
    scrollPane.setBounds(10, 10, 474, 342);
    scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error
    scrollPane.setPreferredSize(new Dimension(465, 330));
    contentPane.add(scrollPane);

    JPanel view = (JPanel)scrollPane.getViewport().getView();

        for(Values v : colDetails)
        {
            JLabel lblC = new JLabel();
            lblC.setText(k);
            view.add(lblC);
            view.validate();

            JLabel lblN = new JLabel();
            lblN.setText(v.getName());
            view.add(lblN);
            view.validate();

            JLabel lblT = new JLabel();
            lblT.setText(v.getType());
            view.add(lblT);
            view.validate();

            JTextField valueBox = new JTextField();
            valueBox.setText(v.getValue());
            view.add(valueBox);
            view.validate();

        }
}

I marked the line which causes the Problem according to the compiler. I dont understand why, with the JPanel the same code worked fine. The for-loop where the elements are added I posted for completion purposes, the issue must be somewhere in the setLayout()-Method.

Thanks in advance, appreciate every help.

回答1:

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 );