JavaFX Boolean Binding for an value ContextMenuItem - java

I want to bind a MenuItem of my contextMenu to an specific value of the selectedValue of the tableView.
Like you can see in the picture: if status is "anwesend" what is specified with an int = 2 in the Controller, then the option should be disabled.
I thought about this here, but it throws a NullPointer because the selection is empty:
contextMenuItemUnentschuldigt.disableProperty().bind(Bindings.createBooleanBinding(() -> !loggedIn || tableView.getSelectionModel().getSelectedItem().getExcused() == 2));
But how to check first if it's not empty and then check that it's not that specific value?

I got the answer:
contextMenuItemUnentschuldigt.disableProperty().bind(Bindings.createBooleanBinding(() -> tableView.getSelectionModel().getSelectedItems().isEmpty() || tableView.getSelectionModel().getSelectedItem().getExcused() == 2, tableView.getSelectionModel().getSelectedItems()));

Related

How can I embed images into a Birt report to show an image depending on if the column result equals 1 or 0?

How can I embed images into a Birt report to show images depending on if the result equals 1 or 0?
I’ve tried following the guide from:
http://birtworld.blogspot.com/2010/09/birt-image-report-item.html
I am not able to show an image (checkbox) when the column value equals 1 and another image (unchecked box) when the column value equals 0.
I’ve tried using the following expression:
if( row["acceptsplititem"] == 1 ){
"checked-50x49.jpg";
}else if( row["acceptsplititem"] == 0 ){
"unchecked-50x49.jpg";
}
I’ve tried adding both images (checked and unchecked) by dragging the image report item and selecting from image file in shared resources, then editing the URL of the image via:
Properties > Advanced > URL
if( row["acceptsplititem"] == 1 ){
"checked-50x49.jpg";
}else if( row["acceptsplititem"] == 0 ){
"unchecked-50x49.jpg";
}
I’ve also tried a suggestion from a previous question/answer on stackoverflow:
Embedding an image in BIRT
But this doesn’t display either image.
Adding an image using Dynamic text
Embedding an image
Please help and advise where I am making a mistake
Thanks in advance,
Stuart
This is an expression, so you should NOT use a semicolon afteer the string literals.
Furthermore, the value might be different from 0 or 1 (e.g. null), so you should also add yet another image filename for the "else" part.
In order to check that your images can be displayed at all, you should also use a constant expression as the very first step:
Constant expression:
"check-50x49.jpg"
This should show the "checked" image.
Next, the expression depending on the value (assuming you also have an image file "other.jpg":
if( row["acceptsplititem"] == 1 ) {
"checked-50x49.jpg"
} else if( row["acceptsplititem"] == 0 ) {
"unchecked-50x49.jpg"
} else {
"unknown.jpg"
}
Or - slightly more verbose:
var fname = "unknown.jpg";
if( row["acceptsplititem"] == 1 ) {
fname = "checked-50x49.jpg";
} else if( row["acceptsplititem"] == 0 ) {
fname = "unchecked-50x49.jpg";
}
fname
I've found that by inserting an image then selecting visibility from the Property Editor you can then tick Hide Element and enter an expression:
row["preferred"]==0
This would hide the element (Tick Box) if the result is 0 Place another image (Crossed Box) on the same row with expression:
row["preferred"]==1
This will hide the image if the result is 1

ComboBox with multiple columns - search by key

I have a JComboBox that displays 2 columns. Now I would like to enable to search by key on all columns. Example:
Column1 | Column2
A1 | S1
A2 | B1
A3 | P1
The search by key on the first column works fine with default Implementation of KeySelectionManager for JComboBox. However, I would also like to be able to search for the second column as well, meaning when I press 'B' the second item is selected.
I have taken a look at the KeySelectionManager but didn't find anything useful. I have attached a screenshot of the ComboBox to show what I mean.
Thanks for any pointers.
Check KeySelectionManager implemetation in JComboBox's source code
class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
public int selectionForKey(char aKey,ComboBoxModel aModel) {
....
for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
Object elem = aModel.getElementAt(i);
if (elem != null && elem.toString() != null) {
v = elem.toString().toLowerCase();
if ( v.length() > 0 && v.charAt(0) == aKey )
return i;
}
}
So try to override your model's Element's class toString() method to include both columns.
If you display two columns in the combo box then you must be using a custom renderer.
So maybe the approach presented in Combo Box With Custom Renderer can help. The solution in the blog combines the renderer and KeySelectionManager into a single class. All you need to do is implement the getDisplayValue() method to return the text to renderer.
In your case you have two pieces of text to render and search, so you might be able to just change the getDisplayValue() method to return List of text you want to display.
Then the renderer can use both items in the List and the getNextMatch() method of the class would also be modified to check each item in the list for a match.

Edit Text and CheckBox checking

I currently have 2 checkboxes with an edit text field beside them. There are 4 checkboxes total and I have the logic to check each one and uncheck others if they are checked (almost radio button style). These 2 however could both be checked if they have values in the edit text fields.
However, if the EditText field (which is set to numeric) has a value of 0 or is blank I want it to uncheck the check box and set the value to 0.
Here is the code I have to do this
if (etBase.getText().toString() == "0" || etBase.getText().toString() == ""){
etBase.setText("0");
cbBase.setChecked(false);
} else {
cbBase.setChecked(true);
}
if (etField.getText().toString() == "" || etField.getText().toString() == "0"){
etField.setText("0");
cbField.setChecked(false);
} else {
cbField.setChecked(true);
}
As it sits right now I default the two fields to be "0" when it starts. When this logic runs, it is setting both checkboxes to checked.
I must be missing something here.
To compare string, you must use .equals() function. ( Or .compareTo() but that's no the today's deal)
Ex :
if (etBase.getText().equals("0") || etBase.getText().equals("")){
...
}
var1 == var2 is used to get if these both variable have the same object

Getting error when checking combobox for null value

I got a combobox, and a submit button, when the button is submitted, i want to check if the combobox value was null. Im using this code:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem().equals(null)) {
infoLabel.setText("Combo box value was null");
}
i am getting this error when i press the submit button:
java.lang.NullPointerException
how can i fix this?
You can not call equals on null. Instead simply use == null.
Something like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
Should work.
You can not give the null reference to equals(), do it like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
And a remark that has nothing to do with your question: I suggest using the Java Naming Convention, which would lead to your combo box being named comboBox (and not ComboBox).
The condition should be :
ComboBox.getSelectedItem() != null
or
ComboBox.getSelectedItem().toString().equals("")
This checks if what is selected in the Combobox is null or empty
Another way of doing this is leaving the first item empty, then check for the selected index against 0 i.e
ComboBox.getSelectedIndex() != 0
Thanks

Checking for NULL in Java

I am using a Bonded JComboBox. What I want to know is how do I check if that JComboBox has any items in it or not.
Thank you.
To test if the JComboxBox reference is null, you can compare it with null using the == operator. To test if the combo-box contains any items, you can use the instance method getItemCount, which returns the number of items it contains.
JComboxBox box = ...
boolean boxIsNull = (box == null); // answers the title of the question
boolean boxHasItems = (box.getItemCount() > 0);

Categories

Resources