Thread: [Java-gnome-developer] DataColumns and adding TreeView rows
Brought to you by:
afcowie
From: Spyros S. <fou...@gm...> - 2005-10-27 12:59:06
|
Well, this may sound a little bit silly, however I have a little problem when it comes to adding a row. I do not know what to put as DataColumn in the setValue method. But let's take a look to the following piece of code: public class Whatever{ TreeView list; public Whatever(){ initTree(); ... ... fillTree(); } /*Initialize the tree by creating the columns, but NOT filling in the Data*= / private void initTree(){ list =3D (TreeView)glade.getWidget("listTreeView"); DataColumn[] dc =3D new DataColumn[4]; for(int i =3D 0; i < 4; i++) dc[i] =3D new DataColumnString(); ListStore ls =3D new ListStore(dc); tv.setModel(ls); TreeViewColumn col0 =3D new TreeViewColumn(); col0.setTitle("ONE"); CellRendererText render0 =3D new CellRendererText(); col0.setResizable(true); col0.packStart(render0, false); col0.addAttributeMapping(render0, org.gnu.gtk.CellRendererText.Attribute.TEXT , (DataColumnString)dc[0]); TreeViewColumn col1 =3D new TreeViewColumn(); col1.setTitle("TWO"); CellRendererText render1 =3D new CellRendererText(); col1.setResizable(true); col1.packStart(render1, false); col1.addAttributeMapping(render1, CellRendererText.Attribute.TEXT, (DataColumnString)dc[1]); TreeViewColumn col2 =3D new TreeViewColumn(); col2.setTitle("THREE"); CellRendererText render2 =3D new CellRendererText(); col2.setResizable(true); col2.packStart(render2, false); col2.addAttributeMapping(render2, CellRendererText.Attribute.TEXT, (DataColumnString)dc[2]); TreeViewColumn col3 =3D new TreeViewColumn(); col3.setTitle("FOUR"); CellRendererText render3 =3D new CellRendererText(); col3.setResizable(true); col3.packStart(render3, false); col3.addAttributeMapping(render3, CellRendererText.Attribute.TEXT, (DataColumnString)dc[3]); tv.appendColumn(col0); tv.appendColumn(col1); tv.appendColumn(col2); tv.appendColumn(col3); /* No further data is provided to the TreeView*/ } } private void fillTree(){ ListStore ls =3D (ListStore)(list.getModel()); TreeIter it =3D ls.appendRow(); ls.setValue(it, /*WHAT HERE???*/, "Test"); } } Well I was not able to find a way to get the DataColumn from the Model, as = I would when calling method ls.setValue(it, (DataColumnString)dc[0], "A String"); from within the method initTree. I mean, there is no dc to use in method fillTree(). I've read in the docs that this should be done through the Renderer, however I wasn't able to find a solution to this either. Is there a way to retrieve the DataColumn of the specified ListStore? /** * Sets a value in the data store. To display the data in the widget, you * need to associate the datablock with the renderer, using methods of the * {@link TreeViewColumn}. * @param iter A valid iterator which specifies the row in which the data * should be set. Iterators can be gained by using methods such as * {@link #appendRow()}. * @param dataBlock The data block to store the value in. * @param value The value to store. This must be of the same type * for the column as that set in the constructor to the ListStore. */ public void setValue( TreeIter iter, DataColumnString dataBlock, String value){...} Thnx everyone that might take a look in advance, Spyros "Foucault" Stathopoulos |
From: Sami W. <swa...@re...> - 2005-10-27 13:57:22
|
Hi Spyros, There is a very good tutorial on using TreeView on the java-gnome web page http://java-gnome.sourceforge.net/cgi-bin/bin/view/Main/TreeViewTutorial But from your email this is probably the most important part of the tutorial for you: Rather than creating an array of DataColumns, your code will be far easier to write if you create separate variables for each column: DataColumnBoolean ColIsChecked = new DataColumnBoolean(); DataColumnPixbuf ColFaceImage = new DataColumnPixbuf(); col3.addAttributeMapping(render3, CellRendererText.Attribute.TEXT, (DataColumnString)dc[3]); DataColumnString ColName = new DataColumnString(); ListStore ls = new ListStore( new DataColumn[] {ColIsChecked, ColFaceImage, ColName} ); Then later you can do this: ... ls.setValue(it, ColName , "Test"); ... > private void initTree(){ > list = (TreeView)glade.getWidget("listTreeView"); > > DataColumn[] dc = new DataColumn[4]; > for(int i = 0; i < 4; i++) col3.addAttributeMapping(render3, > CellRendererText.Attribute.TEXT, (DataColumnString)dc[3]); > dc[i] = new DataColumnString(); > > ListStore ls = new ListStore(dc); > tv.setModel(ls); This probably a mistake that happened when you were writing the email but shouldent it be list.setModel(ls) ? > * Sets a value in the data store. To display the data in the > widget, you > * need to associate the datablock with the renderer, using > methods of the > * {@link TreeViewColumn}. This just means calling addAttributeMaping which you already did here: col3.addAttributeMapping(render3, CellRendererText.Attribute.TEXT, (DataColumnString)dc[3]); I hope this helps :). Good luck, Sami Wagiaalla |
From: Spyros S. <fou...@gm...> - 2005-10-27 15:27:44
|
Thanks Sami, that was helpful indeed; it makes things much clearer. I' ll try to adjust it to my code, see how it works and mail back if smth goes wrong. This probably a mistake that happened when you were writing the email but > shouldent it be > list.setModel(ls) ? > > True. I' ve changed a bit the code to make it more "mail- compatible", bu= t I've forgotten to change tv to list. Anyway you seem to have gotten the point!. I've read the tutorial, but I must have missed a few things. I'm still trying to find out how the whole java- gnome thing works. Again... thanks. Spyros "Foucault" Stathopoulos |