Results 1 to 10 of 10
  1. #1
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default How To Check What Image Is Displayed

    Hello, I am building a tictactoe game and I wanted to have it so you couldn't just click a button that has already been used, so my question is how would I check what image is being used on a button.

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    getIcon() will return the icon associated with the button.

    I'm not sure what you mean by a button "that has already been used", but if it is your intention that a button be clicked only once the easiest thing might be to disable it when it is clicked.

  3. #3
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    getIcon() will return the icon associated with the button.

    I'm not sure what you mean by a button "that has already been used", but if it is your intention that a button be clicked only once the easiest thing might be to disable it when it is clicked.
    How would I disable the button? I tried *button*.disable(); but that didn't work

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Don't try and make up methods. Search for button methods that really exist nearby the link already given. Disabling a button is setting an aspect of its state, so I would start by surveying the setXXX() methods.

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    I should have added the other goto place: Oracle's Tutorial has a page on How To Use Buttons whose example includes a buttons that are disabled and enabled. Between them the Tutorial's overview and the javadoc's detail are resources that always ought to be open in your browser when you are writing code.

  6. #6
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Don't try and make up methods. Search for button methods that really exist nearby the link already given. Disabling a button is setting an aspect of its state, so I would start by surveying the setXXX() methods.
    I did look at the button methods one came up as disable, but I found another one that was setEnable(false) the only problem is when a button isn't enabled the icon isn't either

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Remove the ActionLIstener from the button once it is clicked.

  8. #8
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Remove the ActionLIstener from the button once it is clicked.
    I actually just figured it out on my own, but thanks a lot for you help!

    Here is what I did
    Java Code:
    if(X == ((AbstractButton) event.getSource()).getIcon()){
    				JOptionPane.showMessageDialog(null, "Spot Is Already Taken");
    I had tried getIcon before, but the reason it didn't work is I didn't have the abstract button code.

  9. #9
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    I did look at the button methods one came up as disable
    Whoops, you're right. My mistake: disable() is the deprecated version of what we now write as setEnabled(false).

    the only problem is when a button isn't enabled the icon isn't either
    I'm not sure I understand this. The Tutorial example - belatedly remembered - has an example where buttons are disabled and their icons (little triangles) remain visible although "dimmed".

    [Edit]: Our posts crossed. I've just caught up and realise what you are getting at. I'm glad you've got it doing what you want.
    Last edited by pbrockway2; 08-14-2011 at 05:44 AM.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    The easiest way to have the same Icon, not dimmed, whether the button is enabled or disabled is
    Java Code:
    button.setDisabledIcon(button.getIcon());
    db

Similar Threads

  1. Saving output displayed in a frame in image format
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 05-25-2011, 03:28 PM
  2. Check if image contains mouse
    By trishtren in forum Java 2D
    Replies: 2
    Last Post: 05-15-2011, 11:49 PM
  3. How do I check if user clicks on an image?
    By rajkobie in forum New To Java
    Replies: 10
    Last Post: 05-04-2011, 05:45 AM
  4. TextField not displayed
    By weezy2894 in forum Java Applets
    Replies: 6
    Last Post: 04-18-2011, 03:37 AM
  5. ImageIcon : Image not displayed
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 08:36 AM

Tags for this Thread

Posting Permissions

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