Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 11-05-2009
Member
 
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.
Reply With Quote
  #2  
Old 11-05-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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);
   }
}
Reply With Quote
  #3  
Old 11-05-2009
Member
 
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)
Reply With Quote
  #4  
Old 11-05-2009
Member
 
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.
Reply With Quote
  #5  
Old 12-05-2009
Member
 
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?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "JAVA Help - Background change by color matching"
Thread Thread Starter Forum Replies Last Post
Unable to change background color of PDF file GurdeepS Windows Software 4 12-03-2010 07:22 PM
Change The Background Color Of The Jsp rashmi_ay Software Development 4 27-02-2010 08:22 PM
Set ToolTip Text background color in java Adrina_g Software Development 4 24-02-2010 11:05 PM
How Can I Change the background Color Of A JOptionPane? Adrina_g Software Development 5 27-01-2010 09:41 AM
Change the background color of QTextEdit Gomeler Software Development 3 27-11-2009 01:13 PM


All times are GMT +5.5. The time now is 07:01 PM.