HighLight painter and JTextField : JTextField « Swing « Java Tutorial






/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * TextFieldDemo.java requires one additional file:
 * content.txt
 */

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.GroupLayout;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class TextFieldDemo extends JFrame implements DocumentListener {

  private JTextField entry;
  private JLabel jLabel1;
  private JScrollPane jScrollPane1;
  private JLabel status;
  private JTextArea textArea;

  final static Color HILIT_COLOR = Color.LIGHT_GRAY;
  final static Color ERROR_COLOR = Color.PINK;
  final static String CANCEL_ACTION = "cancel-search";

  final Color entryBg;
  final Highlighter hilit;
  final Highlighter.HighlightPainter painter;

  public TextFieldDemo() {
    initComponents();

    InputStream in = getClass().getResourceAsStream("content.txt");
    try {
      textArea.read(new InputStreamReader(in), null);
    } catch (IOException e) {
      e.printStackTrace();
    }

    hilit = new DefaultHighlighter();
    painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
    textArea.setHighlighter(hilit);

    entryBg = entry.getBackground();
    entry.getDocument().addDocumentListener(this);

    InputMap im = entry.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = entry.getActionMap();
    im.put(KeyStroke.getKeyStroke("ESCAPE"), CANCEL_ACTION);
    am.put(CANCEL_ACTION, new CancelAction());
  }

  /**
   * This method is called from within the constructor to initialize the form.
   */

  private void initComponents() {
    entry = new JTextField();
    textArea = new JTextArea();
    status = new JLabel();
    jLabel1 = new JLabel();

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setTitle("TextFieldDemo");

    textArea.setColumns(20);
    textArea.setLineWrap(true);
    textArea.setRows(5);
    textArea.setWrapStyleWord(true);
    textArea.setEditable(false);
    jScrollPane1 = new JScrollPane(textArea);

    jLabel1.setText("Enter text to search:");

    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);

    // Create a parallel group for the horizontal axis
    ParallelGroup hGroup = layout
        .createParallelGroup(GroupLayout.Alignment.LEADING);

    // Create a sequential and a parallel groups
    SequentialGroup h1 = layout.createSequentialGroup();
    ParallelGroup h2 = layout
        .createParallelGroup(GroupLayout.Alignment.TRAILING);

    // Add a container gap to the sequential group h1
    h1.addContainerGap();

    // Add a scroll pane and a label to the parallel group h2
    h2.addComponent(jScrollPane1, GroupLayout.Alignment.LEADING,
        GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE);
    h2.addComponent(status, GroupLayout.Alignment.LEADING,
        GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE);

    // Create a sequential group h3
    SequentialGroup h3 = layout.createSequentialGroup();
    h3.addComponent(jLabel1);
    h3.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
    h3.addComponent(entry, GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE);

    // Add the group h3 to the group h2
    h2.addGroup(h3);
    // Add the group h2 to the group h1
    h1.addGroup(h2);

    h1.addContainerGap();

    // Add the group h1 to the hGroup
    hGroup.addGroup(GroupLayout.Alignment.TRAILING, h1);
    // Create the horizontal group
    layout.setHorizontalGroup(hGroup);

    // Create a parallel group for the vertical axis
    ParallelGroup vGroup = layout
        .createParallelGroup(GroupLayout.Alignment.LEADING);
    // Create a sequential group v1
    SequentialGroup v1 = layout.createSequentialGroup();
    // Add a container gap to the sequential group v1
    v1.addContainerGap();
    // Create a parallel group v2
    ParallelGroup v2 = layout
        .createParallelGroup(GroupLayout.Alignment.BASELINE);
    v2.addComponent(jLabel1);
    v2.addComponent(entry, GroupLayout.PREFERRED_SIZE,
        GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
    // Add the group v2 tp the group v1
    v1.addGroup(v2);
    v1.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
    v1.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 233,
        Short.MAX_VALUE);
    v1.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
    v1.addComponent(status);
    v1.addContainerGap();

    // Add the group v1 to the group vGroup
    vGroup.addGroup(v1);
    // Create the vertical group
    layout.setVerticalGroup(vGroup);
    pack();
  }

  public void search() {
    hilit.removeAllHighlights();

    String s = entry.getText();
    if (s.length() <= 0) {
      message("Nothing to search");
      return;
    }

    String content = textArea.getText();
    int index = content.indexOf(s, 0);
    if (index >= 0) { // match found
      try {
        int end = index + s.length();
        hilit.addHighlight(index, end, painter);
        textArea.setCaretPosition(end);
        entry.setBackground(entryBg);
        message("'" + s + "' found. Press ESC to end search");
      } catch (BadLocationException e) {
        e.printStackTrace();
      }
    } else {
      entry.setBackground(ERROR_COLOR);
      message("'" + s + "' not found. Press ESC to start a new search");
    }
  }

  void message(String msg) {
    status.setText(msg);
  }

  // DocumentListener methods

  public void insertUpdate(DocumentEvent ev) {
    search();
  }

  public void removeUpdate(DocumentEvent ev) {
    search();
  }

  public void changedUpdate(DocumentEvent ev) {
  }

  class CancelAction extends AbstractAction {
    public void actionPerformed(ActionEvent ev) {
      hilit.removeAllHighlights();
      entry.setText("");
      entry.setBackground(entryBg);
    }
  }

  public static void main(String args[]) {
    // Schedule a job for the event dispatch thread:
    // creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        // Turn off metal's use of bold fonts
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        new TextFieldDemo().setVisible(true);
      }
    });
  }

}








14.15.JTextField
14.15.1.Make a Text Field two columns wide
14.15.2.Sharing Data Models between two JTextFieldSharing Data Models between two JTextField
14.15.3.Adding ActionListener to JTextFieldAdding ActionListener to JTextField
14.15.4.Verifying Input During Focus TraversalVerifying Input During Focus Traversal
14.15.5.JTextField with a JScrollBar for ScrollingJTextField with a JScrollBar for Scrolling
14.15.6.Horizontal AlignmentHorizontal Alignment
14.15.7.Using JLabel Mnemonics: Interconnect a specific JLabel and JTextFieldUsing JLabel Mnemonics: Interconnect a specific JLabel and JTextField
14.15.8.To make sure that the end of the contents is visibleTo make sure that the end of the contents is visible
14.15.9.Use the write() method to write the contentsUse the write() method to write the contents
14.15.10.Drag and drop Text Demo
14.15.11.Using Actions with Text Components: JTextField
14.15.12.HighLight painter and JTextField
14.15.13.Limit JTextField input to a maximum length
14.15.14.Apply special filter to a JTextField
14.15.15.Format JTextField's text to uppercase
14.15.16.Add key listener event handler to JTextField
14.15.17.Right justified JTextfield content
14.15.18.Set the focus on a particular JTextField
14.15.19.Customizing a JTextField Look and Feel
14.15.20.Cut, paste, and copy in a JTextField
14.15.21.Aligning the Text in a JTextField Component
14.15.22.Based on JTextField content, enable or disable a JButton
14.15.23.Subclass InputVerfier
14.15.24.extends JTextField to create integer JTextField