Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JToggleButton's icon won't show up when pressed!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation JToggleButton's icon won't show up when pressed!

    Hi. I'm making a minesweeper game and I'm trying to assign icons to each button for when they're pressed, but for some reason they don't show up after I press them. I'll just post the code here.

    private void setButtonValues(int iRows, int iCols)
        {
            for (int i = 0 ; i < iRows ; i++ )
                for (int j = 0 ; j < iCols ; j++ )
                   if ( buttons[i][j].getDisabledIcon() != mine )
                      switch(addValues(i , j))
                      {
                          case 0: buttons[i][j].setDisabledIcon(null); break;
                          case 1: buttons[i][j].setDisabledIcon(value1); break;
                          case 2: buttons[i][j].setDisabledIcon(value2); break;
                          case 3: buttons[i][j].setDisabledIcon(value3); break;
                          case 4: buttons[i][j].setDisabledIcon(value4); break;                      
                      }
        }
     
        private int addValues(int iRow, int iCol)
        {   int surroundingMines = 0;
            for (int i = -1 ; i <= 1 ; i++ )
                for ( int j = -1 ; j <= 1 ; j++ )
                    if(notOutOfBounds(iRow + i, iCol + j) && (i!= 0 || j != 0))
                    {                
                        if(buttons[iRow + i][iCol + j].getDisabledIcon() == mine)
                        surroundingMines++;
                    }
            return surroundingMines;
      }

    What this does is basically scan the buttons around and check how many mines there are and give the final value which is assigned by adding a picture with the corresponding number on it. For some reason they don't appear when I press the button, even though I know they are assigned since I have this in the event handler:

    if(b.getDisabledIcon()== BoardPanel.getMine())
            JOptionPane.showMessageDialog(null, "You lost!");

    Which actually works and lets me know when I've clicked a mine, even if it doesn't display the picture. What am I missing? Is there another method for this?

    Thank you!


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: JToggleButton's icon won't show up when pressed!

    Where are you initializing the variable for the images..

Similar Threads

  1. Pausing a loop until a button is pressed
    By amcqueen in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 01:57 AM
  2. Trying to start a game when 'Enter' is pressed.
    By Shaybay92 in forum AWT / Java Swing
    Replies: 6
    Last Post: September 27th, 2011, 07:48 AM
  3. Is Key Pressed statements
    By Yo Cas Cas in forum AWT / Java Swing
    Replies: 6
    Last Post: August 27th, 2011, 12:48 AM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM