Results 1 to 7 of 7
  1. #1
    danielmessick is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default How do i fix this 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 ??????


    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));
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by danielmessick View Post
    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 ??????

    Java Code:
    aTextField.setForeground(new Color(red, green, blue));
    So instead of the above you want to do this:

    Java Code:
    aTextField.setBackground(new Color(red, green, blue));
    That didn't take a rocket scientist to think of it ... Did you write the code of that applet yourself?

    kind regards,

    Jos

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Darn, it's like graffiti isn't it?

    kind regards,

    Jos

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Triple posted
    Then there's bytes.com...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    Then there's bytes.com...
    Yup, the answer is identical to my answer. We'll see if the poster returns here after spamming (almost) the entire net with his trivial question.

    kind regards,

    Jos

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Help with this Java applet !!!!!
    By danielmessick in forum Java Applets
    Replies: 2
    Last Post: 03-07-2010, 12:58 PM
  2. Replies: 3
    Last Post: 07-23-2008, 08:07 AM
  3. Java Applet Help
    By Nuluvius in forum New To Java
    Replies: 0
    Last Post: 03-01-2008, 03:04 PM
  4. New to JAVA Applet
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 10-31-2007, 08:11 PM
  5. Java 3d in applet
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 05:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •