javaswingjscrollpanejtextpanemiglayout

Why am I not getting scroll-bars with MigLayout manager?


I'm writing a desktop application. The environment is following:

The source-code, the important parts.

    textPane = new JTextPane();
    textPane.setDoubleBuffered(true);
    textPane.setLayout(new MigLayout("debug", "[grow]"));
    textPane.setContentType("text/plain");
    textPane.setBorder(new LineBorder(new Color(0, 255, 0), 1, true));
                            
    scrollPaneScratches = new JScrollPane(textPane);
    scrollPaneScratches.setAutoscrolls(true);

That's the JTextPane area where I add, at run-time, some components.

This is where I add the components in:

      for(int key : keys) {
        JLabel header = retrieveHeader(key, types, allScratches);
                                    
        textPane.add(header, "wrap, growy");
                                
        JTextPane txt = new JTextPane();
        txt.setText(allScratches.get(key).getScratches());
                
        textPane.add(txt, "wrap, growy");
     }

Now what the problem is, that the JTextPane, which displays the components added at run-time, is not scrollable. That's, the vertical scroll-bar supposed to appear, though but it does not.

Here is the image:

enter image description here

I tried different layout managers, but the contents is still not scrollable.

Why do not I get the scroll-bars? N.B., I did use the search engine on this, for about a day.

Best regards


Solution

  • Well, I resolved my issue by following the suggestions by @camickr, provided under the comments section.

    That's, the new components I add under JPanel, rather than on JTextPane. In that case, the scroll-bars do appear as expected.

    That is:

    GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.ipady = 20;
        constraints.ipadx = 0;
        constraints.weightx = 0.0;
        constraints.gridx = 0;
        constraints.gridy = GridBagConstraints.RELATIVE;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        
        JLabel header = retrieveHeader(key, types, allScratches);
                                                                                
        textPanel.add(header, constraints);