Results 1 to 11 of 11
  1. #1
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default Change bgcolor newbea question

    Hi everyone,
    Could you pls help me to find my problem here,
    I wanna change background color when the button is clicked. I know to do it with some other ways but want to learn my mistake here.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class ColorTest extends JFrame implements ActionListener {

    JButton ylw,ble,grn;

    Container c;

    public ColorTest(){


    setTitle("Coloring");



    JButton ylw=new JButton("Yellow");
    JButton ble=new JButton("Blue");
    JButton grn=new JButton("Green");




    c =getContentPane();
    c.setLayout(new FlowLayout());
    show();


    c.add(ylw);
    c.add(ble);
    c.add(grn);

    ylw.addActionListener(this);
    ble.addActionListener(this);
    grn.addActionListener(this);



    }
    public void actionPerformed(ActionEvent ae){

    JButton s = (JButton) ae.getSource();
    try {
    if ( s == ylw) c.setBackground(Color.red);

    else if ( s == ble) c.setBackground ( Color.blue);

    else if (s==grn) c.setBackground(Color.GREEN);

    // p.repaint();
    } catch (Exception e) {
    System.out.println(e);
    // TODO: handle exception
    }
    }

    public static void main(String args[]){
    ColorTest clr=new ColorTest();
    clr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    clr.setSize(300,300);
    clr.setVisible(true);
    }
    }

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Next time post in code tags

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class ColorTest extends JFrame implements ActionListener {
    
    	JButton ylw,ble,grn;
    
    	Container c;
    
    	public ColorTest(String color){
    
    
    		super(color);
    
    
    
    		JButton ylw=new JButton("Yellow");
    		JButton ble=new JButton("Blue");
    		JButton grn=new JButton("Green");
    
    		c =getContentPane();
    		c.setLayout(new FlowLayout());
    		show();
    
    
    		c.add(ylw);
    		c.add(ble);
    		c.add(grn);
    
    		ylw.addActionListener(this);
    		ble.addActionListener(this);
    		grn.addActionListener(this);
    
    
    
    	}
    	public void actionPerformed(ActionEvent e){
    
    
    		try {
    			if (e.getActionCommand()=="Yellow") {
    				c.setBackground(Color.yellow);
    			}
    			
    			else if ( e.getActionCommand()=="Blue"){ 	
    				c.setBackground ( Color.blue);
    			}
    
    			else if (e.getActionCommand()=="Green"){ 
    				c.setBackground(Color.GREEN);
    			}
    
    	// p.repaint();
    		} catch (Exception ex) {
    			ex.printStackTrace();
    			// TODO: handle exception
    		}
    	}
    
    	public static void main(String args[]){
    		ColorTest clr=new ColorTest("Coloring");
    		clr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    		clr.setSize(300,300);
    		clr.setVisible(true);
    	}
    }

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    What's your mistake? It all makes sense logically to me except that p.repaint() is commented out. I'm pretty sure after you set the background color you need to refresh the view/applet/image whatever it is

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

    Default

    > Could you pls help me to find my problem here,
    Shouldn't you be telling us what problem you have?

    @xcallmejudasx: setBackground in JComponent invokes repaint(), so for Swing components there is no need to call repaint() explicitly.

    db

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by Darryl.Burke View Post

    @xcallmejudasx: setBackground in JComponent invokes repaint(), so for Swing components there is no need to call repaint() explicitly.

    db
    Good to know. I havn't worked with paint in a few years and just assumed since I had to use refresh with my TreeViewer view. Yet another reason my SWT program is failing and I should get around to writing it in Swing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem I see is that you are trying to compare Strings by using
    Java Code:
    if (stringA == stringB) {
    This will compare if the two String objects are identical not if the Strings they contain are the same. Much better is to compare them using the equals method:
    Java Code:
    if (stringA.equals(stringB)) {

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Sure Fubarable,or it is also the option matches,who are really sure that they declared JButtons names and want to compare them.but equals is the best.Thanks Fubarable.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I'd use an enum...
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SelectColor
    {
      enum MyColor
      {
        //GRAY("Gray", Color.gray), //to add a new button, simply add an enum constant to the list
        YELLOW("Yellow", Color.yellow), 
        AZURE("Azure", new Color(0, 127, 255)), 
        CORAL("Coral", new Color(255, 127, 80)), 
        LAVENDER("Lavender", new Color(181, 126, 220));
    
        private String name;
        private Color color;
    
        private MyColor(String name, Color color)
        {
          this.name = name;
          this.color = color;
        }
    
        public Color getColor()
        {
          return color;
        }
    
        public String toString()
        {
          return name;
        }
      }
    
      private JPanel mainPanel = new JPanel();
    
      public SelectColor()
      {
        int glGap = 10;
        mainPanel.setLayout(new GridLayout(1, 0, glGap, 0));
        int ebGap = 20;
        int ebBottomGap = 300;
        mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebBottomGap, ebGap));
    
        ButtonListener myBtnListener = new ButtonListener();
        ButtonGroup btnGroup = new ButtonGroup();
        for (MyColor myColor : MyColor.values())
        {
          AbstractButton colorBtn = new JToggleButton(myColor.toString());
          colorBtn.setBackground(myColor.getColor());
          colorBtn.addActionListener(myBtnListener);
          btnGroup.add(colorBtn);
          mainPanel.add(colorBtn);
        }
    
      }
    
      public JComponent getMainPanel()
      {
        return mainPanel;
      }
    
      private class ButtonListener implements ActionListener
      {
    
        public void actionPerformed(ActionEvent e)
        {
          String command = e.getActionCommand();
          mainPanel.setBackground(MyColor.valueOf(command.toUpperCase()).getColor());
        }
      }
    
      public static void main(String args[])
      {
        JFrame frame = new JFrame("Coloring");
        frame.getContentPane().add(new SelectColor().getMainPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }

  9. #9
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default

    Thank you very much for all answers, I found out the problem, the clue was String object, I just omitted
    Java Code:
    JButton ylw= new JButton(); //here JButton
      ylw=new JButton();

  10. #10
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    I agree with Fubarable, the color Enum always works for me.

    Ex.

    component.setBackground(Color.BLACK);

    that will change the color.
    there is also a .setForeground() method that has the same Enumerated data types.

  11. #11
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. How to change JDK?
    By mew in forum Eclipse
    Replies: 5
    Last Post: 05-21-2010, 05:21 AM
  3. ip change
    By iiggoorr in forum Networking
    Replies: 9
    Last Post: 11-05-2008, 07:11 PM
  4. Is it possible to change the '\n' into ' ' ...
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 02:32 PM
  5. How to change the resolution ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 11:15 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
  •