I want to selected the first item of a comboBox by code if the value list have only one item. I've tried this:
Comboitem item = new Comboitem();
for(Empresa e : empresasList){
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
item.setParent(cb_empresa);
}
if(empresasList.size()==1){
idEmpresa = empresasList.get(0).getEmpId();
//cb_empresa.setSelectedIndex(0);
cb_empresa.setSelectedItem(item);
}
But it does not work. I also tried the commented line:
//cb_empresa.setSelectedIndex(0);
Any help?
Thanks!
I don't know whether the item.setParent(cb_empresa); has an immediately effect to the view.
You can try setting the index later, or after refresh the cb_empresa.
First of all, your code is buggy. you have to create as many Comboitems as empresasList.size(). I guess something like
for(Empresa e : empresasList){
Comboitem item = new Comboitem();
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
cb_empresa.appendItem(item);
}
if(cb_empresa.getItemCount()==1){
cb_empresa.setSelectedIndex(0);
}
Should work. However, there was a bug in ZK that the setSelectedIndex() had to be postponed, as Aloong mentioned. As far as I remember, this bug has been fixed. If not, you can use Event.echoEvent() as a workaround.
for(Empresa e : empresasList){
Comboitem item = new Comboitem();
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
cb_empresa.appendChild(item);
}
if(cb_empresa.getItemCount()==1){
cb_empresa.setSelectedIndex(0);
}
Related
.get(i) is not working
List<WebElement> prdct = driver.findElements(By.xpath("//h4[#class = 'product-name']"));
WebElement addto = driver.findElement(By.xpath("//button[#test() = 'ADD TO CART']"));
for (int i = 0; i < prdct.size(); i++) {
String Name = prdct.get(i).getText();
System.out.println(Name);
if (Name.contains("Pomegranate"))
{
addto.get
break;
}
}
I need to get the i index and perform click action but get() is working, please check the enclosed image for better understanding.
How to proceed with this?
From your posted code it seems you should do the get(i) on prdct that is the list of elements if you have to perform an action with that element.
The addto seems to be an 'ADD TO CART' button and there is only one, you can perform the action in it as well but it's not a list so there is no possibility to use get in it.
In code, you probably want prdct.get(i).click(). And maybe after that addto.click().
addto is not a List but a WebElement, hence you cannot use get(i) (WebElement interface does not extend List).
private void displayGroupsInRanking() {
for (int i = 0; i < 4; i++)
{
RankingANames.setItems(FXCollections.observableArrayList(groupModel.getListA().get(i).getName()));
System.out.println(RankingANames);
}
RankingBNames.setItems(FXCollections.observableArrayList(groupModel.getListB()));
RankingCNames.setItems(FXCollections.observableArrayList(groupModel.getListC()));
RankingDNames.setItems(FXCollections.observableArrayList(groupModel.getListD()));
}
I´m trying to, to get a specific attribute from an arraylist into a new arraylist. This works fine, but the listview only shows one object?
[The output1
the Code
I'll not rewrite code from your screen to show you the right way to do this but I can tell you what is wrong here.
On every iteration you are creating new collection with exactly one item and then you are using it as items list for table.
That's clearly wrong.
To solve it, you have to first prepare full list of items and then pass it to setItems method.
Previously i wrote a query which is queries the proper couchbase file.I have a entity for it:
class SubMenu{
String name,url;
List<SubMenu>; //and of course getters-setters...
}
when i queried i get a arrayList of SubMenu:
Submenu = [name="menu",url="menu",SubMenu[name = "Submenu1",url="submenu1"]
So a SubMenu can contains more Submenu
My JSON:
{
"name":"menu",
"url:"menu",
"subMenu":[{
"name":"submenu",
"url":"submenu",
"wubMenu[]
},
"name":"submenu2",
"url":"submenu2",
"wubMenu[]
]
}
And i want to build a tree like menu which has two children submenu and submenu2...and so on...and if submenu has other submenus go show it.
I started from this example....but i dont know how to solve the override methods...getChild,childCount,isLeaf in my case: ArrayList
Could anyone helps to me? Thanks a lot!
Actually it's pretty straight forward.
IsLeaf actually means is this bean has children or not.
In your example => return true if List<SubMenu> is null or is the size is 0.
childcount => return the size of List<SubMenu>
getChild => return the item at index x of the List. remember also to throw an error or null when index is higher then size of the List.
I am trying to allow my user to search through a table of information, dynamically hiding/showing results that contain the search. I have the hiding part down, and it works well, but I'm having trouble showing the table item again once the search criteria is changed.
Here is my hide code:
searchField.addModifyListener(new ModifyListener() {
#Override
public void modifyText(ModifyEvent arg0) {
modified = true;
for (int i = 0; i < table.getItems().length; i++) {
if (!(table.getItem(i).getText(2)
.contains(searchField.getText()))) {
table.getItem(i).dispose();
}
}
if ("".equals(searchField.getText())) {
modified = false;
//where I would want to un-hide items
}
}
});
Looking at your code, it seems you try to hide the item by calling dispose(). If you dispose a widget, it is gone for good. You cannot get it back.
If you want to unhide it again, will have to create a new item at the position of the previously hidden one with the same content.
Isn't it better to actually operate with some kind of a table model and JFace bindings, rather, then do it like that? And yes, disposing is not hiding. You should probably remove the item from the table.
You have probably to save the data from TableItem into collection before you call dispose. Then when you search again you could check that collection and if matches are found, then insert back into Table by creating new TableItem.
I am trying to search for UserName and return values onto jComboBox, here is the code
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
after you click to somewhere else or click enter,it will conduct the search, however, it will go search for the first item again, which is very confusing... then i tried using key Pressed
if(e.getKeyCode()==13){
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
And this one does not react at all.
You need to set the listener(s) on the Editor not the ComboBox itself. See the answer here:
Detecting when user presses enter in Java
Wow, you're rebuilding a ComboBoxModel each time ? Isn't it a little expensive ? You know there is a MutableComboBoxModel, also implemented by DefaultComboBoxModel that would allow you to add/remove elements from you combobox without rebuilding its model each time ?
Concerning your question, I don't understand the statement
However, if i do that, it does perform correctly, however, it will go search for the first item again
Do you mean your JComboBox starts to blink with content being modified each time ?
if so, maybe is it because your ActionListener is linked to JComboBox, which content changes continuously.
Anyway, i suggest you add some logs, like
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxReceiver.getModel();
model.remvoeAllElements();
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
String username = usrList.get(i).getUserName();
System.out.println(username); // feel free to instead use one loger
model.addElement(username);
}
Besides, i would tend to suggest you an other approach, in which combo box model don't contain simple Strings, but rather User objects, with a ListCellRenderer displaying only the user name.
IMO, what will really be confusing for your users is to have the content and selection of a combo box changed as soon as they select one of its options.
Anyway, if you really want to do that, then you should remove the action listener (or deactivate it) before changing its content, and re-add it (or reactivate it) after :
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.removeActionListener(this);
jComboBoxReceiver.setModel(model);
jComboBoxReceiver.addActionListener(this);
}