show the 1st and 2nd value of object in comboBox - java

I have a combobox with list of authors and with change in combobox I have to show the author's detail in table using java swing. I did like:
for(Author author: Application.authors){
jComboBoxAuthors.addItem(author);
}
and with change in item selected :
if(jComboBoxAuthors.getSelectedIndex()>0){
Author author = (Author)e.getItem();
String name = author.getFirstName()+" "+author.getLastName();
}
It shows object in combo but i need the name only and if I dojComboBoxAuthors.addItem(author.getFirstName());
I can't get value in table ie. name return nothing. How can I fix this issue?

One thing you can do is override the toString() method as following
#Override
public String toString() {
return firstName+" "+lastName; // so that name will be displayed instead of default object
}
But it has its limitations. Hope it helps

Using a custom renderer will break the default functionality of a JComboBox. That is you will no longer be able to select an item using the keyboard.
Check out Combo Box With Custom Renderer for more information and a more complete solution that shows how to fix this problem.

Related

Vaadin Grid TextField for Boolean StringToBooleanConverter

While working on an in-line editing feature in Vaadin Grid (8.1.0), I have created the beans and use setItems method from the Grid to populate all rows.
But when I double clicked a row to edit it, an exception came up. I thought I have bound the bean's property type correctly with the TextField but it still throws exception.
The following is my manual binding code that finds a Boolean property to the Textfield I like to use for editing.
Binder<RegistrationRecord> needFancialFlagBinder = new Binder<>(RegistrationRecord.class);
needFancialFlagBinder .forField ( needFancialFlagField )
.withNullRepresentation( "" )
.withConverter (new StringToBooleanConverter("Need financial flag must be true or false!"))
.bind ( RegistrationRecord:: isNeedFancialFlag, RegistrationRecord:: setNeedFancialFlag);
The following code attaches the TextField with the column in the Grid.
registrationGrid.getColumn("needFancialFlag")
.setEditorComponent(needFancialFlagField)
.setExpandRatio(1);
Below is part of the exception. Does the StringToBooleanConverter only take care of converting from String to Boolean and not the other way around? What method should I be using for the other direction?
java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
at com.vaadin.ui.AbstractTextField.setValue(AbstractTextField.java:47) ~[vaadin-server-8.1.0.jar:8.1.0]
at com.vaadin.data.Binder$BindingImpl.initFieldValue(Binder.java:893) ~[vaadin-server-8.1.0.jar:8.1.0]
at com.vaadin.data.Binder$BindingImpl.access$100(Binder.java:766) ~[vaadin-server-8.1.0.jar:8.1.0]
at com.vaadin.data.Binder.lambda$readBean$2(Binder.java:1386) ~[vaadin-server-8.1.0.jar:8.1.0]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_121]
So, the question was asked inadequately. I wanted to use a TextField to edit a Boolean model member in a column of a Grid and somewhere in the process, a better idea was proposed. Instead of using a TextField, I should have used a CheckBox. So, the solution got turned around, and below is the correct code. Note this is Vaadin 8.1.0. (I found Vaadin has changed a lot of versions quickly.)
private void addBooleanPropertyColumn(Grid theGrid, String propertyName, String caption) {
CheckBox bBox = new CheckBox();
Column<RegistrationRecord, String> adultFlagColumn = theGrid.addColumn(record->
"<span class=\"v-checkbox v-widget\"><input type=\"checkbox\" id=\"my-uid-1\" " + returnChecked(propertyName, record) + " > <label for=\"my-uid-1\"></label> </span>",
new HtmlRenderer());
adultFlagColumn.setId(propertyName)
.setCaption(caption)
.setEditorComponent(bBox)
.setExpandRatio(1);
}
The concept is that a Column in a Grid supports the association of a Renderer, a Validator, and an Editor. Once you have learned that, it becomes straightforward.
I am not using a Validator since the model field is only a Boolean. And it is worth noting the editor component must have an associated property, hence the setID, a convenient setter.

Appium : Verify relative element presence

Can you give a suggestion(please see pic) how can I check if selectIndicator is present on one block then I should choose another one. I know how to check if that element isPresent on whole page, but I need to find if it present on particular element. In my example I have Living Room chosen, and I need to check if DVR not chosen -choose that one. Any idea how can I do it? I was trying to check this way, but no luck:
WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
[http://i.stack.imgur.com/F98DM.png]
If I am not getting you wrong you want to implement a self defined data structure for an appropriate solution. That could be something similar to this :
public class DVRList {
//declare components required to comprise one item
private String dvrOptionText ;
private boolean dvrOptionCheck ;
// implement setter..getter for these two
}
...in some method set the value using the logic
DVRList dvrlist = new DVRList();
WebElement parentOfBoth = driver.findElement(By.xpath("
//android.widget.RelativeLayout[1]/android.widget.R‌​elativeLayout[1]");
String text = parentOfBoth.findElementByAccessibilityId("First element").getText();
dvrlist.setdvrOptionText(text);
if(isElement(parenOfBoth.findElementByAccessibilityId("Second element"))
dvrlist.setdvrOptionCheck(true);
else dvrlist.setdvrOptionCheck(false);
and thereafter you can use these parameters accordingly.
Note : Parameters and approach are generalised and should be modified for serving the exact purpose.

checkbox to string conversion

I have a page that contains diff properties with checkboxes.and I have another page that contains the summary of the above page. That means If I check the in the first page its has to reflect the relavent field details in the summary page.But as the variable type in pojo is boolean its showing as true of false.I unable to get the fields name.Im using HTML front end and java. Please help me on how to do it.
In pojo the variable is boolean. Im fetching that variable in the next summary page. I need that to be name of the variable not true of false. hope im clear
You can add the string you want to properties, evaluate your boolean, get the message you want from properties according to the boolean value:
public String getLabel(){ if(isMarried) return messages.get(marriedPropertyMessage);}

How to get source object from DocumentListener (DocumentEvent)? [duplicate]

This question already has answers here:
how to find source component that generated a DocumentEvent
(3 answers)
Closed 9 years ago.
I have my class and I have implemented DocumentListener
public void removeUpdate( DocumentEvent arg0 ) {
System.out.println( arg0.getDocument());
}
It would print javax.swing.text.PlainDocument#49ea903c
Is there any possible way I would get the object so I can get the value of the changed textfield? At the moment I have only one field so I do not need a check, but what if I use two or more, how do I know which JTextField has notified the listener?
I'm not sure it's possible to get the swing component from a Document. But the issue is easily solved: just add a different instance of the listener to every text field, and store the text field in the listener itself.
textField1.getDocument().addDocumentListener(new MyDocumentListener(textField1));
textField2.getDocument().addDocumentListener(new MyDocumentListener(textField2));
textField3.getDocument().addDocumentListener(new MyDocumentListener(textField3));
One option is to use an inner class, which will provide you an opportunity to reference the text field.
final JTextField field = new JTextField();
field.getDocument().addDocumentListener(new DocumentListener() {
// Here you can reference 'field' in your methods
});
If you need to perform the same action for each text field, JB Nizet's solution will be neater.
You actually are getting the PlainDocument object. Just store it in a variable instead of printing it.
For more info see docs.
What you need to do is adding document listners to each component you need.If you really need to know which text fields text has changed then you can have a property in the DocumentListner as textFieldName or something and you can set it when you create the document listner for them.But I think you better change your approaching to situation.It doesn't sound good.
You may update the document without knowing the context.
Consult the documentation of javax.swing.event.DocumentEvent
javax.swing.event.DocumentEvent.getOffset() Returns the offset within the document of the start of the change.
javax.swing.event.DocumentEvent.getLength() Returns the length of the change.
These methods combined with the document retrieved by javax.swing.event.DocumentEvent.getDocument() allow you to update the document in a proper way. Later you may add other text field without any change.
To simply get the text of a JTextField after change:
JTextField myTf = new ...; // maybe an attribute definition
...
myTf.addActionListener(
new ActionListener(){#Override public void actionPerformed( ActionEvent e ) {
JTextField tf = (JTextField)e.getSource();
System.out.println( tf.getText());
if( tf == myTf ) { // == for reference comparison
... // do something dedicated to myTf
}
}});

Java Binding bean to String empty property

I am trying to bind the visibility of a JLabel to whether the text of a JTextField is empty or not.
I want to do this because I want to hide the JLabel with a red asterisk, which denotes that filling in a text field is compulsory, so it should hide when it is filled in.
The following does however not work (with ${text.isEmpty}):
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE,
estimatedCostTextField,
org.jdesktop.beansbinding.ELProperty.create("${text.isEmpty}"),
estimatedCostAsterisk,
org.jdesktop.beansbinding.BeanProperty.create("visible"));
bindingGroup.addBinding(binding);
Can anybody help me with this?
I found the answer. You should use ${empty text}
So the code becomes:
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ,
estimatedCostTextField,
org.jdesktop.beansbinding.ELProperty.create("${empty text}"),
estimatedCostAsterisk,
org.jdesktop.beansbinding.BeanProperty.create("visible"));
bindingGroup.addBinding(binding);

Categories

Resources