Results 1 to 5 of 5

Thread: JAVA Help - Background change by color matching

  1. #1
    Join Date
    Feb 2009
    Posts
    96

    JAVA Help - Background change by color matching

    I have a JTextField and a jbutton and when i enter data on the field and hit the button i want the back ground to change color if the color matches the ones stored. How do i verify that the color matches the stored color?
    public void actionPerformed(ActionEvent e) //make the actions
    {
    String red, white, blue;

    Object source = e.getSource(); //get the source
    String aColor = choice.getText();
    if(aColor == "red")
    panel02.setBackground(Color.RED);

    //panel02.setBackground(Color.RED);



    //panel03.setBackground(Color.BLUE);



    //panel04.set
    //catch(Exception ex)
    //
    // error.setText("Wrong Color Choice");
    // }
    }


    the // are parts that dont work and neither does the color check but when the color check is removed the panes change color to the stored ones.
    Last edited by Yogesh; 11-05-2009 at 09:03 AM. Reason: Title edited suitably. Always give proper title that best describes your thread.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: JAVA Help

    Here's an example of the Java Code to change the color of the background -

    import java.awt.*;
    import java.awt.event.*;
    Code:
    import javax.swing.*;
    
    
    public class BackgroundFrame extends JFrame 
    	implements ActionListener {
    
       private WindowTerminator  wt = new WindowTerminator();  // Code below
       private JButton red,blue; // the JButtons which will cause the background colors to change
       private Container cp; // our contentpane
    
       public BackgroundFrame( ) {
          setSize(300,300);
          setLocation(100,100);
          setTitle(getClass().getName());
    
    
         addWindowListener(wt);
    
         red = new JButton("Red");
         blue = new JButton("Blue");
         red.addActionListener(this);
         blue.addActionListener(this);
       
        cp = getContentPane();
        cp.setBackground(Color.pink);
        cp.setLayout(new FlowLayout());
        cp.add(red);
        cp.add(blue);
    
        show();
       }
    
    
       public void actionPerformed(ActionEvent e) {
    
          JButton s = (JButton) e.getSource();   //get the source of the event
    
         if ( s == red)  cp.setBackground(Color.red);
         else if ( s == blue) cp.setBackground ( Color.blue);
    
         cp.repaint();
       }
    
    
       public static void main (String[] args) {
    
          JFrame f = new BackgroundFrame();
       }
    
    }
    
    class WindowTerminator extends WindowAdapter {
    
       public void windowClosing (WindowEvent e)  {
          System.exit(0);
       }
    }

  3. #3
    Join Date
    May 2008
    Posts
    685

    Re: JAVA Help

    I don't know much about what you are trying to achieve because I am unable to identify what you are trying to achieve. But what I assume with your query is you need to use Java getColor function.

    Syntax: public static Color getColor(String nm)
    where nm - the name of the color property

    For more information, see this: http://java.sun.com/j2se/1.3/docs/ap...va.lang.String)

  4. #4
    Join Date
    Feb 2009
    Posts
    96

    Re: JAVA Help - Background change by color matching

    what i need is the following:
    Create an applet that has a JTextField for the user to enter the name of a color. If the color entered is not either red, white, blue, then your applet should throw an Exception and tell the user the color entered was not valid. Otherwise change the background color to the appropriate color.

  5. #5
    Join Date
    May 2008
    Posts
    685

    Re: JAVA Help - Background change by color matching

    Quote Originally Posted by Daren View Post
    what i need is the following:
    Create an applet that has a JTextField for the user to enter the name of a color. If the color entered is not either red, white, blue, then your applet should throw an Exception and tell the user the color entered was not valid. Otherwise change the background color to the appropriate color.
    So in that case, my assumption would be best as per your need. You just use

    Syntax: public static Color getColor(String nm)
    where nm - the name of the color property

    You except the user entered color in "nm" and check if that color matches your requirement. If yes then do what you need, else through an exception.

    Its very simple, isn't it?

Similar Threads

  1. Unable to change background color of PDF file
    By GurdeepS in forum Windows Software
    Replies: 4
    Last Post: 12-03-2010, 07:22 PM
  2. Change The Background Color Of The Jsp
    By rashmi_ay in forum Software Development
    Replies: 4
    Last Post: 27-02-2010, 08:22 PM
  3. Set ToolTip Text background color in java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 24-02-2010, 11:05 PM
  4. How Can I Change the background Color Of A JOptionPane?
    By Adrina_g in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 09:41 AM
  5. Change the background color of QTextEdit
    By Gomeler in forum Software Development
    Replies: 3
    Last Post: 27-11-2009, 01:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,982,972.47834 seconds with 16 queries