A frame with three text fields to set the background color. : Document Event « Swing JFC « Java






A frame with three text fields to set the background color.

  
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * @version 1.40 2007-08-05
 * @author Cay Horstmann
 */
public class ChangeTrackingTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               ColorFrame frame = new ColorFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

/**
 * A frame with three text fields to set the background color.
 */
class ColorFrame extends JFrame
{
   public ColorFrame()
   {
      setTitle("ChangeTrackingTest");

      DocumentListener listener = new DocumentListener()
         {
            public void insertUpdate(DocumentEvent event)
            {
               setColor();
            }

            public void removeUpdate(DocumentEvent event)
            {
               setColor();
            }

            public void changedUpdate(DocumentEvent event)
            {
            }
         };

      panel = new JPanel();
      
      panel.add(new JLabel("Red:"));
      redField = new JTextField("255", 3);
      panel.add(redField);
      redField.getDocument().addDocumentListener(listener);

      panel.add(new JLabel("Green:"));
      greenField = new JTextField("255", 3);
      panel.add(greenField);
      greenField.getDocument().addDocumentListener(listener);

      panel.add(new JLabel("Blue:"));
      blueField = new JTextField("255", 3);
      panel.add(blueField);
      blueField.getDocument().addDocumentListener(listener);
      
      add(panel);
      pack();
   }

   /**
    * Set the background color to the values stored in the text fields.
    */
   public void setColor()
   {
      try
      {
         int red = Integer.parseInt(redField.getText().trim());
         int green = Integer.parseInt(greenField.getText().trim());
         int blue = Integer.parseInt(blueField.getText().trim());
         panel.setBackground(new Color(red, green, blue));
      }
      catch (NumberFormatException e)
      {
         // don't set the color if the input can't be parsed
      }
   }

   private JPanel panel;
   private JTextField redField;
   private JTextField greenField;
   private JTextField blueField;
}

   
    
  








Related examples in the same category

1.DocumentFilter that maps lowercase letters to uppercaseDocumentFilter that maps lowercase letters to uppercase
2.An extension of PlainDocument that restricts the length of its contentAn extension of PlainDocument that restricts the length of its content
3.Document Event DemoDocument Event Demo
4.React to the document update insert changed eventReact to the document update insert changed event
5.HTMLDocument: Document Iterator Example
6.DocumentListener DemoDocumentListener Demo
7.extends PlainDocument to create a float value type text field
8.extends PlainDocument to create alpha and numeric value based field field
9.Implement a document cleaner that maps lowercase letters to uppercaseImplement a document cleaner that maps lowercase letters to uppercase
10.Document ElementIterator DemoDocument ElementIterator Demo
11.Format JTextField's text to uppercase
12.Overriding a Few Default Typed Key Bindings in a JTextComponent
13.Overriding - key
14.Override $ key
15.Overriding space key
16.Disable a character so that no action is invoked.
17.Bind a keystroke to shift-space
18.Prevent the space from being inserted when shift-space is pressed.
19.Subclass InputVerifier
20.extends PlainDocument
21.This Document restricts the size of the contained plain text to the given number of characters.