Results 1 to 6 of 6
  1. #1
    elliot is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default JFrame refresh problem

    Hi

    I have been coding Java for a couple of days, having come from a C background.

    I have an application that displays a JFrame with a JFreeChart object held in the centre and three buttons in the south layout.

    I have registered action listeners to the button, and the idea is to re draw the graph with a moving average series.

    I can see my callback, but am unable to redraw the chart. What do I need to do to get the frame to redraw?

    thanks

    elliot

    Java Code:
    public void graphData() {
    		String[] chartInfo = { "Gold Price USD", "Date /(Yr/)", "Price /(USD/)" };
    		JButton monthBtn = new JButton("M");
    		JButton sixMBtn = new JButton("6M");
    		JButton yearBtn = new JButton("1Y");
    	
    		final TimeSeriesChart tsc = new TimeSeriesChart("Gold Price (USD)", "Date (Yr)", "Price (USD)");
    		final int[] yIntArr = new int[ySeries.length];
    		
    		for (int i = 0; i < ySeries.length; i++) {
    			yIntArr[i] = (int) ySeries[i];
    		}	    
    
    		final JFrame graphFrame = new JFrame();
    		BorderLayout bl = new BorderLayout();		
    		
    		graphFrame.setLayout(bl);
    		graphFrame.add(tsc.createPanel(xSeries, yIntArr, "null"), BorderLayout.CENTER);
    		
    		JPanel southPanel = new JPanel();
    		monthBtn.addActionListener(new ActionListener (){
    				public void actionPerformed(ActionEvent e){
    					graphFrame.add(tsc.createPanel(xSeries, yIntArr, "month"), BorderLayout.CENTER);
    					graphFrame.repaint();
    				}
    			});
    		
    		sixMBtn.addActionListener( new ActionListener (){
    			public void actionPerformed(ActionEvent e){
    				graphFrame.add(tsc.createPanel(xSeries, yIntArr, "sixmonth"), BorderLayout.CENTER);
    				graphFrame.repaint();
    			}
    		});
    		
    		yearBtn.addActionListener( new ActionListener (){
    			public void actionPerformed(ActionEvent e){
    				graphFrame.add(tsc.createPanel(xSeries, yIntArr, "year"), BorderLayout.CENTER);
    				graphFrame.repaint();
    				System.out.println("here.......");
    			}
    		});
    		
    		southPanel.add(monthBtn);
    		southPanel.add(sixMBtn);
    		southPanel.add(yearBtn);
    		
    		graphFrame.add(southPanel, BorderLayout.SOUTH);
    		graphFrame.setVisible(true);
    		
    		graphFrame.pack();
    	}

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

    Default

    If you add a new component, you will need to call revalidate on the the JPanel/container and sometimes repaint (as you are already doing).

  3. #3
    elliot is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    Many thanks :-)

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

    Default

    Quote Originally Posted by elliot View Post
    Many thanks :-)
    You're quite welcome. Oh, and if your question is answered, please mark it "Answered".

    And finally, welcome to our forum!

  5. #5
    elliot is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks... how do I show as solved? I could not edit the title.

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

    Default

    Quote Originally Posted by elliot View Post
    Thanks... how do I show as solved? I could not edit the title.
    Click on "Thread Tools" above. :)

Similar Threads

  1. Problem-----JFrame
    By HariPrasad in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2009, 12:34 PM
  2. GUI refresh problem in java 1.6
    By gokhan in forum Java Applets
    Replies: 2
    Last Post: 09-26-2008, 12:23 PM
  3. My Simple Refresh Problem
    By pmcastillo in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 07:59 AM
  4. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 02:53 AM
  5. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 05:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •