Results 1 to 3 of 3
  1. #1
    aadi_j is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default change Panel Color

    I am having problem in changing color of panel according to user input in JTextField..I dont know what is wrong..please help me..
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package project_5;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPanel;
    import java.awt.Color;
    import javax.swing.JLabel;

    /**
    *
    * @author Jai Shree Krishna
    */

    public class ColorChange extends JFrame implements ActionListener{


    private JPanel p;
    private int n = 1;
    private IntField intField = new IntField(12, 100);
    private int userInt;
    private JLabel l;

    public ColorChange()
    {
    p = new JPanel();
    p.setSize(300, 100);
    p.setBackground(Color.green);
    intField.addActionListener(this);
    l = new JLabel("HELLO");
    p.add(intField);
    p.add(l);
    getContentPane().add(p);

    }

    public void actionPerformed(ActionEvent event)
    {
    n = intField.getInt();
    repaint();

    }

    public void paintComponent(Graphics g) {

    l.setText(String.valueOf(n));
    switch(n)
    {
    case '1': p.setBackground(Color.yellow);
    break;
    case '2': p.setBackground(Color.blue);
    break;
    case '3': p.setBackground(Color.red);
    break;
    default: p.setBackground(Color.green);
    }

    }



    public static void main(String[] args) {
    ColorChange c = new ColorChange();
    c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    c.setSize(300, 100);
    c.setVisible(true);

    }

    }
    ************************************************** ******
    IntField.java
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package project_5;
    import javax.swing.*;
    /**
    *
    * @author Jai Shree Krishna
    */
    public class IntField extends JTextField {



    public IntField (int size) {
    super(size);
    }


    public IntField(int size, int max) {
    super(size);

    }


    public int getInt() throws NumberFormatException{
    int num = Integer.parseInt(getText());

    return num;
    }

    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    paintComponent never gets called, and even if it were, it would not work as you test against chars in the switch statement. Get rid of the paintComponent method, which should be paintComponents anyway. Set the background in actionPerformed.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Please use code tags if you have to post code.
    Describe your problem fully.
    does it compile? Does it throw an exception?
    What is happening vs what is supposed to happen?

Similar Threads

  1. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 12:20 PM
  2. Color Change of data
    By Java.child in forum AWT / Swing
    Replies: 20
    Last Post: 02-12-2009, 06:51 AM
  3. How to Change the color of MultiColumnListBox
    By Java.child in forum AWT / Swing
    Replies: 1
    Last Post: 01-22-2009, 12:07 AM
  4. How to change string Color
    By Java.child in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2009, 04:27 AM
  5. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 01:39 AM

Posting Permissions

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