Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default Help with Chess GUI

    I've been working on a Chess program GUI.
    I've set this up as JButtons in a GridLayout, except I made a subclass of JButton (JGridButton) that stores its position on the gird.
    To find out from which JButton the event was fired, I'm using getSource() (Is there any other way to do this?).
    Since this returns an Object and not the specific object type that it was from, I'm currently casting it as a JGridButton.
    My question is - what exactly is going on here when I cast it? Does the information (about the position) get passed on?

    Upon review I think this might be better of in the New to Java since the question really isn't GUI specific.
    Last edited by Lord Voldemort; 01-24-2011 at 01:30 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's kind of hard to know why you're having problems given the code that you've posted so far. Nothing in your code above suggests (to me at least) why you should be getting this error. So that suggests to me that your error lies elsewhere in code not shown.

  3. #3
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default

    Sorry I'll edit my post.
    Reading over it again it doesn't even ask the original question very well.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If a JGridButton was pressed and has an ActionListener added to it, then calling getSource will return the JGridButton that was pressed. If this is not happening, then there's a bug somewhere in your code.

  5. #5
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default

    It returns an Object and not a JGridButton though.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Lord Voldemort View Post
    It returns an Object and not a JGridButton though.
    It doesn't matter. I fear that you may be confusing the type of the variable with the type of the object it refers to. If the object that initiated the event is a JGridButton, then the Object returned is that very JGridButton. Casting it to JGridButton is a viable solution, if you are sure that it is this. So no having your GUI implement ActionListener here as you want to give these buttons there own actionlistener class.

  7. #7
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default

    I'm sorry I didn't understand exactly what you meant by, or how to do it:
    "So no having your GUI implement ActionListener here as you want to give these buttons there own actionlistener class."

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What I meant was you shouldn't have your class look something like so:

    Java Code:
    // imports go here...
    
    public MyGui extends JFrame [b][color="red"]implements ActionListener[/color][/b] {
    
      //....
    
    }

    Instead use either a private inner class that implements ActionListener or a stand-alone class that implements ActionListener.

  9. #9
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default

    Again, I can't understand why my GUI should have a custom Action Listener... sorry :confused:
    I seem to have the GUI working except from some issues with my other code that I'm resolving, I can post this if it would help explain something. The thing is, I don't really know how ActionListener works apart from the fact that it gets certain ActionEvents and once it gets that then it executes the code I tell it to.
    Last edited by Lord Voldemort; 01-24-2011 at 04:23 PM.

  10. #10
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    My question is - what exactly is going on here when I cast it? Does the information (about the position) get passed on?
    I'm going to take a stab at what I think you are asking. Others please correct me
    if I'm wrong.

    Your JGridButton is a type of object. When you cast it as such you are telling the compiler how to treat it. Your position information is already part of the object,
    the cast is just showing how to get to it.

Similar Threads

  1. Chess program
    By Chro in forum New To Java
    Replies: 1
    Last Post: 10-26-2010, 09:20 AM
  2. chess game
    By michail in forum New To Java
    Replies: 13
    Last Post: 01-12-2010, 07:24 AM
  3. Chess game
    By michail in forum New To Java
    Replies: 4
    Last Post: 12-23-2009, 06:51 PM
  4. I want play chess in Java
    By ganzorig in forum Advanced Java
    Replies: 6
    Last Post: 11-15-2009, 04:53 AM
  5. New Chess-Like game
    By jSarK0Y in forum Reviews / Advertising
    Replies: 3
    Last Post: 06-10-2009, 03:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •