Thread: [Java-gnome-developer] [TreeView] How to know which DataColumn is used by a TreeViewColumn ?
Brought to you by:
afcowie
From: Mehdi R. <meh...@gm...> - 2006-06-06 02:58:45
|
Hi everybody, I'm trying to get the value of a cell after a double-click in a TreeView where the model is a ListStore. I have a class which implements TreeViewListener and from within it, the event parameter can give me the TreeViewColumn and a TreeIter, and even the TreeView (thus the ListStore). But to get a value with the ListStore.getValue() I also need the DataColumn behind the TreeViewColumn, and I don't know how to get it. So, which functions do I have to use ? Any help is appreciated, --- Mehdi |
From: Andrew C. <an...@op...> - 2006-06-06 06:56:43
|
On Mon, 2006-06-05 at 17:32 +0200, Mehdi Rabah wrote: > I'm trying to get the value of a cell after a double-click ^^^^^^^^^^^^^^^^^^^ For an activate, you just ask the event for the TreeIter that goes with it. We do something like this: view.addListener(new TreeViewListener() { public void treeViewEvent(TreeViewEvent event) { if (event.getType() == TreeViewEvent.Type.ROW_ACTIVATED) { TreeIter pointer = event.getTreeIter(); Blah b = (Blah) model.getValue(pointer, blahObject_DataColumn); /* do something with b */ } } }); To see this in context, refer to [1]. > But to get a value with the ListStore.getValue() I also need the > DataColumn behind the TreeViewColumn, and I don't know how to get it. The bad way to do it is to get ask the TreeViewEvent for the TreeModel and ask the TreeModel for the DataColumn[], and then for the 5th DataColumn, say dataColArry[4] or whatever. A much better way to do it is to have a field for each DataColumn as private members of your class. Then you've got all the DataColumns available by name, and you just use them. As with all of UI programming, variable naming pretty quickly becomes a problem. If you look at lines 61-119 of the above source file (or follow [2]), you'll see how we handle it. Oh - if for whatever reason TreeView{Listener,Event} isn't what you want, then see also TreeSelection{Listener,Event} AfC Sydney [1]: http://research.operationaldynamics.com/source/darcsweb/?r=objective;a=headblob;f=/src/accounts/ui/TransactionListView.java#l258 [2]: http://research.operationaldynamics.com/source/darcsweb/?r=objective;a=headblob;f=/src/accounts/ui/TransactionListView.java#l56 -- Andrew Frederick Cowie Technology strategy, managing change, establishing procedures, and executing successful upgrades to mission critical business infrastructure. http://www.operationaldynamics.com/ Sydney New York Toronto London |
From: Mehdi R. <meh...@gm...> - 2006-06-07 20:40:49
|
Hi everyone, I have seen that I didn't CC the mail to the list. Since I'm always stuck I'll appreciate any help ^^ ---------- Forwarded message ---------- From: Mehdi Rabah <meh...@gm...> Date: Jun 6, 2006 9:59 AM Subject: Re: [Java-gnome-developer] [TreeView] How to know which DataColumn is used by a TreeViewColumn ? To: Andrew Cowie <an...@op...> Hi Andrew, > view.addListener(new TreeViewListener() { > public void treeViewEvent(TreeViewEvent event) { > if (event.getType() == TreeViewEvent.Type.ROW_ACTIVATED) { > TreeIter pointer = event.getTreeIter(); > Blah b = (Blah) model.getValue(pointer, > blahObject_DataColumn); In fact I wanted to know how do you choose and get this blah_Object_DataColumn. The Listener that I use is in a class in a different package (it contains a lot of code). I can make the two class aware of each others and write getters, but... > > But to get a value with the ListStore.getValue() I also need the > > DataColumn behind the TreeViewColumn, and I don't know how to get it. > > The bad way to do it is to get ask the TreeViewEvent for the TreeModel > and ask the TreeModel for the DataColumn[], and then for the 5th > DataColumn, say dataColArry[4] or whatever. Which is the method to use to get this array? I didn't find it. A much better way to do it is to have a field for each DataColumn as > private members of your class. Then you've got all the DataColumns > available by name, and you just use them. > In fact my real problem is not to get the columns, it is to know which is the dataColumn behind the TreeViewColumn clicked (it's the title of my mail ^^) I want to know the cell (x,y) clicked - I have the TreeIter (x) and I want the DataColumn (y). I only have the TreeViewColumn (y but for view, not data), how to get the DataColumn behind this ?? |
From: Andrew C. <an...@op...> - 2006-06-10 05:37:34
|
On Wed, 2006-06-07 at 09:04 +0200, Mehdi Rabah wrote: > In fact I wanted to know how do you choose and get this > blah_Object_DataColumn. The Listener that I use is in a class in a > different package (it contains a lot of code). I can make the two > class aware of each others and write getters, but... ... or you could add a parameter to your concrete implementation of the Listener and pass the object in that way. Just a thought. > > Which is the method to use to get this array? I didn't find it. You could use TreeIter's getModel() give you the underlying TreeModel - your ListStore or TreeStore presumably. From there you could use ListStore.getDataColumn(int)... http://java-gnome.sourceforge.net/docs/javadoc/org/gnu/gtk/ListStore.html#getDataColumn(int) Sometimes people just keep a reference to the array they feed to ListStore's constructor around and use that. I prefer to keep references to the individual DataColumnWhatevers as it reduces the casting hassle. > In fact my real problem is not to get the columns, it is to know which > is the dataColumn behind the TreeViewColumn clicked (it's the title > of my mail ^^) I want to know the cell (x,y) clicked Ah. That's a bigger problem. GTK's TreeViews are not spreadsheets, although the alternating shading in both dimensions might give that impression. They're more like square datatables as you might find in an RDBMS - columns specify data types, and then rows enumerate the data itself. > - I have the TreeIter (x) and I want the DataColumn (y). > I only have the TreeViewColumn (y but for view, not data), how to get > the DataColumn behind this ?? As such, a click event does not "go" to a DataColumn, for two reasons. One is that the event goes to the entire row (the row is selected; the row is activated, whichever). Note that that's not "selects all the columns Ctrl-A style, but just that the row, as a single atomic object, is selected|activated. The other reason is that in an MVC paradigm (which the TreeView/TreeModel code implements), the "click" doesn't hit the DataColumns of the model. It hits a piece of the presented UI, ie in your TreeView widget. You then have to ask the event for an TreeIter which points you at which row of data in the underlying square TreeModel (ie ListStore) that event corresponds to. You then just read off whichever columns are of interest to you. AfC Sydney -- Andrew Frederick Cowie Managing Director Operational Dynamics Consulting Pty Ltd Australia: Office 02 9977 6866, Mobile 04 1079 6725 Management Consultants specializing in strategy, organizational architecture, procedures to survive change, and performance hardening for the people and systems behind the mission critical enterprise. http://www.operationaldynamics.com/ |