Advertisement
Guest User

Untitled

a guest
Nov 9th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package javaapplication74;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class APanel extends JScrollPane {
  8.  
  9.     int width = 0;
  10.  
  11.     public static String getRandomMultilineText() {
  12.         String filler = "";
  13.         int words = (int) (Math.random() * 7) + 1;
  14.         for ( int w = 0 ; w < words ; w++ ) {
  15.             int lettersInWord = (int) (Math.random() * 12) + 1;
  16.             for ( int l = 0 ; l < lettersInWord ; l++ ) {
  17.                 filler += "a";
  18.             }
  19.             filler += "\n";
  20.         }
  21.         return filler.trim();
  22.     }
  23.  
  24.     public APanel() {
  25.         super();
  26.  
  27.         setAlignmentX( LEFT_ALIGNMENT );
  28.         setAlignmentY( TOP_ALIGNMENT );
  29.  
  30.         final Box B = Box.createVerticalBox();
  31.         B.setAlignmentX( LEFT_ALIGNMENT );
  32.         B.setAlignmentY( TOP_ALIGNMENT );
  33.  
  34.         for ( int i = 0 ; i < 4 ; i++ ) {
  35.             B.add( new CPanel() {
  36.  
  37.                
  38.                 //Important!!! Make sure the width always fits the screen
  39.                 public Dimension getPreferredSize() {
  40.                    
  41.                    
  42.                     Dimension result = super.getPreferredSize();
  43.                     result.width = width - 20; // 20 is for the scroll bar width
  44.                     return result;
  45.                 }
  46.             } );
  47.         }
  48.  
  49.         setViewportView( B );
  50.  
  51.         //Important!!! Need to invalidate the Scroll pane, othewise it
  52.         //doesn't try to lay out when the container is shrunk
  53.         addComponentListener( new ComponentAdapter() {
  54.             public void componentResized( ComponentEvent ce ) {
  55.                 width = getWidth();
  56.                 B.invalidate();
  57.             }
  58.         } );
  59.     }
  60.  
  61.     // nothing really very special in this class - mostly here for demonstration
  62.     public static class CPanel extends JPanel {
  63.  
  64.         public CPanel() {
  65.             super( new WrapLayout( WrapLayout.LEFT ) );
  66.             setOpaque( true );
  67.             setBackground( Color.gray );
  68.             setAlignmentY( TOP_ALIGNMENT );
  69.             setAlignmentX( LEFT_ALIGNMENT );
  70.  
  71.             int wordGroups = (int) (Math.random() * 14) + 7;
  72.  
  73.             //Adding test data (TextAreas)
  74.             for ( int i = 0 ; i < wordGroups ; i++ ) {
  75.                
  76.                 JTextArea ta = new JTextArea( getRandomMultilineText() );
  77.                 ta.setAlignmentY( TOP_ALIGNMENT );
  78.                
  79.                 add( ta );
  80.             }
  81.  
  82.             setBorder( BorderFactory.createTitledBorder( "Lovely container" ) );
  83.         }
  84.     }
  85.  
  86.     public static void main( String[] args ) {
  87.         final JFrame frame = new JFrame();
  88.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  89.         frame.add( new APanel() );
  90.         frame.pack();
  91.         frame.setSize( 400 , 300 );
  92.         frame.setLocationRelativeTo( null );
  93.         frame.setVisible( true );
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement