Changing textbox color using java applet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danielmessick
    New Member
    • Mar 2010
    • 9

    Changing textbox color using java applet

    In the Java applet below, i'm trying to make the text box change color when the slider is moved to the left and right. Right now when I move the JSlider left or right the text in the text box changes color. How do I get the text box the change color ??????

    Code:
    import java.awt.Dimension;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JSlider;
    import javax.swing.SwingUtilities;
    import java.util.Random;
    import java.awt.Color;
    
    public class Main extends javax.swing.JApplet {
    	private JTextField aTextField;
    	private JSlider aSlider;
    
    public static void main(String[] args) {
    	SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    			JFrame frame = new JFrame();
    				Main inst = new Main();
    					frame.getContentPane().add(inst);
    
    frame.pack();
    frame.setVisible(true);
    }
    });
    
    }
    
    public Main() {
    	super();
    		initGUI();
    }
    
    private void initGUI() {
    	try {
    		setSize(new Dimension(200, 200));
    		getContentPane().setLayout(null);
    {
    	aTextField = new JTextField();
    		aTextField.setBackground(Color.white);
    			getContentPane().add(aTextField);
    				aTextField.setText("How do I make the whole text box change color ?");
    					aTextField.setBounds(65, 80, 300, 80);
    }
    {
    aSlider = new JSlider();
    aSlider.setBackground(Color.yellow);
    
    getContentPane().add(aSlider);
    aSlider.setBounds(65, 200, 300, 80);
    aSlider.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent evt) {
    aSliderMouseDragged(evt);
    
    }
    });
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    private void aSliderMouseDragged(MouseEvent evt) {
    	Random r = new Random();
    		int red = Math.abs(r.nextInt())%200;
    			int green = Math.abs(r.nextInt())%100;
    				int blue = Math.abs(r.nextInt())%100;
    					aTextField.setForeground(new Color(red, green, blue));
    }
    }
  • anurag275125
    New Member
    • Aug 2009
    • 79

    #2
    Color of text in text field is changing as you move the slider because you use setForeground method. To change the color of text box change it to setBackground.

    Why are you using Frame? You're working with applet, so you don't need to use Frame.

    I've done some modification in your code for simplicity, it's working--

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    /* <applet 
    code=slider.class
    width=400
    height=400
    > 
    </applet> */
    public class slider extends JApplet
    {
    	private JTextField aTextField;
    	private JSlider aSlider;
    	public void init()
    	{
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		aTextField = new JTextField();
    		aTextField.setFont(new Font("georgia",Font.BOLD,40));
    		aTextField.setBackground(Color.yellow);
    		aTextField.setText("How do I make the whole text box change color ?");
    		aTextField.setBounds(65, 80, 300, 80);
    		aSlider = new JSlider();
    		aSlider.setBackground(Color.yellow);
    		aSlider.setBounds(65, 200, 300, 80);
    		c.add(aTextField);
    		c.add(aSlider);
    		aSlider.addMouseMotionListener(new MouseMotionAdapter() 
    		{
    			public void mouseDragged(MouseEvent evt) 
    			{
    				Random r = new Random();
    				int red = Math.abs(r.nextInt())%200;
    				int green = Math.abs(r.nextInt())%100;
    				int blue = Math.abs(r.nextInt())%100;
    				aTextField.setBackground(new Color(red, green, blue));	
    			}
    		});
    	}
    }

    Comment

    • danielmessick
      New Member
      • Mar 2010
      • 9

      #3
      T h a n k s !!!

      Wow, pretty much I need to change the setForeground --> setBackground ! Thank you again, I like the modification of your code better than mine.

      Comment

      Working...