Results 1 to 4 of 4
  1. #1
    3RDofApril is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Removing icon from JButton

    I'm trying to make a game of checkers. I have made the board into 64 JButtons(In a grid of 8x8), and have made the "pieces" icons(pictures of actual pieces). However, I'm now trying to write the code for the basis of gameplay, but ran into a problem. Is there a way to remove the icon from a specific Button and then add an icon to another without adding new buttons and therefore ruining my gameboard. Also, if you can think of a more efficient/effective way, I'm all ears.

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

    Default

    The JButton API has a method called setIcon that allows you to change the icon of a button (actually it's a method of the parent of JButton, AbstractButton). So a skill that you must master to move forward will be to get this stuff from the API on your own.

    Myself, in a program like this, I like clicking on a piece and then dragging the piece with the mouse to another square, letting go, and if the square is valid, having the piece appear in the new square. One way to do this sort of thing is by using a JLayeredPane and a MouseListener and MouseMotionListener (actually combined in a MouseAdapter). I've written an example of something like this here: questions about events in differents layers

  3. #3
    3RDofApril is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    I'm confused about what AbstractButton is, but I like the idea of drag and drop as well so I will look into that. Thank you for the quick response

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

    Default

    Quote Originally Posted by 3RDofApril View Post
    I'm confused about what AbstractButton is
    You're familiar with inheritance, I assume. Again, look at the JButton API and it will tell you the inheritance tree for a JButton. The direct ancestor for JButton is AbstractButton which inherits from JComponent, which is a child of Container, .....


    It's all about OOPs, baby! :)

Similar Threads

  1. JButton with Icon & text alignment issue
    By ShardaD in forum AWT / Swing
    Replies: 4
    Last Post: 01-11-2011, 11:10 AM
  2. Removing a JButton
    By Lingerz in forum New To Java
    Replies: 15
    Last Post: 01-10-2010, 12:18 AM
  3. Removing Buttons?
    By Mass Zero in forum Java Applets
    Replies: 10
    Last Post: 10-28-2009, 03:16 PM
  4. Removing Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 07:23 AM
  5. My icon extends my JButton
    By hitmen in forum AWT / Swing
    Replies: 5
    Last Post: 03-06-2009, 07:49 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
  •