/* * [TestJScrollPane.java] * * Summary: demonstrate the use of javax.swing.JScrollPane. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-09-20 initial version */ package com.mindprod.example; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import java.awt.Container; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; /** * demonstrate the use of javax.swing.JScrollPane. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-09-20 initial version * @since 2011-09-20 */ public final class TestJScrollPane { /** * is user busy with the scroller? */ private static boolean userBusyScrolling; /** * Debugging harness for a Frame * * @param args command line arguments are ignored. */ public static void main( String args[] ) { SwingUtilities.invokeLater( new Runnable() { /** * fire up a JFrame on the Swing thread */ public void run() { final JFrame jFrame = new JFrame(); final Container contentPane = jFrame.getContentPane(); // The user hitting enter inserts a \n character into the text. final JEditorPane jEditorPane = new JEditorPane(); jEditorPane.setContentType( "text/html" ); jEditorPane.setEditable( false ); jEditorPane.setText( "The destructive Seven Blunders of the World that cause " + "violence:\n" + "
  1. Wealth without work.
  2. \n" + "
  3. Pleasure without conscience.
  4. \n" + "
  5. Knowledge without character.
  6. \n" + "
  7. Commerce without morality.
  8. \n" + "
  9. Science without humanity.
  10. \n" + "
  11. Religion without sacrifice.
  12. \n" + "
  13. Politics without principle.
\n" + "~ Mahatma Gandhi\n" ); // add what you want to scroll, e.g. a JTextPane, JTextArea or JPanel // other option is VERTICAL_SCROLLBAR_NEVER to turn off bar. final JScrollPane scroller = new JScrollPane( jEditorPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); // control the speed effect of the wheelmouse scroller.getVerticalScrollBar().setUnitIncrement( 16 ); scroller.getVerticalScrollBar().addAdjustmentListener( new AdjustmentListener() { /** * detect user fiddling with the scroller */ public void adjustmentValueChanged( AdjustmentEvent e ) { userBusyScrolling = e.getValueIsAdjusting(); } } ); // GOTCHA! does nothing. Must wait until rendering complete. scroller.getVerticalScrollBar().setValue( 0 ); // Using the default VERTICAL_SCROLLBAR_AS_NEEDED will use scrollbars even when // not strictly needed. If you want precise control you must decide for yourself // dynamically. scroller.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ); contentPane.add( scroller /* not jEditorPane! */ ); jFrame.setSize( 270, 120 ); jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); jFrame.validate(); jFrame.setVisible( true ); } } ); } // end main }