Results 1 to 5 of 5
Thread: strange refreshing behavior
- 12-27-2008, 04:47 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
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
-
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 { }
Last edited by Fubarable; 12-27-2008 at 05:03 PM.
-
Edit:
and in fact that should work. If you replace:
Java Code:south1.repaint(); south3.repaint(); south.repaint(); frame.repaint();
Java Code:south.revalidate();
- 12-27-2008, 05:49 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
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??
-
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
-
Same Behavior of VB's Method's Static Variable
By erosszz_jp@yahoo.co.jp in forum New To JavaReplies: 6Last Post: 10-30-2008, 05:54 PM -
Object class's equals() method behavior????
By skyineyes in forum New To JavaReplies: 4Last Post: 07-20-2008, 12:58 AM -
Why my JTree always collasped after refreshing?
By lmsook10 in forum AWT / SwingReplies: 2Last Post: 06-24-2008, 06:55 AM -
Bug in refreshing jsp
By anki1234 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-31-2007, 08:09 AM -
how to stop refreshing page
By cecily in forum New To JavaReplies: 1Last Post: 07-24-2007, 02:25 AM
Bookmarks