Results 1 to 4 of 4
  1. #1
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default adding Image to JButton

    I am looking to add an Image to a JButton after it has been pressed. Im not sure of how to do this and I cant find much about it online, I found setPressedIcon(); but i've tried and got nothing. Any help would be great. this is a little of what i have so far. Thanks



    Container c = getContentPane();
    ImageIcon knight = new ImageIcon("knight.jpg");
    private JButton[][] buttons = new JButton[length][height];

    public Knight(){

    setTitle("Knight's Tour");
    setSize(1000, 1000);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    c.setLayout(null);

    //Initialize and add the buttons
    for (int i = 0; i < buttons.length; i++)
    {
    for (int j = 0; j < buttons[i].length; j++)
    {
    buttons[i][j] = new JButton();
    buttons[i][j].setPressedIcon(new ImageIcon("knight.jpg"));
    buttons[i][j].setSize(100, 100);
    buttons[i][j].setLocation(10 + (i * 100), 10 + (j * 100));
    //buttons[i][j].addActionListener(buttons);
    c.add(buttons[i][j]);
    }
    }

    setVisible(true);

    }


    private class buttonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent a)
    {

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

    Default

    Changing a button's image is sometimes as easy as just setting it's image icon via setIcon(Icon icon).

    Please describe in greater detail what you want to have happen on button press. Do you want to change the image on the button and for this to remain after the press has occurred? Perhaps you want to look at a JToggleButton instead (hard to say given the info at hand)?

  3. #3
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    I have 64 buttons (8x8) board. I want all the buttons to be empty at first and after I click each button I want to add a image to it.

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

    Default

    Then the simplest is probably to set the icon as I described above.

Similar Threads

  1. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  2. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 PM
  3. adding image in a frame(urgent)
    By arunkumarinfo in forum NetBeans
    Replies: 0
    Last Post: 02-23-2009, 10:49 AM
  4. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 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
  •