Monday, November 14, 2011

Set Background Color of JTextField

    package org.best.example;
    /*
            Set Background Color of JTextField Example
            This java example shows how to set background color of JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldBackgroundColorExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldBackgroundColorExample extends JApplet{
    
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Background Color Example",20);
                  
                    //create new custom color
                    Color color = Color.GRAY;
                  
                    /*
                     * To set background color of JTextField's text use,
                     * void setBackground(Color color)
                     * method of JTextField class.
                     */
                  
                    field.setBackground(color);
    
                    add(field);
            }
    }

No comments: