Results 1 to 20 of 24
Thread: JPanel not refreshing
- 07-13-2010, 01:56 PM #1
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); }
-
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!
- 07-13-2010, 05:00 PM #3
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.
- 07-13-2010, 05:02 PM #4
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-13-2010, 06:14 PM #5
Thanks, Ph, but that takes me to a download.oracle.com page which opens blank.
- 07-13-2010, 06:28 PM #6
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 06:31 PM.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-13-2010, 06:37 PM #7
Works for me. I get to the site ok.
- 07-13-2010, 06:44 PM #8
- 07-13-2010, 07:54 PM #9
To me FF displays a blank window, IE displays:
Can anyone post the relevant content for me, or explain what needs done?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.
- 07-13-2010, 08:50 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 07-13-2010, 09:49 PM #11
So, can someone explain this to me?
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?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?
- 07-13-2010, 09:52 PM #12
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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 09:54 PM.
- 07-13-2010, 10:28 PM #13
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();
- 07-13-2010, 10:32 PM #14
Can you reduce the code to a small program that executes to demo your problem and post it here?
- 07-13-2010, 10:37 PM #15
I will give it a shot.
- 07-13-2010, 10:40 PM #16
- 07-13-2010, 11:00 PM #17
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(); }
-
Problems:
#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).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);
#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 ...
-
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.
- 07-14-2010, 01:36 AM #20
Similar Threads
-
Refreshing Jtable once again...
By Norther in forum AWT / SwingReplies: 2Last Post: 06-29-2010, 07:46 PM -
Refreshing JList - need help
By Unsub in forum New To JavaReplies: 1Last Post: 04-13-2010, 06:31 AM -
JTableHeader not refreshing
By aznboarder in forum AWT / SwingReplies: 4Last Post: 04-11-2009, 04:31 AM -
Why my JTree always collasped after refreshing?
By lmsook10 in forum AWT / SwingReplies: 2Last Post: 06-24-2008, 05:55 AM -
Bug in refreshing jsp
By anki1234 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-31-2007, 07:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks