Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: jbutton states and events

    If you got the reference to the button object using e.getSource(), you could test what state it was in and change it as you wanted.
    There are two state values: its color and its text.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    ryuti is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: jbutton states and events

    i tried this also

    Java Code:
    class RM1 implements ActionListener(ActionEvent e) 
    { 
    if(getBackground() == GREEN)
     {
    setBackground(Color.GREEN);
    } 
    else
    {
     setBackground(Color.RED); 
    setText("LIGHT OFF");  
     } 
    }

  3. #23
    ryuti is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: jbutton states and events

    or this

    Java Code:
    class RM1 implements ActionListener{
    
    	private Color GREEN;
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == rm1L)
    				{
    			if(getBackground() == GREEN) 
    			{
    				setBackground(Color.GREEN); 
    				setText("LIGHT ON");
    			}
    				
    		else
    		{
    			setBackground(Color.RED); 
    			setText("LIGHT OFF");
    		}
    		
    	}
    	
    }
    Last edited by ryuti; 03-26-2012 at 10:16 PM.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: jbutton states and events

    What object's background value are you looking at when you call the getBackground() method?

    What is the value of the variable/symbol: GREEN?

    Does the code compile? Sort of a waste of time posting code that does not compile without posting the compiler's error messages.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    ryuti is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: jbutton states and events

    the code does compile but there is no action

    Color GREEN;
    GREEN = rm1L.getBackground;

    I tried different methods to get it working but no luck. I really cannot afford to loose more time on this. I will add a second button(easier way).
    I will have one button for on and one for off.

    The assigment is due on Friday and I concentrated on this part too much. I need to do testing, networkingm threadsm which are far more important. The thing is seems to understand those better.

    Anyway, many thanks for your assistance!

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: jbutton states and events

    rm1L.getBackground;
    The syntax for method reference ends with ()s
    Why is the name of the variable: GREEN? Can't the background be other colors?

    Can you write the code to compare the button's current color to the Color.green?
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    ryuti is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: jbutton states and events

    I think that is the problem. i do not know how to compare the current color to Color.GREEN.
    Can be any color. I choose green and red for on and off;

    I also changed the variable name(wasn;t right) to:
    Color GR = rm1L.getBackground();

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: jbutton states and events

    Color.green is an instance of a Color object. You could use the Color class's equals() method to compare two color objects.
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    ryuti is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: jbutton states and events

    Quote Originally Posted by Norm View Post
    Color.green is an instance of a Color object. You could use the Color class's equals() method to compare two color objects.
    hi again,

    You mentioned a very interesting method, but I could not use it(can't get it working - like I said before new to Java). Because of my lack of time i will leave that one for my spare hours.

    I found another way to do what i needm with jToggleButton, Here is my example, maybe someone will need it.


    Java Code:
    	JToggleButton rm8L = new JToggleButton("LIGHT");
    	
    	
    	rm8L.setSize(new Dimension(79, 23));
    	rm8L.setBackground(Color.GRAY);
    		
    		
    		rm8L.addItemListener(new ItemListener(){
    			public void itemStateChanged(ItemEvent ie){
    				if(rm8L.isSelected()){
    					rm8L.setText("LIGHT ON");
    					rm8L.setBackground(Color.GREEN);
    					}
    				else
    				{
    					rm8L.setText("LIGHT OFF");
    					rm8L.setBackground(Color.RED);
    				}
    			}
    			
    		});
    Thanks again for your assistance!

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: jbutton states and events

    You would use the equals method like this:
    if(aColor.equals(anotherColor)) { ..
    If you don't understand my response, don't ignore it, ask a question.

  11. #31
    ryuti is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: jbutton states and events

    Quote Originally Posted by Norm View Post
    You would use the equals method like this:
    if(aColor.equals(anotherColor)) { ..
    Yes, I got there at some point but I did not know to to implement that to an action, to change between colors when the button is clicked. Apart from color and text I need to display some other info into a text area(representing the temperature of the room, the energy loss in BTU, the energy needed from solar panels to heat the room and so on...)

    I will try that option but not now.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. applets states in browsers
    By laxtrappa in forum Java Applets
    Replies: 5
    Last Post: 03-24-2012, 05:24 PM
  2. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  3. JButton States Set to Pictures From Image Editor
    By wired-in=p in forum New To Java
    Replies: 10
    Last Post: 08-21-2011, 04:33 AM
  4. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM
  5. Help, events in Jbutton class
    By Heather in forum Java Applets
    Replies: 2
    Last Post: 06-30-2007, 04:32 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
  •