I have a quick question about rendering the advanceddatagrid cells.
I need to programatically color the cell of the datagrid based on the conditions. Lets say, the stock quotes. If there is an increase from the previous day, I need to have the cell colored in GREEN and in RED, when there is a decrease.
Now, the important part here is, I need to do these things dynamically, which means, when the user enables the comparison/conditions, then the cells are colored. And when the user disables the comparison, then it again goes back to its default behavior.
I know I have to use renderers. But not sure, how to use it for the cells and that too dynamically. Can anyone please explain how to go for it?
Thanks
Item renderers are components used to define the appearance of a component's "items" or subcomponents. In the case of the ADG, the "items" are the individual cells. You can create a completely custom class to function as the renderer (given it implements certain required interfaces) or, in most cases, you extend an existing component. Since the default renderer for ADG cells doesn't support background colors, you have to create or extend a component that does and use that as the renderer. That is the basic premise that these tutorials, linked to in the following question, work from:
Setting background color for datagrid row in Adobe Flex
After creating an itemRenderer that supports a background color, you have two options as to where you can define your "conditions"; inside of the itemRenderer or using the ADG's styleFunction (additionally requiring that your itemRenderer defines a "background" style).
In your case, you could include both today's and yesterday's stock price values in the data sent to each cell and compare the two to determine the color used to draw the background. Again, more on that in the tutorial links provided above. In either the itemRenderer or the styleFunction, you would compare properties on the itemRenderer's/styleFunction's data object (corresponding to the row you're looking at), e.g.:
if(data.today > data.yesterday)
{
// set color or return style
}
else ...
To "toggle" custom cell colors, switch between your custom renderer and the default (colorless) renderer. In other words, set the itemRenderer property to your custom itemRenderer class when you need display the colors and set it to "null" when you want the "default behavior".
Related
I am new to JavaFX and I don't know how to set cell styles dynamically in a TreeTableView.
For example, I wish to set a cell's background color on the fly based on user input. The user may specify a range (e.g. dark red to light red). I would calculate the appropriate value for a specific cell and then set the background color based on the calculation result.
I am familiar with JTable's approach of writing a cell renderer but JavaFX appears to handle things quite differently.
How to Set alternat Row Color In Flex 4.5 MobileApplication For Spark Data Grid. The Main Problem Of SparkDataGrid in Mobile Application not Allowed to Add Skin.
Use the alternatingRowColors style. To quote the docs:
Type: Array Format: Color CSS Inheritance: no Theme: spark
Used to initialize the DataGrid's rowBackground skin part. If the
alternatingRowColors style is specified, then use the
alternatingRowColorsBackground skin part as the value of the
rowBackground skin part. The alternating colors for the grid rows are
defined by successive entries in the Array value of this style.
If you want to change how this style is rendered, replace the
alternatingRowColorsBackground skin part in the DataGridSkin class. If
you want to specify the background for each row, then initialize the
rowBackground skin part directly.
The default value is undefined.
Add the following style to your application:
s|DataGrid
{
alternatingRowColors:#FFFFFF,#CCCCCC;
}
This will make the odd rows white and the even rows gray. Now, I have not tested this using Mobile so it may or may not work. Please follow up with results.
-Vic
Using Qt 4.6.3 on Linux/X11.
I have a QTreeView widget which uses a QStandardItemModel as its model, with 4 columns and hundreds of rows. Most of the items in the list are to be displayed with a standard color, but a few need to be of a different color. I can change the colors of those few items easily with QStandardItem::setForeground().
However, that only affects the color of the item when it is not selected. When I select a colored item, its background color changes to blue (which is ok), and the text color changes to white (which is not ok). I tried using a stylesheet to affect the foreground color of selected items (with selector QTreeView::item:selected), but it affects all items.
I would like items for which I called item->setForeground(Qt::red) to remain red even when they are selected, and other items to use the default set of colors (which they already do). How can that be done?
The colors being used are (I assume) those for the QPalette's Hightlight and HighlightedText roles. Unfortunately, I don't know of any way to set those on an individual standard item.
However, since standard items are used in the model/view framework, you have another option. You should be able to create a delegate to paint the view however you want to. I would recommend inheriting from the styled delegate, and calling the parent class's functionality as much as possible. Likely, you'll only need to change a few parameters in the cases where an item is selected and has a non-standard foreground color.
How to skin spark List component so that every adjacent itemRenderer has different color (something like mx DataGrid)?
You can do this by creating a custom renderer derived from ItemRenderer and overriding the itemIndex setter. In your overridden method, set the background color based on whether the item index is odd or even. The Adobe docs have an example of this which you can find in this section:
http://help.adobe.com/en_US/flex/using/WS03d33b8076db57b9-23c04461124bbeca597-8000.html#WS03d33b8076db57b9-43c4a246124bc002543-8000
Hope that helps.
I have datagrid in my page and I want to change particular cell background color. I don't want to use itemrenderer. Is there any alternate ?
Here is an example of changing the selection color: Flex Examples
If you want to do more than that, I'm afraid you will have to use an itemRenderer.
You could subclass DataGrid and override the protected drawRowBackground() method or possibly drawColumnBackground(). These are intended to draw the background for an entire row or column but you may be able to get it to draw a special color only for the cell(s) you want.