An example of several text components including password fields and formatted fields. : TextPane « Swing JFC « Java






An example of several text components including password fields and formatted fields.

An example of several text components including password fields and formatted fields.
   
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// TextComponentSampler.java
//An example of several text components including password fields and
//formatted fields.
//

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class TextComponentSampler extends JFrame {

  public static String word = "portmeiron";

  public static String markup = "Questions are <font size=\"+1\" color=\"blue\">a burden</font> to others,\n"
      + "answers <font size=\"+2\" color=\"red\">a prison</font> for oneself.";

  public TextComponentSampler() {
    super("TextComponentSampler");

    JTextField tf = new JTextField(word, 12);
    JPasswordField pf = new JPasswordField(word, 12);

    MaskFormatter formatter = null;
    try {
      formatter = new MaskFormatter("UUUUU");
    } catch (java.text.ParseException ex) {
    }
    JFormattedTextField ftf = new JFormattedTextField(formatter);
    ftf.setColumns(12);
    ftf.setValue(word);

    JTextArea ta1 = new JTextArea(markup);
    JScrollPane scroll1 = new JScrollPane(ta1);

    JTextArea ta2 = new JTextArea(markup);
    ta2.setLineWrap(true);
    ta2.setWrapStyleWord(true);
    JScrollPane scroll2 = new JScrollPane(ta2);

    JTextPane tp = new JTextPane();
    tp.setText(markup);
    // Create an AttributeSet with which to change color and font.
    SimpleAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setForeground(attrs, Color.blue);
    StyleConstants.setFontFamily(attrs, "Serif");
    // Apply the AttributeSet to a few blocks of text.
    StyledDocument sdoc = tp.getStyledDocument();
    sdoc.setCharacterAttributes(14, 29, attrs, false);
    sdoc.setCharacterAttributes(51, 7, attrs, false);
    sdoc.setCharacterAttributes(78, 28, attrs, false);
    sdoc.setCharacterAttributes(114, 7, attrs, false);
    JScrollPane scroll3 = new JScrollPane(tp);

    JEditorPane ep1 = new JEditorPane("text/plain", markup);
    JScrollPane scroll4 = new JScrollPane(ep1);

    JEditorPane ep2 = new JEditorPane("text/html", markup);
    JScrollPane scroll5 = new JScrollPane(ep2);

    // Done creating text components; now lay them out and make them pretty.
    JPanel panel_tf = new JPanel();
    JPanel panel_pf = new JPanel();
    JPanel panel_ftf = new JPanel();
    panel_tf.add(tf);
    panel_pf.add(pf);
    panel_ftf.add(ftf);

    panel_tf.setBorder(new TitledBorder("JTextField"));
    panel_pf.setBorder(new TitledBorder("JPasswordField"));
    panel_ftf.setBorder(new TitledBorder("JFormattedTextField"));
    scroll1.setBorder(new TitledBorder("JTextArea (line wrap off)"));
    scroll2.setBorder(new TitledBorder("JTextArea (line wrap on)"));
    scroll3.setBorder(new TitledBorder("JTextPane"));
    scroll4.setBorder(new TitledBorder("JEditorPane (text/plain)"));
    scroll5.setBorder(new TitledBorder("JEditorPane (text/html)"));

    JPanel pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(panel_tf);
    pan.add(panel_pf);
    pan.add(panel_ftf);

    Container contentPane = getContentPane();
    contentPane.setLayout(new GridLayout(2, 3, 8, 8));

    contentPane.add(pan);
    contentPane.add(scroll1);
    contentPane.add(scroll2);
    contentPane.add(scroll3);
    contentPane.add(scroll4);
    contentPane.add(scroll5);
  }

  public static void main(String args[]) {
    JFrame frame = new TextComponentSampler();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 450);
    frame.setVisible(true);
  }
}

           
         
    
    
  








Related examples in the same category

1.TextPane SampleTextPane Sample
2.Brower based on JEditorPaneBrower based on JEditorPane
3.JTextPane demo with various format and html loading and renderingJTextPane demo with various format and html loading and rendering
4.Styled TextStyled Text
5.Appending TextPaneAppending TextPane
6.Text Component DisplayText Component Display
7.JTextPane Styles Example 1JTextPane Styles Example 1
8.JTextPane Styles Example 2JTextPane Styles Example 2
9.JTextPane Styles Example 3JTextPane Styles Example 3
10.JTextPane Styles Example 4JTextPane Styles Example 4
11.JTextPane Styles Example 5JTextPane Styles Example 5
12.JTextPane Styles Example 6JTextPane Styles Example 6
13.JTextPane Styles Example 7JTextPane Styles Example 7
14.JTextPane Styles Example 8JTextPane Styles Example 8
15.JTextPane Highlight ExampleJTextPane Highlight Example
16.JTextPane Extended Paragraph Example
17.TextPane ElementsTextPane Elements
18.TextPane Views 2TextPane Views 2
19.List HTML ValuesList HTML Values
20.Show HTML Document
21.Show HTML Views
22.JEditorPane Replace ReaderJEditorPane Replace Reader
23.Bi-Directional TextBi-Directional Text
24.TextPane: DocumentEvent TextPane: DocumentEvent
25.Show how Icons, Components, and text can be added to a JTextPaneShow how Icons, Components, and text can be added to a JTextPane
26.Parentheses matcherParentheses matcher
27.A TabSet in a JTextPaneA TabSet in a JTextPane
28.Extension of JTextPane that allows the user to easily append colored text to the documentExtension of JTextPane that allows the user to easily append colored text to the document
29.An implementation of HighlightPainter that underlines text with a thick lineAn implementation of HighlightPainter that underlines text with a thick line
30.An example of highlighting multiple, discontiguous regions of a text component.An example of highlighting multiple, discontiguous regions of a text component.
31.A custom caret classA custom caret class
32.Enumerating the Paragraphs of a JTextPane Component
33.Inserting an Image into a JTextPane Component
34.Inserting a Component into a JTextPane Component
35.Customizing Tab Stops in a JTextPane Component
36.Sharing Styles Between JTextPanes
37.Listing the Styles Associated with a JTextPane
38.Listing the Attributes in a Style
39.Replace style
40.Set logical style; replaces paragraph style's parent
41.Get logical style and restore it after new paragraph style
42.Determining If a Style Attribute Applies to a Character or the Paragraph
43.Determine if the attribute is a color or a font-related attribute.
44.Create a tab set from the tab stops
45.Foreground color
46.Background color
47.Change the Font size of JTextPane
48.Font family
49.Bold style
50.A style can have multiple attributes; this one makes text bold and italic
51.Duplicate style
52.Italicize the entire paragraph containing the position 12
53.Inserting Styled Text in a JTextPane Component
54.A separation of a data from the visual representation. In a JTextPane component, we have a StyledDocument for setting the style of the text data.
55.Tests two attributed strings for equality.
56.Get Leading White Space
57.Get Leading White Space Width
58.Get Max Fitting FontSize
59.Reads a AttributedString object that has been serialised by the SerialUtilities.writeAttributedString(AttributedString, ObjectOutputStream)} method.