Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default JPanel not refreshing

    I am creating a calendar program we need to do scheduling. I am using BorderLayout, the northern panel contains drop-down selections for month and year, the center panel contains the calendar itself. Upon initial entry the calendar for the current month is presented, with today's date highlighted. All works fine.

    When a month or year is selected from the drop-down I want to recreate the center panel. On the Mac where I have been developing it things work fine. Porting it to Windows finds that the center panel always shows the current month. Not having to recreate a panel before I am at a loss as to why it is not working on the Windows platform (XP).

    The code that calls the code to create the month panel is:
    Java Code:
    monthCombo.addActionListener(new ActionListener()
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		d.set(Calendar.MONTH, monthCombo.getSelectedIndex());
    		CreateMonth (d, false);
    		repaint();
    	}
    });

    The CreateMonth code is:
    Java Code:
    private void CreateMonth (GregorianCalendar date, boolean redraw)
    {
    	int curDate  = date.get(Calendar.DAY_OF_MONTH);
    	int curMonth = date.get(Calendar.MONTH);
    	int curYear  = date.get(Calendar.YEAR);
    
    // Month and Year fields
    	for ( String month : months )
    		monthCombo.addItem(month);
    	monthCombo.setSelectedIndex(curMonth);
    
    	for ( int i = 1900; i < 2101; i++ )
    		yearCombo.addItem(i);
    	yearCombo.setSelectedIndex(curYear - 1900);
    
    
    // add the Month and Years
    	if (redraw)
    	{
    		JPanel selectPanel = new JPanel();
    		selectPanel.setOpaque(true);
    		selectPanel.setBackground(new Color(153,255,153));
    		selectPanel.setLayout(new GridLayout(1,4));
    
    		selectPanel.add(todayButton);
    		selectPanel.add(monthCombo);
    		selectPanel.add(yearCombo);
    		selectPanel.add(mathButton);
    		add(selectPanel, BorderLayout.NORTH);
    	}
    
    
    	// add the days
    	JPanel monthPanel = new JPanel();
    	monthPanel.setOpaque(true);
    	monthPanel.setBackground(new Color(153,255,153));
    	monthPanel.setLayout(new GridLayout(7,7));
    	BevelBorder bevel = new BevelBorder(BevelBorder.LOWERED);
    
    	for (String day : days)
    	{
    		JLabel temp = new JLabel(day, JLabel.CENTER);
    		temp.setBorder(bevel);
    		temp.setOpaque(true);
    		temp.setBackground(Color.YELLOW);
    		monthPanel.add(temp);
    	}
    
     
    // Move days
    	GregorianCalendar gDate = new GregorianCalendar();
    	int curJulDay = 0;
    	if (gDate.isLeapYear(curYear))	curJulDay = leapJulDay[curMonth];
    	else							curJulDay = julianDay[curMonth];
    
    	int day = 1;
    	date.set(Calendar.DAY_OF_MONTH, 1);
    
    	for (int r = 0; r < 6; r++)
    	{
    		for (int c = 0; c < 7; c++)
    		{
    			JLabel temp = new JLabel("");
    
    			if (r == 0 && c < (date.get(Calendar.DAY_OF_WEEK) - 1))
    			{
    				monthPanel.add(temp);
    			}
    			else
    			{
    				if (day <= daysInMonth[curMonth])
    				{
    					temp = new JLabel("" + day + " / " + curJulDay++,  JLabel.CENTER);
    					temp.setBorder(bevel);
    					temp.setOpaque(true);
    
    					if (day == curDate)	temp.setBackground(new Color(000,255,255));
    					else				temp.setBackground(new Color(153,153,255));
    					day++;
    				}
    			}
    			monthPanel.add(temp);
    		}
    	}
    
    	add(monthPanel, BorderLayout.CENTER);
    	date.set(Calendar.DAY_OF_MONTH, curDate);
    }

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

    Default

    Could the issue be something as simple as needing to call revalidate and repaint on the BorderLayout-using JPanel after making your changes?

    If this suggestion doesn't help, please look into creating and posting an SSCCE that we can run and investigate. The related link in my signature will discuss the mechanics of this as well as the advantages of using this technique.

    Luck!

  3. #3
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Could the issue be something as simple as needing to call revalidate and repaint on the BorderLayout-using JPanel after making your changes?
    I'm not familiar with those methods. Do I call them on the panel in the center layout, or for the entire screen? Is there a sequence to calling them? Anything else I might need to know about using them?

    Sorry to ask, but it seems Oracle/Sun has made changes to their website recently and I can't access the Java Docs at all.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Thanks, Ph, but that takes me to a download.oracle.com page which opens blank.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    It takes me to the API Doc page you used to know from Sun.

    Is anyone else having problems with that link like the OP?
    Last edited by PhHein; 07-13-2010 at 07:31 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Works for me. I get to the site ok.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Thanks, Norm.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    To me FF displays a blank window, IE displays:

    Java Code:
     Internet Explorer cannot display the webpage 
       
       Most likely causes:
    You are not connected to the Internet. 
    The website is encountering problems. 
    There might be a typing error in the address. 
     
       What you can try: 
         Diagnose Connection Problems  
     
         More information 
    
    This problem can be caused by a variety of issues, including: 
    
    Internet connectivity has been lost. 
    The website is temporarily unavailable. 
    The Domain Name Server (DNS) is not reachable. 
    The Domain Name Server (DNS) does not have a listing for the website's domain. 
    If this is an HTTPS (secure) address, click Tools, click Internet Options, click Advanced, and check to be sure the SSL and TLS protocols are enabled under the security section. 
    
    For offline users
    
    You can still view subscribed feeds and some recently viewed webpages.
    To view subscribed feeds 
    
    Click the Favorites Center button , click Feeds, and then click the feed you want to view. 
    
    To view recently visited webpages (might not work on all pages) 
    
    Click Tools , and then click Work Offline. 
    Click the Favorites Center button , click History, and then click the page you want to view.
    Can anyone post the relevant content for me, or explain what needs done?

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by pahiker View Post
    Thanks, Ph, but that takes me to a download.oracle.com page which opens blank.
    It looks to me like the API documentation and tutorial links are being re-assigned by Oracle. They appear to be redirecting to the new locations, but some in other fora have noted delays and some problems with this.

  11. #11
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    So, can someone explain this to me?

    Originally Posted by Fubarable
    Could the issue be something as simple as needing to call revalidate and repaint on the BorderLayout-using JPanel after making your changes?
    I'm not familiar with those methods. Do I call them on the panel in the center layout, or for the entire screen? Is there a sequence to calling them? Anything else I might need to know about using them?

  12. #12
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I think what he's saying is that any time you add or remove a component from a container, you should make these changes and then call revalidate(); followed by repaint(); on the container (assuming it is derived from a JComponent such as a JPanel).

    So specifically, if you are removing the original JPanel from the containing JPanel's BorderLayout.CENTER position and then adding a new JPanel into the same position, you should call revalidate() followed by repaint() on the containing JPanel.

    revalidate() tells the JComponent's layout manager and any nested layout managers to re-lay out the components contained within it.
    repaint() queues a message to the the repaint manager that this Container needs to be repainted. This is not always needed but is almost always needed if a Component is removed and is not fully covered by a new Component.
    Last edited by curmudgeon; 07-13-2010 at 10:54 PM.

  13. #13
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    I tried it, unless I am doing something wrong, it is still not working. Does this look right?

    Java Code:
    	remove(monthPanel);
    	add(monthPanel, BorderLayout.CENTER);
    	monthPanel.revalidate();
    	monthPanel.repaint();

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you reduce the code to a small program that executes to demo your problem and post it here?

  15. #15
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    I will give it a shot.

  16. #16
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Quote Originally Posted by pahiker View Post
    I tried it, unless I am doing something wrong, it is still not working. Does this look right?

    Java Code:
    	remove(monthPanel);
    	add(monthPanel, BorderLayout.CENTER);
    	monthPanel.revalidate();
    	monthPanel.repaint();
    Nope, you've got to revalidate the parent:
    Java Code:
    	remove(monthPanel);
    	add(monthPanel, BorderLayout.CENTER); 
    	revalidate();
    	repaint();
    But you're adding the same panel(monthPanel), so you won't see a change anyway.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  17. #17
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    The revalidate wouldn't compile without the mathPanel, repaint did. In any case it didn't work.

    Here is the code, small as I can get it.

    Java Code:
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.io.*;
    import java.lang.Integer;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.border.BevelBorder;
    
    
    public class JCal
    {
    	public static void main (String args[])
    	{
    		displayMenu();
    	}
    
    
    	public static void displayMenu()
    	{
    		EventQueue.invokeLater (new Runnable()
    		{
    			public void run()
    			{
    				MainFrame frame = new MainFrame();
    				frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    	}
    }
    
    
    
    class MainFrame extends JFrame
    {
    	public MainFrame()
    	{
    		// Main window setup
    		setLocationByPlatform(true);
    		setSize(windowWidth, windowHeight);
    		setTitle("JCal - Julian Calendar");
    
    
    		// Create the month panel
    		CreateMonth (d, true);
    
    
    		// Listener for month changes
    		monthCombo.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent event)
    			{
    				d.set(Calendar.MONTH, monthCombo.getSelectedIndex());
    				CreateMonth (d, false);
    			}
    		});
    	}
    
    
    	private void CreateMonth (GregorianCalendar date, boolean redraw)
    	{
    		int curDate  = date.get(Calendar.DAY_OF_MONTH);
    		int curMonth = date.get(Calendar.MONTH);
    		int curYear  = date.get(Calendar.YEAR);
    
    	// Month and Year fields
    		for ( String month : months )
    			monthCombo.addItem(month);
    		monthCombo.setSelectedIndex(curMonth);
    
    
    	// add the Month and Years
    		if (redraw)
    		{
    		JPanel selectPanel = new JPanel();
    		selectPanel.setOpaque(true);
    		selectPanel.setBackground(new Color(153,255,153));
    		selectPanel.setLayout(new GridLayout(1,4));
    
    		selectPanel.add(monthCombo);
    		add(selectPanel, BorderLayout.NORTH);
    		}
    
    
    	// add the days
    		JPanel monthPanel = new JPanel();
    		remove(monthPanel);
    		monthPanel.setOpaque(true);
    		monthPanel.setBackground(new Color(153,255,153));
    		monthPanel.setLayout(new GridLayout(7,7));
    		BevelBorder bevel = new BevelBorder(BevelBorder.LOWERED);
    
    		for (String day : days)
    		{
    			JLabel temp = new JLabel(day, JLabel.CENTER);
    			temp.setBorder(bevel);
    			temp.setOpaque(true);
    			temp.setBackground(Color.YELLOW);
    			monthPanel.add(temp);
    		}
    
     
    	// Move days
    		GregorianCalendar gDate = new GregorianCalendar();
    		int curJulDay = 0;
    		if (gDate.isLeapYear(curYear))	curJulDay = leapJulDay[curMonth];
    		else							curJulDay = julianDay[curMonth];
    
    		int day = 1;
    		date.set(Calendar.DAY_OF_MONTH, 1);
    
    		for (int r = 0; r < 6; r++)
    		{
    			for (int c = 0; c < 7; c++)
    			{
    				JLabel temp = new JLabel("");
    
    				if (r == 0 && c < (date.get(Calendar.DAY_OF_WEEK) - 1))
    				{
    					monthPanel.add(temp);
    				}
    				else
    				{
    					if (day <= daysInMonth[curMonth])
    					{
    						temp = new JLabel("" + day + " / " + curJulDay++,  JLabel.CENTER);
    						temp.setBorder(bevel);
    						temp.setOpaque(true);
    
    						if (day == curDate)	temp.setBackground(new Color(000,255,255));
    						else				temp.setBackground(new Color(153,153,255));
    						day++;
    					}
    				}
    
    				monthPanel.add(temp);
    			}
    		}
    
    		add(monthPanel, BorderLayout.CENTER);
    		monthPanel.revalidate();
    		monthPanel.repaint();
    		date.set(Calendar.DAY_OF_MONTH, curDate);
    	}
    
    
    	/**
    		*---------------------------*
    		*   Application Variables   *
    		*---------------------------*
    	*/
    	static int windowWidth  = 500;
    	static int windowHeight = 300;
    	static GregorianCalendar d = new GregorianCalendar();
    
    	// Month arrays
    	String[] months   = { "January", "February", "March", "April", "May", "June",
    						  "July", "August", "September", "October", "November", "December" };
    	int[] daysInMonth = { 31, 28, 31, 30,  31,  30,  31,  31,  30,  31,  30,  31 };
    	int[] julianDay   = {  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
    	int[] leapJulDay  = {  0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
    
    	// Day array
    	String[] days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    
    	// Main panel
    	private JPanel jcalPanel     = new JPanel();
    
    	// Combo box fields
    	private JComboBox monthCombo = new JComboBox();
    }

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

    Default

    Problems:

    Java Code:
       private void CreateMonth(GregorianCalendar date, boolean redraw) {
          int curDate = date.get(Calendar.DAY_OF_MONTH);
          int curMonth = date.get(Calendar.MONTH);
          int curYear = date.get(Calendar.YEAR);
    
          // #1
          for (String month : months)
             monthCombo.addItem(month);
          monthCombo.setSelectedIndex(curMonth);
    
          /...
    
          // #2
          JPanel monthPanel = new JPanel();
          remove(monthPanel);
    #1) You're adding Strings to the JComboBox each time a new month is created. Take the creation of the JComboBox and the JPanel that holds it out of the createMonth method (note that methods should begin with lower case letters).

    #2) You're creating a new monthPanel JPanel object and then removing this very same object from the JFrame before it has ever been added to the JFrame, which effectively does nothing. Solution: create a monthPanel instance field and use that:

    Java Code:
    class MainFrame extends JFrame {
       JPanel monthPanel; // declare new Month Panel
       
       public MainFrame() {
          //... etc ...
    
       }
    
       private void CreateMonth(GregorianCalendar date, boolean redraw) {
          int curDate = date.get(Calendar.DAY_OF_MONTH);
          int curMonth = date.get(Calendar.MONTH);
          int curYear = date.get(Calendar.YEAR);
    
          if (monthPanel != null) {
             remove(monthPanel);         
          }
          monthPanel = new JPanel();
          monthPanel.setOpaque(true);
          monthPanel.setBackground(new Color(153, 255, 153));
          monthPanel.setLayout(new GridLayout(7, 7));
          //... etc ...

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

    Default

    Also: You might just want to pre-make your month JPanels and use a CardLayout to swap them. The tutorials (when they're up and working) can show you how.

  20. #20
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    That worked, thanks. I assume it was #2 that was the problem, can you explain what was wrong?

    Also, where are the tutorials? Other than the basic intro, I haven't seen any in-depth tutorials on the sun site.

Page 1 of 2 12 LastLast

Similar Threads

  1. Refreshing Jtable once again...
    By Norther in forum AWT / Swing
    Replies: 2
    Last Post: 06-29-2010, 08:46 PM
  2. Refreshing JList - need help
    By Unsub in forum New To Java
    Replies: 1
    Last Post: 04-13-2010, 07:31 AM
  3. JTableHeader not refreshing
    By aznboarder in forum AWT / Swing
    Replies: 4
    Last Post: 04-11-2009, 05:31 AM
  4. Why my JTree always collasped after refreshing?
    By lmsook10 in forum AWT / Swing
    Replies: 2
    Last Post: 06-24-2008, 06:55 AM
  5. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 08:09 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
  •