Results 1 to 5 of 5
  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default strange refreshing behavior

    Hey guys, been working on my hangman project. I created a method to set the text of two sets of JLabels. The first one I set as an array of JLabels, each label representing one letter the chosen word for the game, and the second one is simply a map<String, JLabel> of the whole alphabet. I added each of these labels to a JPanel with FlowLayout, and then added these to a JPanel with GridLayout which I placed in the southern region of the frame. I added everything to the frame first before adding the JLabels to them because I wanted the whole frame to appear before I prompted for the game word.

    However there is a strange problem. Even though I am using repaint on both of the JPanels AND on the frame, the letters do not appear unless I resize the window. I thought repaint was supposed to fix this problem but apparently it doesn't. Why is it that the letters appear after I resize it but not immediately?

    Here is my code so far:

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*; //for map
    
    public class Hangman {
      public static void main(String[] args) {
        Hangman hangman = new Hangman();
      }
      
      JFrame frame = new JFrame();
      JLabel winLabel;
      JButton bNew;
      JPanel south1;
      JPanel south2;
      JPanel south3;
      JPanel south4;
      JPanel south;
      JLabel[] wordArray;
      //JLabel[] abcArray;
      Map<String, JLabel> abcMap;
      
      private int tries = 0;
      private int wrong = 0;
      
      public Hangman() {
        HangmanPanel hPanel = new HangmanPanel();
        hPanel.setPreferredSize(new Dimension(400, 300));
        abcMap = new HashMap<String, JLabel>();
        
        winLabel = new JLabel("");
        bNew = new JButton("New");
        
        south1 = new JPanel(new FlowLayout());
        south1.setForeground(Color.WHITE);
        south1.setBackground(Color.WHITE);
        
        south2 = new JPanel(new FlowLayout());
        south2.setBackground(Color.WHITE);
        south2.add(winLabel);
        
        south3 = new JPanel(new FlowLayout());
        south3.setBackground(Color.WHITE);
        
        south4 = new JPanel(new FlowLayout());
        south4.setBackground(Color.GRAY);
        south4.add(bNew);
        
        south = new JPanel(new GridLayout(4, 1));
        
        south.add(south1);
        south.add(south2);
        south.add(south3);
        south.add(south4);
        
        frame.setLayout(new BorderLayout());
        frame.setTitle("Hangman");
        frame.setSize(new Dimension(400,400));
        frame.setLocationRelativeTo(null);
        frame.add(hPanel, BorderLayout.CENTER);
        frame.add(south, BorderLayout.SOUTH);
        
        frame.pack();
        
        frame.setVisible(true);
        
        String gameWord = getWord();
        int length = gameWord.length();
        wordArray = new JLabel[length];
        createBoard(gameWord, length);
      }
      
      private void createBoard(String gameWord, int length) {
        for(int x = 0; x < length; x++) {
          wordArray[x] = new JLabel("" + gameWord.charAt(x));
        }
        
        for(int x = 0; x < 26; x++) {
          //abcArray[x] = new JLabel("" + (char)(x + 97));
          abcMap.put("" + (char)(x + 97), new JLabel("" + (char)(x + 97)));
        }
        
        for(int x = 0; x < length; x++) {
          south1.add(wordArray[x]);
        }
        
        for(int x = 0; x < 26; x++) {
          //south3.add(abcArray[x]);
          south3.add(abcMap.get("" + (char)(x + 97)));
        }
        
        south1.repaint();
        south3.repaint();
        south.repaint();
        frame.repaint();
      }
    
      private String getWord() {
        return JOptionPane.showInputDialog(frame, "Please enter word.");
      }
    }
    
    
    
    And here is the other class:
    
    
    
    import javax.swing.*;
    import java.awt.*;
    
    public class HangmanPanel extends JPanel {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);
      }
    }
    Last edited by diggitydoggz; 12-27-2008 at 05:48 PM. Reason: incomplete code

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

    Default

    Your posted code is incomplete. You may wish to read up on how to create and post an SSCCE to these forums. This will allow us to compile and run a small program that illustrates your problem.

    My other suggestion: have you tried revalidating your JPanels?

    edit:
    adding this:
    Java Code:
    class HangmanPanel extends JPanel
    {
    
    }
    to the bottom of your Hangman class allows your code to be compilable making it an SSCCE. You were almost there. ;)
    Last edited by Fubarable; 12-27-2008 at 05:03 PM.

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

    Default

    Edit:
    and in fact that should work. If you replace:
    Java Code:
        south1.repaint();
        south3.repaint();
        south.repaint();
        frame.repaint();
    with
    Java Code:
        south.revalidate();
    you may be pleasantly surprised.

  4. #4
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Code edited.

    I just tried revalidate. Thanks man. That worked. Why is it that repaint didn't work in this situation and that revalidate did? The API is so confusing... said something about a deferred automatic layout??

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

    Default

    repaint just tells the component to repaint itself. revalidate tells the container's layout manager to relayout the components on the container. If you've added or changed components in a container, you should call revalidate else there's nothing there to repaint. repaint is also needed at times, especially if you remove components from a container.

Similar Threads

  1. Same Behavior of VB's Method's Static Variable
    By erosszz_jp@yahoo.co.jp in forum New To Java
    Replies: 6
    Last Post: 10-30-2008, 05:54 PM
  2. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 12:58 AM
  3. Why my JTree always collasped after refreshing?
    By lmsook10 in forum AWT / Swing
    Replies: 2
    Last Post: 06-24-2008, 06:55 AM
  4. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 08:09 AM
  5. how to stop refreshing page
    By cecily in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 02:25 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
  •