0

I am creating a simple JTextPane and setting a long text after viewport setting, but when I run program in Mac os 10.11.5, my JTextPane text lines collapses with each other. When I scroll slowly it does not happen, but when I scroll little fast it starts collapsing. This problem is not occurring in windows. Here is my sample source code:

public class JTextPaneScroll extends javax.swing.JFrame {

    private String s = "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "\n For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity."
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "\n For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity."
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "\n For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity.";

    public JTextPaneScroll() {
        initComponents();
        setTextToPane();
    }
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          

    private void initComponents() {

        jspcomp = new javax.swing.JScrollPane();
        comp = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jspcomp.setViewportView(comp);

        getContentPane().add(jspcomp, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 290, 180));

        pack();
    }// </editor-fold>                        

    private void setTextToPane() {
        try {
            comp.setText(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JTextPaneScroll().setVisible(true);
            }
        });
    }

// Variables declaration - do not modify                     
    private javax.swing.JTextPane comp;
    private javax.swing.JScrollPane jspcomp;
// End of variables declaration                   
}

this is the sample image of issue

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • `getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());` The `AbsoluteLayout` is really just a fancy way of saying 'no layout'. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 31 '16 at 11:28
  • i have tried that also with BorderLayout, and have also tried with setting styled document. but still facing the same issue. – user3098231 May 31 '16 at 11:39
  • *"tried that also with BorderLayout"* I'd be prepared to help fix that version. Where is it? – Andrew Thompson May 31 '16 at 11:43
  • I mean when i faced this issue then i thought it may be because of Layout manager so i have set the layout. i added the jscrollpane in panel and set the BorderLayout for that panel. but it did not work.then i took different approach and i set the styled document for jtextpane and insert string in document using " doc.insertString(0, content, attribute); " , again i faced this text collapse issue. – user3098231 May 31 '16 at 11:48
  • *"I mean.."* ***I*** mean I could not give a stuff if a `null` layout breaks. I care enough about the version using a `BorderLayout` enough to try help fix it. – Andrew Thompson May 31 '16 at 15:50

1 Answers1

3

JTextPane is Scrollable, so you can make it any preferred size you want and still use any layout you want. Components get added to the BorderLayout.CENTER of the JFrame by default. You can resize the frame to see what happens.

textPane = new javax.swing.JTextPane() {
    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(290, 192);
    }
};

screenshot

import java.awt.Dimension;

public class JTextPaneScroll extends javax.swing.JFrame {

    private javax.swing.JTextPane textPane;
    private javax.swing.JScrollPane scrollPane;

    private String s = ""
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity.\n"
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity.\n"
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity.\n";

    public JTextPaneScroll() {
        initComponents();
        setTextToPane();
    }

    private void initComponents() {
        scrollPane = new javax.swing.JScrollPane();
        textPane = new javax.swing.JTextPane() {
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(290, 192);
            }
        };
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        scrollPane.setViewportView(textPane);
        add(scrollPane);
        pack();
    }

    private void setTextToPane() {
        try {
            textPane.setText(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String args[]) {
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JTextPaneScroll().setVisible(true);
            }
        });
    }
}
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • still facing same problem on scroll. @Catalina Island, The problem is occurring on scrolling fast vertically. Even with using "getPreferredScrollableViewportSize()" scrolling is not working not showing text collapse problem. Please run this program on Mac OS 10.11 or later and scroll up and down little fast , you will see the issue. – user3098231 Jun 01 '16 at 06:31
  • somehow i have figured out the solution. i have added scrollPane.addMouseWheelListener(new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent mwe) { comp.updateUI(); } }); Now now on fast scroll I'm not facing text collapse issue. I think there must be other better solution instead of updating UI because it may harm performance. but currently it resolved my issue in mac. – user3098231 Jun 01 '16 at 08:06
  • @user3098231: I agree that `updateUI()` shouldn't be necessary. On my Mac with 10.11.5 and JavA 8, the mouse wheel works without a listener. Update your question with your new [mcve], and I'll give it a try. – Catalina Island Jun 01 '16 at 11:13
  • The program i have pasted above is minimal and complete and if you run you can see the issue on mac when you scroll up and down little bit faster. – user3098231 Jun 04 '16 at 10:36
  • Yeah, I see it, too. I don't know better way to fix it except to change the layout. My example uses the frame's default, `BorderLayout.CENTER`. – Catalina Island Jun 06 '16 at 11:16