Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-08-2008, 12:15 AM
Member
 
Join Date: Feb 2008
Posts: 12
Deathmonger is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-08-2008, 01:17 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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."
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-12-2008, 03:51 PM
Member
 
Join Date: Feb 2008
Posts: 12
Deathmonger is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
pls help me out its critical ...... how can we use an arralylist reference raj reddy Advanced Java 1 04-15-2008 01:09 PM
pls help me out its critical ...... how can we use an arralylist reference raj reddy Web Frameworks 0 04-15-2008 09:12 AM
reference to elements in array Igor New To Java 1 12-14-2007 12:56 PM
Object and reference katie New To Java 1 08-07-2007 12:00 AM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 04:19 PM


All times are GMT +3. The time now is 10:49 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org