Results 1 to 3 of 3
  1. #1
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default Getting the Object Reference Name

    Hi, I'm creatinga simple Lights Out game, and I'm trying to figure out a way to access the name of the button that is pressed. So if I have three buttons named a1, a2, and a3, and the user presses on a2, then I can have a String that is equal to "a2." I looked up getSource() and getAccessibleName(), but I'm not entirely sure how to use it correctly. Any help is greatly appreciated.

    Here is my code, the problem right now is with the method actionPerformed(), and I'm pretty sure this isn't the most efficient way to the write the code for this program either. So any tips would also be great.

    Java Code:
    import javax.accessibility.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.lang.Object.*;
    
    public class LightsOut {
    	
    	static JButton a1;
    	static JButton a2;
    	static JButton a3;
    	static JButton a4;
    	static JButton b1;
    	static JButton b2;
    	static JButton b3;
    	static JButton b4;
    	static JButton c1;
    	static JButton c2;
    	static JButton c3;
    	static JButton c4;
    	static JButton d1;
    	static JButton d2;
    	static JButton d3;
    	static JButton d4;
    	static JLabel test;
    	static JFrame frame;
    	static Container pane;
    	
    	static class OnOff implements ActionListener {
    		
    		public void actionPerformed(ActionEvent ae) {
    			JButton a = new JButton();
    			String whichButton;
    			a = ae.getSource();
    			whichButton = a.getAccessibleName();
    			test.setText(whichButton);
    			changeLights(whichButton);
    		}
    		
    		public void changeLights(String button) {
    			int row;
    			int col;
    			char first;
    			
    			first = button.charAt(0);
    			col = Integer.valueOf(button.charAt(1)).intValue();
    			
    			switch(first) {
    			case('a') :
    				switch(col) {
    				case(1) :
    					switchLight(a2);
    					switchLight(b1);
    					break;
    				case(2) :
    					switchLight(a1);
    					switchLight(a3);
    					switchLight(b2);
    					break;
    				case(3) :
    					switchLight(a2);
    					switchLight(a4);
    					switchLight(b3);
    					break;
    				case(4) :
    					switchLight(a3);
    					switchLight(b4);
    					break;
    				} break;
    			case('b') :
    				switch(col) {
    				case(1) :
    					switchLight(a1);
    					switchLight(c1);
    					switchLight(b2);
    					break;
    				case(2) :
    					switchLight(b1);
    					switchLight(a2);
    					switchLight(b3);
    					switchLight(c2);
    					break;
    				case(3) :
    					switchLight(b2);
    					switchLight(b4);
    					switchLight(a3);
    					switchLight(c3);
    					break;
    				case(4) :
    					switchLight(b3);
    					switchLight(a4);
    					switchLight(c4);
    					break;
    				} break;
    			case('c') :
    				switch(col) {
    				case(1) :
    					switchLight(b1);
    					switchLight(c2);
    					switchLight(d1);
    					break;
    				case(2) :
    					switchLight(b2);
    					switchLight(c1);
    					switchLight(c3);
    					switchLight(d2);
    					break;
    				case(3) :
    					switchLight(b3);
    					switchLight(c2);
    					switchLight(c4);
    					switchLight(d3);
    					break;
    				case(4) :
    					switchLight(b4);
    					switchLight(c3);
    					switchLight(d4);
    					break;
    				} break;
    			case('d') :
    				switch(col) {
    				case(1) :
    					switchLight(c1);
    					switchLight(d2);
    					break;
    				case(2) :
    					switchLight(c2);
    					switchLight(d1);
    					switchLight(d3);
    					break;
    				case(3) :
    					switchLight(c3);
    					switchLight(d2);
    					switchLight(d4);
    					break;
    				case(4) :
    					switchLight(c4);
    					switchLight(d3);
    					break;
    				} break;
    			}
    		}
    		
    		public void switchLight(JButton button) {
    			String status;
    			status = button.getText();
    			
    			if(status.equals("^OFF^")) {
    					status = "*ON*";
    			}
    			else {
    				status = "^OFF^";
    			}
    			button.setText(status);
    		}
    	}
    	
    	public static void main(String[] args) {
    		OnOff oo = new OnOff();
    		
    		a1= new JButton("^OFF^");
    		a2= new JButton("^OFF^");
    		a3= new JButton("^OFF^");
    		a4= new JButton("^OFF^");
    		b1= new JButton("^OFF^");
    		b2= new JButton("^OFF^");
    		b3= new JButton("^OFF^");
    		b4= new JButton("^OFF^");
    		c1= new JButton("^OFF^");
    		c2= new JButton("^OFF^");
    		c3= new JButton("^OFF^");
    		c4= new JButton("^OFF^");
    		d1= new JButton("^OFF^");
    		d2= new JButton("^OFF^");
    		d3= new JButton("^OFF^");
    		d4= new JButton("^OFF^");
    		test = new JLabel();
    		frame = new JFrame("Lights Out");
    		pane = new Container();
    		
    		a1.addActionListener(oo);
    		a2.addActionListener(oo);
    		a3.addActionListener(oo);
    		a4.addActionListener(oo);
    		b1.addActionListener(oo);
    		b2.addActionListener(oo);
    		b3.addActionListener(oo);
    		b4.addActionListener(oo);
    		c1.addActionListener(oo);
    		c2.addActionListener(oo);
    		c3.addActionListener(oo);
    		c4.addActionListener(oo);
    		d1.addActionListener(oo);
    		d2.addActionListener(oo);
    		d3.addActionListener(oo);
    		d4.addActionListener(oo);
    		
    		frame.setSize(400,400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pane = frame.getContentPane();
    		pane.setLayout(new GridLayout(4,4));
    		pane.add(a1);
    		pane.add(a2);
    		pane.add(a3);
    		pane.add(a4);
    		pane.add(b1);
    		pane.add(b2);
    		pane.add(b3);
    		pane.add(b4);
    		pane.add(c1);
    		pane.add(c2);
    		pane.add(c3);
    		pane.add(c4);
    		pane.add(d1);
    		pane.add(d2);
    		pane.add(d3);
    		pane.add(d4);
    		pane.add(test);
    		frame.setVisible(true);
    	}
    }
    Jai guru deva om, Nothing's gonna change my world

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public void actionPerformed(ActionEvent ae) {
        // Who is sending this event?
        System.out.println("source class name = " +
                            ae.getSource().getClass().getName());
        // If JButton is the only kind/type of component
        // that has registered with this ActionListener
        // then this cast will be safe.
        JButton a = (JButton)ae.getSource();
        // Get the text on the button.
        String whichButton = a.getText();
        // Or, you can do:
        String ac = ae.getActionCommand();
        // Test for a certain button whose text is "right"
        if(ac.equals("right"))
            ; // found it
        System.out.println("whichButton = " + whichButton);
        test.setText(whichButton);
        changeLights(whichButton);
    }
    For another option you can use the setActionCommand(String) method for each button to set the actionCommand you want to use. This is especially useful if your app will be used in an international setting: the button text may be translated into another language and may/wll not match the string you test for in your event code. For example, the "right" button in Spanish may have the text translated to "dereche."

  3. #3
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default

    Thanks, that helps a lot. The setActionCommand() method is very useful. Thanks for showing it to me.
    Jai guru deva om, Nothing's gonna change my world

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 04:45 PM
  3. Replies: 1
    Last Post: 04-15-2008, 01:09 PM
  4. Replies: 0
    Last Post: 04-15-2008, 09:12 AM
  5. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 12:56 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
  •