how to identify JComboBox whether is disabled or enable inside its renderer class - swing

I have a JComboBox contains following iems
{ "select" , "one" , "two" }
i need a separate background for first item so ,
i have made a condition in it's renderer like
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null){
Item item = (Item)value;
setText(item.getDescription());
}
if(index==-1){
setBackground(new Color(199,209,210));
setForeground(Color.BLACK);
}
return this;
}
}
so my question is if we disable a JComboBox, i have to made -1th index of component background color to someother color
like
if(index==-1){
setBackground(Color.RED);
}
please advice

The simplest way is always the best. Since you assigning renderer to a combobox, why don't you pass the combobox into it? Just create a custom renderer that holds the reference to a combobox then use the stored reference inside of your getListCellRendererComponent method

Maybe I'm missing something here, but if a JComboBox is disabled, then that means that it cannot popup its list of items. But if it is not showing its list of items, then that means it is not using the renderer for that list.
So, why do you need to get a reference to the combobox in the renderer?

Related

Unable to select the aria-activedescendant combobox element in java

Im finding it hard to select the items from the combobox element. This combobox is something different. When i drag down the items, i could see the actual value of the item(i.e. aria-activedescendant="react-select-4--option-3"). After selecting the particular item, the value has got changed to "aria-activedescendant="react-select-4--value"". Plz suggest me the best solution to click the item. You have to specifically note this "aria-activedescendant="react-select-4--value"" in input section under html code.
Tried with below code. I was able to just click the combo box element but not the value. I used id="react-select-4--value" to click the combo box, It errors as "element not clickable at point". Technically the system understands something wrong.
public void ScrollAndClickOnElement(String loacator,String LocatorValue)
{
WebElement element = null;
if(loacator.equalsIgnoreCase("cssSelector"))
element = driver.findElement(By.cssSelector(LocatorValue));
else if(loacator.equalsIgnoreCase("xpath"))
element = driver.findElement(By.xpath(LocatorValue));
else if(loacator.equalsIgnoreCase("id"))
element = driver.findElement(By.id(LocatorValue));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);",element);
element.click();
}
public void WaitAndClickElement(String locatorType,String locatorVaue)
{
WebDriverWait wait = new WebDriverWait(driver,50);
if(locatorType.equalsIgnoreCase("cssSelector"))
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(locatorVaue))).click();
else if(locatorType.equalsIgnoreCase("xpath"))
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(locatorVaue))).click();
else if(locatorType.equalsIgnoreCase("id"))
wait.until(ExpectedConditions.elementToBeClickable(By.id(locatorVaue))).click();
}
ScrollAndClickOnElement("id", AssessmentPageData.ElevatorManufacturer);
WaitAndClickElement("cssSelector",AssessmentPageData.Appollo);
Referred to this link and used 'select' method, but it errors as "Element should have been "select" but was "span""
Found the below solution finally,
public void SelectBootStrap(String textval){
WebElement listElement = driver.findElement(By.xpath("//div[#class='Select-menu-outer']//div[text()='"+textval+"']"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);",listElement);
listElement.click();
}

Change data template when the user selects it

I have two datatemplates. One is the default and the other one is for when the user selects this item. I need to give the selected item double width and height of the normal template. How can I do this?
What you want to do is not difficult, but it is not solved by swapping the data template. Instead, it is accomplished by using Visual States in XAML. A Visual State allows you to create multiple "views" of your XAML (for example, what it looks like when it is selected and when it is not selected) and to switch between those easily. Swapping data templates is a big deal, Mostafa, and can result in your UI flickering because the underlying subsystem has to re-render so many parts of the visual tree.
If you want to learn more about the Visual States, you might read over the blog article I wrote on the same subject.
http://blog.jerrynixon.com/2013/11/windows-81-how-to-use-visual-states-in.html
The only problem now is to figure out how to trigger the visual state when the item in a gridview or listview is selected. First, you should know that IsSelected is a property on the gridviewitem or listviewitem control that houses your item. However, it's tricky to reach that property and the most common approach is to sub-class your gridview/listview and override PrepareContainerForItemOverride and set the binding in code-behind.
Like this:
class MyModel
{
public bool IsSelected { get; set; }
}
class MyList : Windows.UI.Xaml.Controls.ListView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
var model = item as MyModel;
var listViewItem = element as Windows.UI.Xaml.Controls.ListViewItem;
var binding = new Windows.UI.Xaml.Data.Binding
{
Source = model,
Mode = Windows.UI.Xaml.Data.BindingMode.TwoWay,
Path = new PropertyPath(nameof(model.IsSelected)),
};
listViewItem.SetBinding(Windows.UI.Xaml.Controls.ListViewItem.IsSelectedProperty, binding);
base.PrepareContainerForItemOverride(element, item);
}
}
I hope this helps.
Best of luck!

how to set checkbox on jtable?

I set the checkbox to the jtable using default model, by using following code:
Object[] ColumnData = {"Sr No","Ward Name","Total voters","Action"};
Object[][] RawData=null;
//loop
model.insertRow(x, new Object[]{ key,ward_name_var,total_vot_var,new Object[]{o}});
model.setValueAt(o,x,3);
tblWard.setModel(model);
Setchk(tblWard,3,checkbox);// by calling this method which refers renderer
//set renderer each time when i fill the rows by using database
private void Setchk(JTable jTable1, int i, JCheckBox checkbox)
{
jTable1.getColumnModel().getColumn(i).setCellRenderer((new CWCheckBoxRenderer()));
jTable1.getColumnModel().getColumn(i).setCellEditor(new CheckBoxCellEditor());
}
But my database having large number of records, when I click on any checkbox it runs
to check state of each and every checkbox contain in same column.
It can decrease performance of mine system so provide me solution for how I
avoid situation so it will not go to check the state of all checkboxes.
Please suggest a book or link to understand the Renderer property of jtable.

Flex List custom color for rows

Is it possible to set custom color for rows in a flex list depending on the data in it.I know how to define a custom itemrenderer and do it but I would likr to know whether there is any simple method because I don't want more components in the list
It is not possible to add custom colors to the itemRenderer that displays your data without modifying, or replacing, the itemRenderer.
The list based classes in Flex use itemRenderers to display data. If you create your own, then you are replacing the default itemRenderer. It will not create any more components in memory; it'll just change what those components are.
you can create your own custom list class and by overrideing the protected drawRowBackground method .... you might achive this....
protected override function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void {
if (rowColoringFunction != null && IList(dataProvider).length > dataIndex) {
color = rowColoringFunction(IList(dataProvider).getItemAt(dataIndex), dataIndex, color);
}
super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
}
try and let me know...

AS3: How to know if dataprovider or its content(s) is changed

I'm implementing some kind of combobox control (by extending spark.components.supportClasses.DropDownListBase)
Now, inside this control; I need to know:
if the dataprovider is changed/assigned. (which I can do... the first approach below works);
if any item in the dataprovider collection has changed.
I tried 2 methods that did not do the trick...
1ST APPROACH:
[Bindable("collectionChange")]
override public function set dataProvider(value:IList):void
{
if (value) value.addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);
super.dataProvider = value;
trace("DATA CHANGED"); //fires
}
protected function onDataChange(event:CollectionEvent):void
{
trace("COLLECTION ITEM(S) CHANGED"); //does not fire
}
2ND APPROACH:
Since this is based on DropDownListBase; it should dispatch the CollectionEvent.COLLECTION_CHANGE event already..?
public function myClass() //constructor
{
addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);
}
protected function onDataChange(event:CollectionEvent):void
{
trace("DATA CHANGED"); //does not fire
}
Any ideas?
UPDATE: Edited above.. The first approach lets me know if the dataprovider is changed but not if any item is updated in the dataprovider collection. The second approach does not work at all..
We'll be able to help significantly more if you show us a runnable sample to demonstrate the problem.
if the dataprovider is
changed/assigned.
Your first approach should work. Can you tell us what makes you think it didn't? ( No trace statement I assume? ). And tell us what you did to change the dataProvider.
The second approach won't work because myClass is not firing off the collectionChange event.
2 if any item in the dataprovider collection has changed.
There is not really a way to tell this. In most cases, a collection is just a list of pointers to other objects. If you change those pointers, then a collectionChange event is fired. IF you change the item that he pointer is pointed to, the collection has no way to know that something changed. Binding works very similarly if your an MXML fan.
If you have control over how items are changed, you can deal with it that way. Instead of:
(collection.getITemAt(x) as myObject).property = newValue;
Do something like this:
var myObject : MyObject = collection.getITemAt(x) as myObject
myObject.property = newValue;
collection.setItemAt(x, myObject);
I would expect that to fire a collectionChange event, but not the former.
That said, in the context of a dropDownListBase: As you scroll or open and close the drop down, the itemRenderers should be updated to reflect the most current dataProvider's data. But if you change something on the fly while the drop down is open, I would not expect it to update automatically [if you're not changing the dataProvider.