Results 1 to 2 of 2
  1. #1
    justsomeguy is offline Member
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Default Java actionlistener help

    I am making a Yahtzee game and i am having trouble getting the dice to change when i press my roll button. the dice are checkboxes so when they are selected, they can be "held".
    For clarity's sake, ill just put the code for changing one of my dice from a one to a three. All my buttons and checkboxes show up just fine but when I click the buttons nothing happens.

    Here are the relevant parts of my code:

    private JButton roll;
    private JCheckBox check;

    in the constructor:
    roll=new JButton("ROLL");
    roll.setActionCommand("roll");
    check=new JCheckBox();
    Check1.setIcon(Dice1); //Dice1 is an ImageIcon

    public void actionPerformed(ActionEvent e) {
    if ("roll".equals(e.getActionCommand())
    rollDice();}

    public void rollDice(){
    check.setIcon(Dice3);}

    if all of the above is fine, then my problem is most likely with repainting. which leads to my other question...how would I get the screen to repaint itself after every time i press a button?

    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public void rollDice(){
        check.setIcon(Dice3);
        // If the new icon is larger/smaller than the older
        // one you might need to ask for a new layout (but
        // likely not). You just have to try and see if you
        // need it. You do that with the JComponent method
        // [i]revalidate[/i] or the Container method [i]validate[/i].
        check.revalidate();  // Often not needed.
        // Generally needed for this.
        check.repaint();
    }

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2009, 10:31 PM
  2. Demonstrating the ActionListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:20 PM
  3. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM
  4. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:24 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
  •