HELP: SWTException: an Invalid Access Error: how to solve?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jinrf
Posts: 6
Joined: Thu Mar 22, 2007 10:28 am
Location: Beijing, China

HELP: SWTException: an Invalid Access Error: how to solve?

Post by jinrf » Wed Mar 28, 2007 4:13 am

HI everyone,

I'm facing a tough problem. I am using JFreechart in Eclipse RCP to write a system resource monitor. I implemented a class DatGenerator as follow:

Code: Select all

public class DataGenerator extends Timer implements ActionListener{
	private static final long serialVersionUID = 3977867288743720505L;
	
	public DataGenerator(int interval){
		super(interval,null);
		System.out.println("in DataGenerator constructor");
		addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent event){
		long f=Runtime.getRuntime().freeMemory();
		long t=Runtime.getRuntime().totalMemory();
		System.out.println("freeMem: " + f + "\t\tTotalMem: " + t);
		MemChart.addTotalObservation(t);
		MemChart.addFreeObservation(f);
	}
}

Code: Select all

public class MemChart {
	static JFreeChart chart;	
	static final int HISTORY_COUNT = 30000;	
	static TimeSeriesCollection dataset;
	static TimeSeries total = new TimeSeries("TotalMemory",Millisecond.class);
	
	static TimeSeries free = new TimeSeries("FreeMemory",Millisecond.class);
	
	
	public static void createDataset(){
		free.setMaximumItemCount(HISTORY_COUNT);
		total.setMaximumItemCount(HISTORY_COUNT);
		dataset = new TimeSeriesCollection();
		dataset.addSeries(total);
		dataset.addSeries(free);
	}

	public static JFreeChart createChart(){
		createDataset();		
		DateAxis domain = new DateAxis("Time");
		NumberAxis range = new NumberAxis("Memory");
		domain.setTickLabelFont(new Font("SansSerif", Font.PLAIN,12));
		range.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
		domain.setLabelFont(new Font("SansSerif",Font.PLAIN,14));
		range.setLabelFont(new Font("SansSerif",Font.PLAIN,14));
		
		XYItemRenderer renderer = new XYLineAndShapeRenderer(true,false);
		renderer.setSeriesPaint(0,Color.red);
		renderer.setSeriesPaint(1,Color.green);
		renderer.setStroke(new BasicStroke(3f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)
		);
		XYPlot plot = new XYPlot(dataset,domain,range,renderer);
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0,5.0,5.0,5.0));
		domain.setAutoRange(true);
		domain.setLowerMargin(0.0);
		domain.setUpperMargin(0.0);
		domain.setTickLabelsVisible(true);
		range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		chart = new JFreeChart(
			"JVM Memory Usage", 
			new Font("SansSerif", Font.BOLD, 24),
			plot, 
			true
		);		
		chart.setBackgroundPaint(Color.white);
		return chart;
		
	}
	
	public static void addTotalObservation(final double y){
		System.out.println("in addTotalObservation");
		total.add(new Millisecond(),y);	
	}
	
	public static void addFreeObservation(final double y){
		System.out.println("in addFreeObservation");
		free.add(new Millisecond(),y);
	}	
}
but when run it, I get an SWTException: Invalid thread access error, I think it is caused by accessing the UI thread from outside.
so i tried to add this:

Code: Select all

Display.getCurrent().syncExec(new Runnable(){
			public void run(){
				free.add(new Millisecond(),y);
			}
		});		
But still dont work, can anyone help me ? PLEASE...
P.R.China, now working with the Grid Project(http://www.crown.org.cn/en/).

silent
Posts: 50
Joined: Wed Mar 16, 2005 2:55 pm
Location: Korea
Contact:

Post by silent » Wed Mar 28, 2007 5:56 am

Hi,

try this.

Code: Select all

SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				add(item);
			}
		});
Tokdo belongs to Korea.

Not a 'sea of japan', But a 'East Sea(Dong hae)'

jinrf
Posts: 6
Joined: Thu Mar 22, 2007 10:28 am
Location: Beijing, China

Post by jinrf » Wed Mar 28, 2007 3:29 pm

Thx, bro! now it's running but still throw exceptions like this:
silent wrote:Hi,

try this.

Code: Select all

SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				add(item);
			}
		});

Code: Select all

Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Widget is disposed
	at org.eclipse.swt.SWT.error(SWT.java:3374)
	at org.eclipse.swt.SWT.error(SWT.java:3297)
	at org.eclipse.swt.SWT.error(SWT.java:3268)
	at org.eclipse.swt.widgets.Widget.error(Widget.java:435)
	at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:330)
	at org.eclipse.swt.widgets.Control.redraw(Control.java:1612)
	at org.jfree.experimental.chart.swt.ChartComposite.chartChanged(ChartComposite.java:1090)
	at org.jfree.chart.JFreeChart.notifyListeners(JFreeChart.java:1319)
	at org.jfree.chart.JFreeChart.plotChanged(JFreeChart.java:1381)
	at org.jfree.chart.plot.Plot.notifyListeners(Plot.java:851)
	at org.jfree.chart.plot.XYPlot.datasetChanged(XYPlot.java:3538)
	at org.jfree.data.general.AbstractDataset.notifyListeners(AbstractDataset.java:173)
	at org.jfree.data.general.AbstractDataset.fireDatasetChanged(AbstractDataset.java:159)
	at org.jfree.data.general.AbstractSeriesDataset.seriesChanged(AbstractSeriesDataset.java:114)
	at org.jfree.data.general.Series.notifyListeners(Series.java:310)
	at org.jfree.data.general.Series.fireSeriesChanged(Series.java:295)
	at org.jfree.data.time.TimeSeries.add(TimeSeries.java:543)
	at org.jfree.data.time.TimeSeries.add(TimeSeries.java:572)
	at org.jfree.data.time.TimeSeries.add(TimeSeries.java:558)
	at cn.org.crown.mymeter.charts.MemChart$2.run(MemChart.java:100)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
is there anything still go wrong? HELP...
P.R.China, now working with the Grid Project(http://www.crown.org.cn/en/).

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by Taqua » Wed Mar 28, 2007 4:56 pm

Hi,

the SWT does not use the AWT-Event dispatcher, so the 'Invoke-later' method will fail. I'd say: Go ahead, grab some SWT documentation and start reading.

(Don't ask me details about SWT programming, I prefer solutions that are 100% Java.)

Have fun,
said Thomas

silent
Posts: 50
Joined: Wed Mar 16, 2005 2:55 pm
Location: Korea
Contact:

Post by silent » Thu Mar 29, 2007 2:45 am

jinrf wrote:Thx, bro! now it's running but still throw exceptions like this:

Code: Select all

Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Widget is disposed
	at org.eclipse.swt.SWT.error(SWT.java:3374)
	at org.eclipse.swt.SWT.error(SWT.java:3297)
	at org.eclipse.swt.SWT.error(SWT.java:3268)
	at org.eclipse.swt.widgets.Widget.error(Widget.java:435)
	at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:330)
	at org.eclipse.swt.widgets.Control.redraw(Control.java:1612)
	at org.jfree.experimental.chart.swt.ChartComposite.chartChanged(ChartComposite.java:1090)
	at org.jfree.chart.JFreeChart.notifyListeners(JFreeChart.java:1319)
	at org.jfree.chart.JFreeChart.plotChanged(JFreeChart.java:1381)
	at org.jfree.chart.plot.Plot.notifyListeners(Plot.java:851)
	at org.jfree.chart.plot.XYPlot.datasetChanged(XYPlot.java:3538)
	at org.jfree.data.general.AbstractDataset.notifyListeners(AbstractDataset.java:173)
	at org.jfree.data.general.AbstractDataset.fireDatasetChanged(AbstractDataset.java:159)
	at org.jfree.data.general.AbstractSeriesDataset.seriesChanged(AbstractSeriesDataset.java:114)
	at org.jfree.data.general.Series.notifyListeners(Series.java:310)
	at org.jfree.data.general.Series.fireSeriesChanged(Series.java:295)
	at org.jfree.data.time.TimeSeries.add(TimeSeries.java:543)
	at org.jfree.data.time.TimeSeries.add(TimeSeries.java:572)
	at org.jfree.data.time.TimeSeries.add(TimeSeries.java:558)
	at cn.org.crown.mymeter.charts.MemChart$2.run(MemChart.java:100)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
is there anything still go wrong? HELP...
At first, you wrote the original jfreechart's sample code.
But, above exception is for SWT jfreechart (experiment).
I cannot understand what is your correct code.

If you use SWT jfreechart then the 'Invoke-later' method are not necessary. Can I see your code? and What is your eclipse version?
Tokdo belongs to Korea.

Not a 'sea of japan', But a 'East Sea(Dong hae)'

jinrf
Posts: 6
Joined: Thu Mar 22, 2007 10:28 am
Location: Beijing, China

Post by jinrf » Thu Mar 29, 2007 5:12 am

silent wrote: At first, you wrote the original jfreechart's sample code.
But, above exception is for SWT jfreechart (experiment).
I cannot understand what is your correct code.

If you use SWT jfreechart then the 'Invoke-later' method are not necessary. Can I see your code? and What is your eclipse version?
In fact, I'm trying to change the original jfreechart's sample code into SWT to work with RCP, so i did some combining work. i changed the code into using the experimental CharComposite

so comes the problems..

and also i used your asyncExec and syncExec method, but seems doesnt work. do u have some example code for this ?
P.R.China, now working with the Grid Project(http://www.crown.org.cn/en/).

silent
Posts: 50
Joined: Wed Mar 16, 2005 2:55 pm
Location: Korea
Contact:

Post by silent » Fri Mar 30, 2007 5:35 am

Tokdo belongs to Korea.

Not a 'sea of japan', But a 'East Sea(Dong hae)'

rspan
Posts: 15
Joined: Wed May 02, 2007 4:36 pm

Post by rspan » Wed May 09, 2007 9:29 pm

Jinrf,

Do you have the fix on this problem? I got the same problem.

Thanks

Locked