Page 1 of 2 12 LastLast
Results 1 to 20 of 31
  1. #1
    ryuti is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default jbutton states and events

    Hi all,

    I am really new to java and I am working for my school assignment. I am a bit under pressure because is due this Friday.

    I am trying to create a jbutton which will perform an action(in my case will switch on/off the light or heating). I would like my jbutton when is clicked to perform this action but also to change the text from - Light ON to Light OFF and to change the background color from green to red with the correct text displayed.

    This is what i got so far:

    //my controller.java file extends the jframe and i have

    ....

    JButton rm1L = new Jbutton("LIGHT ON");
    ....

    //in my constructor i am setting the background color to green
    public Controller()

    {
    rm1L.setBackground(Color.GREEN);

    }



    //..my second file in the GUI interface representing the control panel extends the previous java file
    // my constructor add a action listener


    public MainControlPanel(){

    rm1L.addActionListner(this);

    .....

    }

    // the action performed method ... here is my confusion I think

    @Override
    public void actionPerformed(ActionEvent me) {

    if(me.getSource() == rm1L){
    rm1L.setBackground(Color.RED);
    rm1L.setText("Lights OFF");
    }
    Thank you!
    Last edited by ryuti; 03-26-2012 at 01:08 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    What happens when you execute the code?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: jbutton states and events

    nothing happens.

    to simply understand what the project is about:

    I need to be able to turn on.off the light, to turn on.off heating, to adjust the heating in a specific room, influenced by the outside temperature(threads will be implemented here).

    My GUI compiles but nothing happens when I click the button. Also the jbutton doesn't seems to recognize the Green background set initially.
    rm1L.setBackground(Color.GREEN);

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    Hard to say what the code is doing without seeing a complete program. Can you make a small, simple program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: jbutton states and events

    As Norm says, this is hard to see with such a small amount of (incomplete) code.

    One thing, do you have multiple copies of this button?
    I see it mentioned in two different classes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: jbutton states and events

    I tried to attach the files but is not working(IE, Mozilla, Chrome). I am posting the code here. I have left buttons just for 1 room. You need to use miglayout15-swing.jar for my layout. I have it but i cannot upload.

    To answeer to your question Tolls, I do have multiple copies for the buttons. I need the same buttons for 8 rooms but they are named different(rm1L, rm1H, rm2L, rm2H....)


    //Controller.java
    Java Code:
    import java.util.Calendar; 
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import net.miginfocom.swing.MigLayout;
    
    public class Controller extends JFrame {
    
    
    		
    		JPanel panel = new JPanel();
    		JPanel panel_1 = new JPanel();
    		JPanel panel_2 = new JPanel();
    		JPanel panel_3 = new JPanel();
    
    		JLabel lblNewLabel = new JLabel("ROOM 1");
    		
    		
    		JButton rm1L = new JButton("LIGHT ON");
    		JButton rm1H = new JButton("HEAT ON");
    				
    		
    		
    
    		
    		JSlider rm1slider = new JSlider();
    		
    
    
    		JScrollPane scrollPane = new JScrollPane();
    
    			
    
            public Controller()
            {
            	
        		setForeground(Color.RED);
        		setBounds(100, 100, 697, 516);
        		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		getContentPane().setLayout(new BorderLayout(0, 0));
            	
        		panel.setBackground(Color.DARK_GRAY);
        		getContentPane().add(panel, BorderLayout.NORTH);
        		panel.setLayout(new MigLayout("", "[89px][89px][89px][][][][][][][]", "[][][][][]"));
        		
        		getContentPane().add(scrollPane, BorderLayout.CENTER);
    			
        		panel_1.setSize(new Dimension(200, 0));
        		panel_1.setBackground(Color.DARK_GRAY);
        		getContentPane().add(panel_1, BorderLayout.WEST);
        		panel_1.setLayout(new MigLayout("", "[89px][23px]", "[][23px][][][][][][][][][]"));
        		
        		panel_2.setBackground(Color.DARK_GRAY);
        		getContentPane().add(panel_2, BorderLayout.SOUTH);
        		panel_2.setLayout(new MigLayout("", "[89px][89px][89px][][][][][][][]", "[][][][][]"));
        		
        		panel_3.setBackground(Color.DARK_GRAY);
        		getContentPane().add(panel_3, BorderLayout.EAST);
        		panel_3.setLayout(new MigLayout("", "[][]", "[][][][][][][][][][][]"));
        		
        		
    			lblNewLabel.setForeground(Color.WHITE);
    			lblNewLabel.setFont(new Font("Verdana", Font.PLAIN, 11));
    
    	
    	
    			rm1L.setSize(new Dimension(79, 23));
    rm1L.setBackground(Color.GREEN);
    			rm1H.setSize(new Dimension(79, 23));
    rm1H.setBackground(Color.GREEN);
    
    			rm1slider.setSize(new Dimension(89, 5));
    	
    			
            	
        		
        		panel_1.add(lblNewLabel, "cell 0 6");
        		panel_1.add(rm1L, "cell 0 7,alignx center");
        		panel_1.add(rm1H, "cell 0 8,alignx left,aligny top");
        		panel_1.add(rm1slider, "cell 0 10");
        		
        		setVisible(true);
            	
            	
            }
            
            public static void main (String args[]) {
        		
        		
        		Controller cont = new Controller();
        		
        		
        }
    }


    //MainControlPanel.java
    Java Code:
    import java.awt.Button;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import java.awt.Color;
    import javax.swing.JScrollPane;
    import javax.swing.JButton;
    import net.miginfocom.swing.MigLayout;
    import javax.swing.JLabel;
    import java.awt.Font;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JSlider;
    import java.awt.Dimension;
    import javax.swing.AbstractAction;
    import java.awt.event.ActionEvent;
    import javax.swing.Action;
    import java.awt.event.MouseMotionAdapter;
    import javax.swing.event.ChangeListener;
    import javax.swing.event.ChangeEvent;
    
    public class MainControlPanel extends Controller implements ActionListener{
    	
    	public MainControlPanel(){
    		
    		rm1L.addActionListener(this);
    		rm1H.addActionListener(this);
    		
    		
    	
    	}
    
    	public void actionPerformed(ActionEvent me){
    		if(me.getSource() == rm1L){
                rm1L.setBackground(Color.RED);
                rm1L.setText("LIGHT OFF");
                repaint();
    			}
    			
    		if(me.getSource() == rm1H){
                rm1L.setBackground(Color.RED);
                rm1L.setText("HEAT OFF");
                repaint();
    			}
    			
    		
    		}
    	
    
    }

    Thank you!
    Last edited by ryuti; 03-26-2012 at 03:10 PM.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    How do you test with the above code. There is only one main() method. The MainControlPanel class is not used.

    Can you explain what happens when you execute the code you just posted? For example there is no green color in it.
    Last edited by Norm; 03-26-2012 at 03:20 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: jbutton states and events

    i test it with java MainControlPanel

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: jbutton states and events

    That will only result in a Controller being constructed, not a MainControlPanel.
    Since that's the code in the main() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    Can you explain what the problem is with the code you just posted?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    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
    Can you explain what the problem is with the code you just posted?
    The code does not change the buttons background colors and does not change the text from Light On to Light Off or, Heat On to Heat Off.


    I have created Controller class to be able to extend from there 2 classes, MainControlPanel and RemotePanel. Practically they must do the same thing, the only difference is the layout.

    Do I need to create a main into the MainControlPanel to be able to apply the actionListener?

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

    Default Re: jbutton states and events

    i have added a main in MainControlPanel and is working when the button is clicked. You where right about testing just the Controller class.
    I want to ask something else if posibble:

    if I click on the button changes to red but if i click again does not change back to green.
    How can I make use of an if else statement here? or do I need to use something else to be able to swich between those two states every time when the button is clicked(On and OFF).

    Thank you!
    Last edited by ryuti; 03-26-2012 at 05:05 PM.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    does not change back to green.
    There is no code to change the color to green.

    If you want to toggle the color between two colors, you need a way to detect the current color so you can change it to the other color. You could get the current color and test its value or you could use a variable that says what the current color is or use the value of the label's text.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    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
    There is no code to change the color to green.

    If you want to toggle the color between two colors, you need a way to detect the current color so you can change it to the other color. You could get the current color and test its value or you could use a variable that says what the current color is or use the value of the label's text.
    Ok. The current color needs to be tested in the actionPerformed? I understand what you mean but I do not know how to apply this.

    Thanks!

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    Use an if statement to test the current value of the button's color. When you know what the current color is, you can decide what color to change to.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    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
    Use an if statement to test the current value of the button's color. When you know what the current color is, you can decide what color to change to.
    I still can;t get it working

    i have added this bit to create an inner class:
    Java Code:
    rm1L.addActionListener(new RM1());
    
    ...
    
    class RM1 implements ActionListener(ActionEvent e) {
    if(getSource("LIGHT ON"))
    {setBackground(Color.GREEN);}
    else
    {
    setBackground(Color.RED);
    setText("LIGHT OFF");
    
    }
    }
    I am executing the if statement in the right way?
    Thanks!

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    How does the getSource() method work? What does it look at to make the decision and return true or false?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: jbutton states and events

    getSource()
    {return JButton;}

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: jbutton states and events

    What is that last post supposed to be? It is not a valid method definition. What is it?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: jbutton states and events

    sorry!

    there is no method for getSource(). I was looking to something else

    I tried to use the same approach like

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == rm1L){.....}
    }

Page 1 of 2 12 LastLast

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
  •