Results 21 to 31 of 31
Thread: jbutton states and events
- 03-26-2012, 09:49 PM #21
- 03-26-2012, 10:00 PM #22
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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"); } }
- 03-26-2012, 10:12 PM #23
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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.
- 03-26-2012, 10:15 PM #24
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.
- 03-26-2012, 10:32 PM #25
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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!
- 03-26-2012, 10:37 PM #26
Re: jbutton states and events
The syntax for method reference ends with ()srm1L.getBackground;
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.
- 03-26-2012, 10:49 PM #27
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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();
- 03-26-2012, 10:54 PM #28
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.
- 03-27-2012, 03:39 AM #29
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
Re: jbutton states and events
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.
Thanks again for your assistance!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); } } });
- 03-27-2012, 03:46 AM #30
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.
- 03-27-2012, 03:59 AM #31
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
Re: jbutton states and events
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.
Similar Threads
-
applets states in browsers
By laxtrappa in forum Java AppletsReplies: 5Last Post: 03-24-2012, 05:24 PM -
ActionListener for JButton after changing Button to JButton
By ravi.joshi53 in forum Java AppletsReplies: 2Last Post: 10-07-2011, 07:35 AM -
JButton States Set to Pictures From Image Editor
By wired-in=p in forum New To JavaReplies: 10Last Post: 08-21-2011, 04:33 AM -
typed events vs untyped events.
By Drun in forum SWT / JFaceReplies: 0Last Post: 11-23-2009, 12:22 PM -
Help, events in Jbutton class
By Heather in forum Java AppletsReplies: 2Last Post: 06-30-2007, 04:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks