473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 ??????

Expand|Select|Wrap|Line Numbers
  1. import java.awt.Dimension;
  2. import java.awt.event.MouseEvent;
  3. import java.awt.event.MouseMotionAdapter;
  4.  
  5. import javax.swing.JApplet;
  6. import javax.swing.JTextField;
  7. import javax.swing.JComponent;
  8. import javax.swing.JFrame;
  9. import javax.swing.JSlider;
  10. import javax.swing.SwingUtilities;
  11. import java.util.Random;
  12. import java.awt.Color;
  13.  
  14. public class Main extends javax.swing.JApplet {
  15.     private JTextField aTextField;
  16.     private JSlider aSlider;
  17.  
  18. public static void main(String[] args) {
  19.     SwingUtilities.invokeLater(new Runnable() {
  20.         public void run() {
  21.             JFrame frame = new JFrame();
  22.                 Main inst = new Main();
  23.                     frame.getContentPane().add(inst);
  24.  
  25. frame.pack();
  26. frame.setVisible(true);
  27. }
  28. });
  29.  
  30. }
  31.  
  32. public Main() {
  33.     super();
  34.         initGUI();
  35. }
  36.  
  37. private void initGUI() {
  38.     try {
  39.         setSize(new Dimension(200, 200));
  40.         getContentPane().setLayout(null);
  41. {
  42.     aTextField = new JTextField();
  43.         aTextField.setBackground(Color.white);
  44.             getContentPane().add(aTextField);
  45.                 aTextField.setText("How do I make the whole text box change color ?");
  46.                     aTextField.setBounds(65, 80, 300, 80);
  47. }
  48. {
  49. aSlider = new JSlider();
  50. aSlider.setBackground(Color.yellow);
  51.  
  52. getContentPane().add(aSlider);
  53. aSlider.setBounds(65, 200, 300, 80);
  54. aSlider.addMouseMotionListener(new MouseMotionAdapter() {
  55. public void mouseDragged(MouseEvent evt) {
  56. aSliderMouseDragged(evt);
  57.  
  58. }
  59. });
  60. }
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65.  
  66. private void aSliderMouseDragged(MouseEvent evt) {
  67.     Random r = new Random();
  68.         int red = Math.abs(r.nextInt())%200;
  69.             int green = Math.abs(r.nextInt())%100;
  70.                 int blue = Math.abs(r.nextInt())%100;
  71.                     aTextField.setForeground(new Color(red, green, blue));
  72. }
  73. }
Mar 7 '10 #1

✓ answered by anurag275125

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--

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5. /* <applet 
  6. code=slider.class
  7. width=400
  8. height=400
  9. </applet> */
  10. public class slider extends JApplet
  11. {
  12.     private JTextField aTextField;
  13.     private JSlider aSlider;
  14.     public void init()
  15.     {
  16.         Container c=getContentPane();
  17.         c.setLayout(new FlowLayout());
  18.         aTextField = new JTextField();
  19.         aTextField.setFont(new Font("georgia",Font.BOLD,40));
  20.         aTextField.setBackground(Color.yellow);
  21.         aTextField.setText("How do I make the whole text box change color ?");
  22.         aTextField.setBounds(65, 80, 300, 80);
  23.         aSlider = new JSlider();
  24.         aSlider.setBackground(Color.yellow);
  25.         aSlider.setBounds(65, 200, 300, 80);
  26.         c.add(aTextField);
  27.         c.add(aSlider);
  28.         aSlider.addMouseMotionListener(new MouseMotionAdapter() 
  29.         {
  30.             public void mouseDragged(MouseEvent evt) 
  31.             {
  32.                 Random r = new Random();
  33.                 int red = Math.abs(r.nextInt())%200;
  34.                 int green = Math.abs(r.nextInt())%100;
  35.                 int blue = Math.abs(r.nextInt())%100;
  36.                 aTextField.setBackground(new Color(red, green, blue));    
  37.             }
  38.         });
  39.     }
  40. }

2 9153
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--

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5. /* <applet 
  6. code=slider.class
  7. width=400
  8. height=400
  9. </applet> */
  10. public class slider extends JApplet
  11. {
  12.     private JTextField aTextField;
  13.     private JSlider aSlider;
  14.     public void init()
  15.     {
  16.         Container c=getContentPane();
  17.         c.setLayout(new FlowLayout());
  18.         aTextField = new JTextField();
  19.         aTextField.setFont(new Font("georgia",Font.BOLD,40));
  20.         aTextField.setBackground(Color.yellow);
  21.         aTextField.setText("How do I make the whole text box change color ?");
  22.         aTextField.setBounds(65, 80, 300, 80);
  23.         aSlider = new JSlider();
  24.         aSlider.setBackground(Color.yellow);
  25.         aSlider.setBounds(65, 200, 300, 80);
  26.         c.add(aTextField);
  27.         c.add(aSlider);
  28.         aSlider.addMouseMotionListener(new MouseMotionAdapter() 
  29.         {
  30.             public void mouseDragged(MouseEvent evt) 
  31.             {
  32.                 Random r = new Random();
  33.                 int red = Math.abs(r.nextInt())%200;
  34.                 int green = Math.abs(r.nextInt())%100;
  35.                 int blue = Math.abs(r.nextInt())%100;
  36.                 aTextField.setBackground(new Color(red, green, blue));    
  37.             }
  38.         });
  39.     }
  40. }
Mar 7 '10 #2
Wow, pretty much I need to change the setForeground --> setBackground ! Thank you again, I like the modification of your code better than mine.
Mar 7 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
1
by: Oleg Konovalov | last post by:
Hi, I am running applets in Java Plug-in 1.4.2 under IE6 and often receive OutOfMemory exceptions (dealing with large file - video clips, etc.) although according to option M, almost 50% (out...
6
by: Jeff Sandler | last post by:
I have a database I created in mySQL. I've been entering data every day into the database using a Java application that I wrote. The database and the Java program are on the same Win 98 SE...
7
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses...
3
by: Larry Martin | last post by:
I am trying to run a Java Applet on my ascx page and am getting an IO exception when IE6 tries to load the applet. It seems a lot of others are getting the same problem but a search of the web did...
16
by: chris | last post by:
im new to javascript but slowly getting better what i want to do is have some text on the screen and when an event happens for example click a button the text would change to what i want. how...
4
by: aotemp | last post by:
Does anyone know how to detect the version of Java installed? My website has an application that requires java 1.5+ to be installed, basically if the user doesn;t already have it installed I...
5
by: sck10 | last post by:
Hello, I am using a java applet. When I run the page, the graph shows. The problem that I am having is trying to figure out a way to programmically provide the xml file name: <param...
2
by: Andrew Neiderer | last post by:
This is simple HTML, Java but I am really confused. I include the code since it is so small - ---------------------------------------------------------------------------- -- test.html -- ...
2
by: pssraju | last post by:
Hi, I am having applet display problem on my PC and the same thing working fine on some of my colleagues PC's. When I cross checked content through view source its exactly same on both the PC's....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.