Editing a Component using JTable

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
knrajwan@ncsu.edu
Posts: 1
Joined: Fri Oct 16, 2009 3:44 am
antibot: No, of course not.

Editing a Component using JTable

Post by knrajwan@ncsu.edu » Fri Oct 16, 2009 4:06 am

Hi,

My name is Kailash. I am currently enrolled for CSC 517 class at North Carolina State University. Myself and my team partner are working on JFreeChart and trying to implement below requirement in JFreeChart:

"Write an editing component that uses a JTable to display the values in a PieDataset and allows those values to be edited (added, deleted or modified).
• For a more challenging project, do the same for a CategoryDataset or an XYDataset."

Could you please provide more details in order for us to figure out what files we have to modify and it would be nice if the requirement is explained with an example?

We tried to execute PieChartDemo1.Java file and when we right click on the demo image and view Properties -> other, we see
Series Paint: No editor implemented (with Edit button disabled)
Series Stroke: No editor implemented (with Edit button disabled)
Series Outline Paint: No editor implemented (with Edit button disabled)
Series Outline Stroke: No editor implemented (with Edit button disabled)

Are we supposed to implement the above missing features? Or

Do we have to introduce a new button on any chart (say Edit), clicking on Edit would populate the dataset in JTable.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Editing a Component using JTable

Post by david.gilbert » Fri Oct 16, 2009 7:23 am

Hi Kailash,

The task isn't to integrate this directly into JFreeChart, just to provide a self-contained component that will allow you to pass in any PieDataset---the component will then display the data in a JTable---and allow full editing (modification of keys and values, addition and deletion of key, value pairs) with some validation to prevent duplicate keys and negative values. Ideally, this component should provide some notification mechanism so that a listener can be registered to get notified when the dataset has been modified. And you probably want to provide an option to cancel the editing and restore the original dataset, but that's up to you.

To demonstrate that your component works, you should create a simple Swing application that displays a dataset in a table (using your component) plus a chart somewhere else in the UI, and have the chart automatically updating when you edit the table.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 1:45 am

I am also working on the same project.
I have this issues currently.

The approach I decided was :
1. Accept column names and then make the table editable.
2. Convert the DefaultTableModel to DefaultPieDataset.
3. Draw the piechart for the pie dataset hence obtained.

Is this approach right?

I am able to do the first step.
I am not able to convert the DefaultTableModel to DefaultPieDataset.
I am able to draw the PieChart for some defaultly given PieDataset.

So, what is the method to convert the DefaultTableModel to DefaultPieDataset?

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 1:51 am

I am giving the default data for the DefaultPieDataset as follows:

Code: Select all

        final DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("ABC", new Double(12.3));
        result.setValue("DEF", new Double(4.5));

Using the above e.g., I am trying to convert the TableModel to PieDataset as follows:

Code: Select all

result.setValue((String)tabModel.getValueAt(i,0),Double.parseDouble((String)tabModel.getValueAt(i,1)));
Is this the right way?
But the above line of code works for 2 columns only.
How to work for multiple columns??

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: Editing a Component using JTable

Post by MitchBuell » Mon Oct 19, 2009 4:14 pm

What you described is one way, and would be how I would first try it. Basically, you're creating a user interface to edit a PieDataset. A table would be a natural choice, with some logic to populate the table and save the table values back to the dataset.

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 4:19 pm

private PieDataset createSampleDataset() {

final DefaultPieDataset result = new DefaultPieDataset();
for(int i=0;i<numrows;i++)
{
System.out.println("\n val 1 in Row: "+i+"="+tabModel.getValueAt(i,0));
System.out.println("\n val 2 in Row: "+i+"="+tabModel.getValueAt(i,1));

result.setValue((String)tabModel.getValueAt(i,0),(Double)tabModel.getValueAt(i,1));

}

return result;
}

Considering only 2 columns, how do i pass values in the result.setValue function?
Is that way right?
When I do this there is no Piedataset being created!.

Can you find the flaw in this code?

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: Editing a Component using JTable

Post by MitchBuell » Mon Oct 19, 2009 4:30 pm

I don't see what's wrong with that code. Do you have an Exception or stack trace?

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 4:31 pm

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at Java4.createSampleDataset(Java4.java:84)
at Java4.drawChart(Java4.java:231)
at Java4.actionPerformed(Java4.java:165)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 4:34 pm

private PieDataset createSampleDataset() {

final DefaultPieDataset result = new DefaultPieDataset();
for(int i=0;i<numrows;i++)
{
System.out.println("\n val 1 in Row: "+i+"="+tabModel.getValueAt(i,0));
System.out.println("\n val 2 in Row: "+i+"="+tabModel.getValueAt(i,1));

try{
result.setValue((String)tabModel.getValueAt(i,0),(Double)tabModel.getValueAt(i,1));}
catch(Exception e)
{
System.out.println("\n Creation of sample dataset.. exception "+e);
}

}

return result;

}

for this piece of code.. I caught an exception..

Creation of sample dataset.. exception java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 4:37 pm

result.setValue((String)tabModel.getValueAt(i,0),(String)tabModel.getValueAt(i,1));}

If I give the result.setValue thing as such, I am not able to compile the program.

The following error is shown:
Java4.java:85: cannot find symbol
symbol : method setValue(java.lang.String,java.lang.String)
location: class org.jfree.data.general.DefaultPieDataset
result.setValue((String)tabModel.getValueAt(i,0),(String)tabModel.getValueAt(i,1));}
^

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: Editing a Component using JTable

Post by MitchBuell » Mon Oct 19, 2009 4:41 pm

The exception is "java.lang.String cannot be cast to java.lang.Double" and the only place that I can see where you are trying to cast a String to a Double is "(Double)tabModel.getValueAt(i,1)". You need to look at exactly what tabModel.getValueAt(i,1) is returning. According to the Exception, getValueAt is returning a String. To convert a String to a Double, you need to do Double.valueOf(tabModel.getValueAt(i,1))

The setValue method takes a String and a Number, not a String and a String. You want: result.setValue((String)tabModel.getValueAt(i,0),Double.valueOf(tabModel.getValueAt(i,1)));

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 4:46 pm

Code: Select all

				result.setValue((String)tabModel.getValueAt(i,0),Double.ValueOf(tabModel.getValueAt(i,1))); 
I did this.

But still Ive been getting the following error:

Code: Select all

Java4.java:86: cannot find symbol
symbol  : method ValueOf(java.lang.Object)
location: class java.lang.Double
                        result.setValue((String)tabModel.getValueAt(i,0),Double.ValueOf(tabModel.getValueAt(i,1)));
                                                                               ^

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: Editing a Component using JTable

Post by MitchBuell » Mon Oct 19, 2009 4:56 pm

You have Double.ValueOf, but the method is Double.valueOf. You have the V in valueOf capitalized.

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: Editing a Component using JTable

Post by vindsr2211 » Mon Oct 19, 2009 5:08 pm

Inspite of the editing also, the error remains at the same place.
Java4.java:86: cannot find symbol
symbol : method valueOf(java.lang.Object)
location: class java.lang.Double
result.setValue((String)tabModel.getValueAt(i,0),Double.valueOf(tabModel.getValueAt(i,1)));
^

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Editing a Component using JTable

Post by skunk » Mon Oct 19, 2009 5:54 pm

Try

Code: Select all

Double.valueOf((String)tabModel.getValueAt(i,1))

Locked