home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / TextPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  4.1 KB  |  138 lines

  1. /*
  2.  * @(#)TextPanel.java    1.8 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.border.*;
  17. import javax.swing.text.*;
  18. import javax.accessibility.*;
  19.  
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import java.util.*;
  23. import java.io.*;
  24.  
  25.  
  26. /**
  27.  * Text!
  28.  *
  29.  * @version 1.8 08/26/98
  30.  * @author Jeff Dinkins
  31.  * @author Peter Korn (accessibility support)
  32.  */
  33. public class TextPanel extends JPanel 
  34. {
  35.     // The Frame
  36.     SwingSet swing;
  37.  
  38.     public TextPanel(SwingSet swing) {
  39.     super(true);
  40.     this.swing = swing;
  41.     setBorder(new CompoundBorder(swing.loweredBorder, swing.emptyBorder10));
  42.  
  43.     JPanel textFields = SwingSet.createVerticalPanel(false);
  44.  
  45.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  46.  
  47.     JTextField field1 = new FixedTextField("George Washington", 15);
  48.     field1.getAccessibleContext().setAccessibleName("First text field");
  49.  
  50.     JTextField field2 = new FixedTextField("Thomas Jefferson", 15);
  51.     field2.setForeground(Color.red);
  52.     field2.getAccessibleContext().setAccessibleName("Second text field");
  53.  
  54.     JTextField field3 = new FixedTextField("Benjamin Franklin", 15);
  55.     field3.setBackground(new Color(200, 200, 255)); // cornflower blue
  56.     field3.getAccessibleContext().setAccessibleName("Third text field");
  57.  
  58.     // JTextField field4 = new FixedTextField("Thomas Payne", 15); 
  59.         // Thanks to Chris Paine for pointing out that I misspelled "Paine"). (-:
  60.     JTextField field4 = new FixedTextField("Thomas Paine", 15); 
  61.     field4.setForeground(Color.yellow);
  62.     field4.setBackground(new Color(200, 140, 80)); // pumpkin
  63.     field4.getAccessibleContext().setAccessibleName("Fourth text field");
  64.  
  65.     JTextField field5 = new FixedTextField("Abraham Lincoln", 15);
  66.     field5.setForeground(Color.green.brighter());
  67.     field5.setBackground(Color.black);
  68.     field5.getAccessibleContext().setAccessibleName("Fifth text field");
  69.  
  70.     JLabel label = (JLabel) textFields.add(new JLabel("Text Fields:"));
  71.     label.setFont(swing.boldFont);
  72.     label.setLabelFor(field1);
  73.     textFields.add(Box.createRigidArea(swing.vpad10));
  74.     textFields.add(field1);
  75.     textFields.add(Box.createRigidArea(swing.vpad5));
  76.     textFields.add(field2);
  77.     textFields.add(Box.createRigidArea(swing.vpad5));
  78.     textFields.add(field3);
  79.     textFields.add(Box.createRigidArea(swing.vpad5));
  80.     textFields.add(field4);
  81.     textFields.add(Box.createRigidArea(swing.vpad5));
  82.     textFields.add(field5);
  83.     textFields.add(Box.createHorizontalStrut(5));
  84.  
  85.     String text = LoadFile("Constitution.txt");
  86.  
  87.     JPanel textAreaPanel = SwingSet.createVerticalPanel(false);
  88.     label = (JLabel) textAreaPanel.add(new JLabel("Text Area:"));
  89.     label.setFont(swing.boldFont);
  90.     textAreaPanel.add(Box.createRigidArea(swing.vpad10));
  91.  
  92.     JPanel textWrapper = new JPanel(new BorderLayout());
  93.     textWrapper.setAlignmentX(LEFT_ALIGNMENT);
  94.      textWrapper.setBorder(swing.loweredBorder);
  95.  
  96.     textAreaPanel.add(textWrapper);
  97.  
  98.     JTextArea textArea = new JTextArea(text);
  99.     JScrollPane scroller = new JScrollPane() {
  100.             public Dimension getPreferredSize() {
  101.         return new Dimension(300,100);
  102.         }
  103.         public float getAlignmentX() {
  104.         return LEFT_ALIGNMENT;
  105.         }
  106.     };
  107.     scroller.getViewport().add(textArea);
  108.     textArea.setFont(new Font("Dialog", Font.PLAIN, 12));
  109.     textArea.getAccessibleContext().setAccessibleName("Editable text area");
  110.     label.setLabelFor(textArea);
  111.     textWrapper.add(scroller, BorderLayout.CENTER);
  112.  
  113.     add(Box.createRigidArea(swing.hpad10));
  114.     add(textFields);
  115.     add(Box.createRigidArea(swing.hpad10));
  116.     add(textAreaPanel);
  117.     }
  118.  
  119.  
  120.     class FixedTextField extends JTextField {
  121.     public FixedTextField(String text, int columns) {
  122.         super(text, columns);
  123.     }
  124.     public Dimension getMaximumSize() {
  125.         return getPreferredSize();
  126.     }
  127.     public float getAlignmentX() {
  128.         return LEFT_ALIGNMENT;
  129.     }
  130.     }
  131.  
  132.     public String LoadFile(String filename) {
  133.       return SwingSet.contentsOfFile(filename);
  134.     }
  135.     
  136.     
  137. }
  138.