I am new to Java and I have started developing applications in java using javaFx. searched a lot but couldn't find any date and time picker in javaFx. Even i tried JFxtras but its not working. By the way i am using javafx 2.2.3 and java 7. Any help would be highly appreciated.
Here is a Java version of the DateTimePicker control above, slightly improved.
This code is now part of TornadoFX Controls and you can have a look at the latest version of DateTimePicker.java in the GitHub Repo. The control is available in Maven Central as well under these coordinates:
<dependency>
<groupId>no.tornado</groupId>
<artifactId>tornadofx-controls</artifactId>
<version>1.0.3</version>
</dependency>
The implementation right now:
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.DatePicker;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.StringConverter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* A DateTimePicker with configurable datetime format where both date and time can be changed
* via the text field and the date can additionally be changed via the JavaFX default date picker.
*/
#SuppressWarnings("unused")
public class DateTimePicker extends DatePicker {
public static final String DefaultFormat = "yyyy-MM-dd HH:mm";
private DateTimeFormatter formatter;
private ObjectProperty<LocalDateTime> dateTimeValue = new SimpleObjectProperty<>(LocalDateTime.now());
private ObjectProperty<String> format = new SimpleObjectProperty<String>() {
public void set(String newValue) {
super.set(newValue);
formatter = DateTimeFormatter.ofPattern(newValue);
}
};
public DateTimePicker() {
getStyleClass().add("datetime-picker");
setFormat(DefaultFormat);
setConverter(new InternalConverter());
// Syncronize changes to the underlying date value back to the dateTimeValue
valueProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
dateTimeValue.set(null);
} else {
if (dateTimeValue.get() == null) {
dateTimeValue.set(LocalDateTime.of(newValue, LocalTime.now()));
} else {
LocalTime time = dateTimeValue.get().toLocalTime();
dateTimeValue.set(LocalDateTime.of(newValue, time));
}
}
});
// Syncronize changes to dateTimeValue back to the underlying date value
dateTimeValue.addListener((observable, oldValue, newValue) -> {
setValue(newValue == null ? null : newValue.toLocalDate());
});
// Persist changes onblur
getEditor().focusedProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue)
simulateEnterPressed();
});
}
private void simulateEnterPressed() {
getEditor().fireEvent(new KeyEvent(getEditor(), getEditor(), KeyEvent.KEY_PRESSED, null, null, KeyCode.ENTER, false, false, false, false));
}
public LocalDateTime getDateTimeValue() {
return dateTimeValue.get();
}
public void setDateTimeValue(LocalDateTime dateTimeValue) {
this.dateTimeValue.set(dateTimeValue);
}
public ObjectProperty<LocalDateTime> dateTimeValueProperty() {
return dateTimeValue;
}
public String getFormat() {
return format.get();
}
public ObjectProperty<String> formatProperty() {
return format;
}
public void setFormat(String format) {
this.format.set(format);
}
class InternalConverter extends StringConverter<LocalDate> {
public String toString(LocalDate object) {
LocalDateTime value = getDateTimeValue();
return (value != null) ? value.format(formatter) : "";
}
public LocalDate fromString(String value) {
if (value == null) {
dateTimeValue.set(null);
return null;
}
dateTimeValue.set(LocalDateTime.parse(value, formatter));
return dateTimeValue.get().toLocalDate();
}
}
}
The dateTimeValue property contains the value with time, and the valueProperty contains only the date value.
I have not added tests for this component yet, so the implementation might change, check GitHub for the latest version.
JFXtras project has a working version for JavaFX 2.2. Look for CalendarPicker, CalendarTimePicker, ... at the repo, under the 2.2 branch.
You can test it by downloading the lastest release (2.2-r6-SNAPSHOT) from jfxtras.org.
This short snippet will create a calendar for picking both date and time:
#Override
public void start(Stage primaryStage) {
CalendarPicker dateTime = new CalendarPicker();
dateTime.withCalendar(Calendar.getInstance());
dateTime.withShowTime(Boolean.TRUE);
dateTime.withLocale(Locale.ENGLISH);
dateTime.calendarProperty().addListener(new ChangeListener<Calendar>() {
#Override
public void changed(ObservableValue<? extends Calendar> ov, Calendar t, Calendar t1) {
System.out.println("Selected date: "+t1.getTime().toString());
}
});
StackPane root = new StackPane();
root.getChildren().add(dateTime);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Date & Time from JFXtras 2.2");
primaryStage.setScene(scene);
primaryStage.show();
}
I find it most convenient to enter the time via the keyboard instead of changing it with sliders. It's quite easy to extend the included DatePicker to look like this:
I also find it annoying that the DatePicker doesn't commit the edited value in the TextField onblur, so the following code fixes that as well.
The snippet is written in Kotlin for brevety, you can easily convert it to Java via IntelliJ IDEA:
import javafx.beans.property.SimpleObjectProperty
import javafx.scene.control.DatePicker
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.input.MouseEvent
import javafx.util.StringConverter
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter
class DateTimePicker(val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")) : DatePicker() {
private var dateTimeValue = SimpleObjectProperty<LocalDateTime>(LocalDateTime.now())
init {
converter = object : StringConverter<LocalDate>() {
override fun toString(value: LocalDate?): String {
return if (dateTimeValue.get() != null) dateTimeValue.get().format(formatter) else ""
}
override fun fromString(value: String?): LocalDate? {
if (value == null) {
dateTimeValue.set(null)
return null
}
dateTimeValue.set(LocalDateTime.parse(value, formatter))
return dateTimeValue.get().toLocalDate()
}
}
// Syncronize changes to the underlying date value back to the dateTimeValue
valueProperty().addListener { observable, old, new ->
if (new == null) {
dateTimeValue.set(null)
} else {
if (dateTimeValue.get() == null) {
dateTimeValue.set(LocalDateTime.of(new, LocalTime.now()))
} else {
val time = dateTimeValue.get().toLocalTime()
dateTimeValue.set(LocalDateTime.of(new, time))
}
}
}
// Syncronize changes to dateTimeValue back to the underlying date value
dateTimeValue.addListener { observable, old, new ->
valueProperty().set(new?.toLocalDate())
}
// Persist changes onblur
editor.focusedProperty().addListener { observable, old, new ->
if (!new)
simulateEnterPressed()
}
}
private fun simulateEnterPressed() =
editor.fireEvent(KeyEvent(editor, editor, KeyEvent.KEY_PRESSED, null, null, KeyCode.ENTER, false, false, false, false))
fun dateTimeValueProperty() = dateTimeValue;
}
Bind your LocalDateTime property to the dateTimeValueProperty.
Slightly "improved" (at least for my needs) version that works with NullableTimeStamp... in order to be able to, well, null it (for ease with MySQL)...
Dunno if this can help anyone but here it is:
NullableTimeStamp:
public class NullableTimestamp extends Timestamp {
public NullableTimestamp() {
super(0L);
}
public NullableTimestamp(long value) {
super(value);
}
#Override
public String toString() {
return this.getTime() > 0L ? super.toString() : "";
}
public static NullableTimestamp valueOf(LocalDateTime localDateTime) {
return new NullableTimestamp(Timestamp.valueOf(localDateTime).getTime());
}
}
and DateTimePicker:
public class DateTimePicker extends DatePicker {
public static final String DefaultFormat = "yyyy-MM-dd HH:mm";
private DateTimeFormatter formatter;
private ObjectProperty<LocalDateTime> dateTimeValue = new SimpleObjectProperty<>(LocalDateTime.now());
private ObjectProperty<String> format = new SimpleObjectProperty<String>() {
public void set(String newValue) {
super.set(newValue);
formatter = DateTimeFormatter.ofPattern(newValue);
}
};
public DateTimePicker() {
getStyleClass().add("datetime-picker");
setFormat(DefaultFormat);
setConverter(new InternalConverter());
// Syncronize changes to the underlying date value back to the dateTimeValue
valueProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
dateTimeValue.set(null);
} else {
if (dateTimeValue.get() == null) {
dateTimeValue.set(LocalDateTime.of(newValue, LocalTime.now()));
} else {
LocalTime time = dateTimeValue.get().toLocalTime();
dateTimeValue.set(LocalDateTime.of(newValue, time));
}
}
});
// Syncronize changes to dateTimeValue back to the underlying date value
dateTimeValue.addListener((observable, oldValue, newValue) -> {
setValue(newValue == null ? null : newValue.toLocalDate());
});
// Persist changes onblur
getEditor().focusedProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue)
simulateEnterPressed();
});
}
private void simulateEnterPressed() {
getEditor().fireEvent(new KeyEvent(getEditor(), getEditor(), KeyEvent.KEY_PRESSED, null, null, KeyCode.ENTER, false, false, false, false));
}
public LocalDateTime getDateTimeValue() {
return dateTimeValue.get();
}
public void setDateTimeValue(LocalDateTime dateTimeValue) {
if(dateTimeValue.isAfter(LocalDateTime.of(1971, 6, 30, 12, 00)))
this.dateTimeValue.set(dateTimeValue);
else
this.dateTimeValue.set(null);
}
public ObjectProperty<LocalDateTime> dateTimeValueProperty() {
return dateTimeValue;
}
public String getFormat() {
return format.get();
}
public ObjectProperty<String> formatProperty() {
return format;
}
public void setFormat(String format) {
this.format.set(format);
}
class InternalConverter extends StringConverter<LocalDate> {
public String toString(LocalDate object) {
LocalDateTime value = getDateTimeValue();
return (value != null) ? value.format(formatter) : "";
}
public LocalDate fromString(String value) {
if (value == null) {
dateTimeValue.set(null);
return null;
}
dateTimeValue.set(LocalDateTime.parse(value, formatter));
return dateTimeValue.get().toLocalDate();
}
}
}
It basically masks the 0L Timestamp value as if it was NULL... hope this can help cheers
Related
Below is the relevant method. One of the properties is of LocalDate (Joda).
#ApiMethod(
name = "taxforms.get",
path = "tax-forms",
httpMethod = ApiMethod.HttpMethod.GET
)
public TaxDataList retrieveTaxDataList(
HttpServletRequest httpServletRequest
) {
TaxDataList taxDataList = new TaxDataList( );
TaxData taxData = SampleTax.sampleTaxData( "Tax1098" );
taxDataList.addFormsItem( taxData );
return taxDataList;
}
If I do my own serialization, my code includes this:
ObjectMapper objectMapper = new ObjectMapper( );
// Special handling for dates
objectMapper.registerModule( new JodaModule( ) );
objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS );
objectMapper.writeValue( sw, data );
json = sw.toString( );
How can I customize the way the framework does the serialization?
This is a close sample code to what you want and which uses transforms java LocalDate and Instant classes into strings and numbers:
package com.company.example;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.Transformer;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
#Api(
name="myApi",
version="v1",
transformers={
MyApi.MyInstantTransformer.class,
MyApi.MyLocalDateTransformer.class,
}
)
public class MyApi {
#ApiMethod(name="getStuff")
public MyStuff getStuff() {
return new MyStuff();
}
public static class MyStuff {
private final LocalDate date;
private final Instant instant;
MyStuff() {
date = LocalDate.now();
instant = Instant.now();
}
public LocalDate getDate() { return date; }
public Instant getInstant() { return instant; }
}
public static class MyInstantTransformer implements Transformer<Instant, Long> {
public Instant transformFrom(Long input) {
return Instant.ofEpochMilli(input);
}
public Long transformTo(Instant input) {
return input.toEpochMilli();
}
}
public static class MyLocalDateTransformer implements Transformer<LocalDate, String> {
public LocalDate transformFrom(String input) {
return LocalDate.parse(input, DateTimeFormatter.ISO_LOCAL_DATE);
}
public String transformTo(LocalDate input) {
return input.format(DateTimeFormatter.ISO_LOCAL_DATE);
}
}
}
I'm trying to create custom field in Magnolia CMS. For testing purpose I tried the simplest example and did it the same way as the TextField.
Here is my Definition:
package info.magnolia.blossom.sample.module.ui.form.field.definition;
import info.magnolia.i18nsystem.I18nText;
import info.magnolia.ui.field.ConfiguredFieldDefinition;
import info.magnolia.ui.field.FieldType;
#FieldType("customTextField")
public class CustomTextFieldDefinition extends ConfiguredFieldDefinition<String> {
private int rows;
private int maxLength = -1;
private String placeholder;
#Inject
public CustomTextFieldDefinition() {
this.setType(String.class);
this.setFactoryClass(CustomTextFieldFactory.class);
}
#Override
public String getLabel() {
return super.getLabel() + "33";
}
#I18nText(
fallback = ""
)
public String getPlaceholder() {
return this.placeholder;
}
public int getRows() {
return this.rows;
}
public int getMaxLength() {
return this.maxLength;
}
public void setRows(int rows) {
this.rows = rows;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public void setPlaceholder(String placeholder) {
this.placeholder = placeholder;
}
}
Here is my Factory class:
package info.magnolia.blossom.sample.module.ui.form.field.factory;
import com.vaadin.data.HasValue;
import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.Component;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import info.magnolia.blossom.sample.module.ui.form.field.definition.CustomTextFieldDefinition;
import info.magnolia.objectfactory.ComponentProvider;
import info.magnolia.ui.field.factory.AbstractFieldFactory;
import javax.inject.Inject;
public class CustomTextFieldFactory extends AbstractFieldFactory<String, CustomTextFieldDefinition> {
#Inject
public CustomTextFieldFactory(CustomTextFieldDefinition definition, ComponentProvider componentProvider) {
super(definition, componentProvider);
}
#Override
public HasValue<String> createField() {
return super.createField();
}
#Override
public Component createFieldComponent() {
Object field;
if (this.getDefinition().getRows() > 1) {
TextArea textArea = new TextArea();
textArea.setRows(this.getDefinition().getRows() + 10);
field = textArea;
} else {
field = new TextField();
}
if (this.getDefinition().getMaxLength() != -1) {
((AbstractTextField)field).setMaxLength(this.getDefinition().getMaxLength());
}
((AbstractTextField)field).setPlaceholder(this.getDefinition().getPlaceholder());
return (Component)field;
}
}
Here is my register:
This is my dialog:
form:
tabs:
- name: tabMain
fields:
- name: title
class: info.magnolia.ui.form.field.definition.TextFieldDefinition
- name: resultPage
i18n: true
class: info.magnolia.blossom.sample.module.ui.form.field.CustomTextFieldDefinition
label: Test Field
actions:
commit:
class: info.magnolia.ui.admincentral.dialog.action.SaveDialogActionDefinition
cancel:
class: info.magnolia.ui.admincentral.dialog.action.CancelDialogActionDefinition
This is what I see as a result:
As you can see the second field is not shown.
I don't have errors in the console. The only "Minor" error I have in the definitions app is this one:
Element [info.magnolia.blossom.sample.module.ui.form.field.definition.CustomTextFieldDefinition#7e441530]
of type [info.magnolia.blossom.sample.module.ui.form.field.definition.CustomTextFieldDefinition]
may not be added to the collection of type
[interface info.magnolia.ui.form.field.definition.FieldDefinition]
Title: Source data processing problem
Path: /form/tabs/tabMain/fields/resultPage
I'm using Magnolia 6.2.3.
Any idea what I'm missing?
Thanks!
Your example is all correct. However, you try to add that dialog to a 'compatibility-app'. That's why you receive the error saying 'I cannot add this type' and it shows that it tries to use different field definition (the one under definition package)
This page explains well the logic behind compatibility-apps and new apps.
https://documentation.magnolia-cms.com/display/DOCS62/Changes+in+Magnolia+6+UI
In a JavaFX TreeView I'm using 'custom' classes which extend TreeItem. This makes me able to edit the items in the TreeView (I can double click them and edit the contents when running the application) but I can't seem to be able to set the .setOnEditCommit() method properly. I was hoping it'd work similar as the function in a tableview but I didn't have any luck yet.
This is my code in my controller in which I try to set the setOnEditCommit() method. In my TreeView called 'trvDivisies' I display football team divisions / competitions and one level lower I display all the teams that are in a certain division.
private void setUpTreeView() {
trvDivisies.setEditable(true);
trvDivisies.setShowRoot(false);
TreeItem<String> root = new TreeItem<>();
for (Divisie d : divisies) {
TreeItem<String> divisieTreeItem = d;
divisieTreeItem.valueProperty().set(d.getNaam());
for (VoetbalTeam vt : d.getVoetbalTeams()) {
TreeItem<String> voetbalTeamTreeItem = vt;
voetbalTeamTreeItem.valueProperty().setValue(vt.getTeamNaam());
divisieTreeItem.getChildren().add(voetbalTeamTreeItem);
}
root.getChildren().add(divisieTreeItem);
}
trvDivisies.setRoot(root);
trvDivisies.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
System.out.println(newValue);
}
});
trvDivisies.setCellFactory(TextFieldTreeCell.forTreeView());
// I get an error at the following line when compiling
trvDivisies.setOnEditCommit((TreeView.EditEvent p) -> {
TreeItem<String> selectedItem = p.getTreeItem();
if (selectedItem instanceof Divisie) {
updateDivisie((Divisie)selectedItem);
} else if (selectedItem instanceof VoetbalTeam) {
updateTeam((VoetbalTeam)selectedItem);
}
});
}
This is what my 'custom' classes look like.
public class Divisie extends TreeItem<String> {
private static int idCount = 0;
private int id;
private String naam;
private List<VoetbalTeam> voetbalTeams;
public int getId() {
return id;
}
public String getNaam() {
return naam;
}
public List<VoetbalTeam> getVoetbalTeams() {
return voetbalTeams;
}
public Divisie(int id, String naam) {
super(naam);
this.id = id;
this.naam = naam;
}
public Divisie(String naam) {
this.id = ++idCount;
this.naam = naam;
}
public void addTeam(VoetbalTeam toBeAdded) {
if (voetbalTeams == null) {
voetbalTeams = new LinkedList<>();
}
voetbalTeams.add(toBeAdded);
}
#Override
public String toString() {
return this.naam;
}
}
Second 'lower level' class
public class VoetbalTeam extends TreeItem<String> {
private static int idCount = 0;
private int id;
private String teamNaam;
private List<Speler> spelers;
public int getId() {
return id;
}
public String getTeamNaam() {
return teamNaam;
}
public List<Speler> getSpelers() {
return this.spelers;
}
public VoetbalTeam(int id, String teamNaam) {
super(teamNaam);
this.id = id;
this.teamNaam = teamNaam;
}
public VoetbalTeam(String teamNaam) {
super(teamNaam);
this.id = ++idCount;
this.teamNaam = teamNaam;
}
public void addSpeler(Speler nieuweSpeler) {
if (spelers == null) {
spelers = new LinkedList<>();
}
this.spelers.add(nieuweSpeler);
}
#Override
public String toString() {
return this.teamNaam;
}
}
When trying to run the application WITH the .setOnEditCommit() method I get an error saying:
Error:(97, 37) java: incompatible types: incompatible parameter types in lambda expression
I was hoping you guys can tell me what I need to change my TreeView.EditEvent lambda to or help me find an easier solution.
For a TreeView<T>, the signature of setOnEditCommit is
void setOnEditCommit(EventHandler<TreeView.EditEvent<T>> value)
Since you have (apparently) a TreeView<String>, you need
trvDivisies.setOnEditCommit((TreeView.EditEvent<String> p) -> {
// ...
});
Or, of course, you can just let the compiler do the work for you:
trvDivisies.setOnEditCommit(p -> {
// ...
});
I have a CursorLoader that observes a directory with this URI:
uriDirectory = content://com.myapp.stocks/stocks
and another CursorLoader that observes an item with this URI:
uriItem = content://com.myapp.stocks/stocks/GOOG
When I update uriItem and call getContext().getContentResolver().notifyChange(uriItem , null); in my ContentProvider, how can I prevent it from notifying uriDirectory as well?
Thanks!
Edit: So my solution so far is just to have a boolean that is set to true when I notify a uriItem. Then when it notifies the parent, uriDirectory, it will see that the boolean is true and won't perform any operations. After, I set the boolean back to false.
You can write your own CursorLoader. The default cursor loaders register a content observer via Cursor.RegisterContentObserver(ContentObserver observer). Instead, we want to use registerContentObserver(Uri uri, boolean notifyForDescendants, ContentObserver observer).
I'm not sure if you are using the support library CursorLoader but for the greatest applicability, that's what I'm using.
The only changes from the stock android versions are in loadInBackground(). You should create an entire class instead of just extending android's and overriding loadInBackground because it protects you from future changes made to Android. Be advised that this will not use any notification url you set for the cursor in your ContentProvider unless you the device is KitKat or newer
Uri notificationUri;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
notificationUri = cursor.getNotificationUri();
} else {
notificationUri = mUri;
}
getContext().getContentResolver().registerContentObserver(
notificationUri != null ? notificationUri : mUri,
false, //don't notify for descendants
mObserver
);
Full class descendantChangeIgnoringCursorLoader.java:
package com.innomatixdata.busscan.utils;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.ContentResolverCompat;
import android.support.v4.content.Loader;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Arrays;
public class DescendantChangeIgnoringCursorLoader extends AsyncTaskLoader<Cursor> {
final Loader.ForceLoadContentObserver mObserver;
Uri mUri;
String[] mProjection;
String mSelection;
String[] mSelectionArgs;
String mSortOrder;
Cursor mCursor;
android.support.v4.os.CancellationSignal mCancellationSignal;
/* Runs on a worker thread */
#Override
public Cursor loadInBackground() {
synchronized (this) {
if (isLoadInBackgroundCanceled()) {
throw new android.support.v4.os.OperationCanceledException();
}
mCancellationSignal = new android.support.v4.os.CancellationSignal();
}
try {
Cursor cursor = ContentResolverCompat.query(getContext().getContentResolver(),
mUri, mProjection, mSelection, mSelectionArgs, mSortOrder,
mCancellationSignal);
if (cursor != null) {
try {
// Ensure the cursor window is filled.
cursor.getCount();
Uri notificationUri;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
notificationUri = cursor.getNotificationUri();
} else {
notificationUri = mUri;
}
getContext().getContentResolver().registerContentObserver(
notificationUri != null ? notificationUri : mUri,
false, //don't notify for descendants
mObserver
);
} catch (RuntimeException ex) {
cursor.close();
throw ex;
}
}
return cursor;
} finally {
synchronized (this) {
mCancellationSignal = null;
}
}
}
#Override
public void cancelLoadInBackground() {
super.cancelLoadInBackground();
synchronized (this) {
if (mCancellationSignal != null) {
mCancellationSignal.cancel();
}
}
}
/* Runs on the UI thread */
#Override
public void deliverResult(Cursor cursor) {
if (isReset()) {
// An async query came in while the loader is stopped
if (cursor != null) {
cursor.close();
}
return;
}
Cursor oldCursor = mCursor;
mCursor = cursor;
if (isStarted()) {
super.deliverResult(cursor);
}
if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
oldCursor.close();
}
}
/**
* Creates an empty unspecified CursorLoader. You must follow this with
* calls to {#link #setUri(Uri)}, {#link #setSelection(String)}, etc
* to specify the query to perform.
*/
public DescendantChangeIgnoringCursorLoader(Context context) {
super(context);
mObserver = new Loader.ForceLoadContentObserver();
}
/**
* Creates a fully-specified CursorLoader. See {#link ContentResolver#query(Uri, String[],
* String, String[], String) ContentResolver.query()} for documentation on the meaning of the
* parameters. These will be passed as-is to that call.
*/
public DescendantChangeIgnoringCursorLoader(Context context, Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
super(context);
mObserver = new Loader.ForceLoadContentObserver();
mUri = uri;
mProjection = projection;
mSelection = selection;
mSelectionArgs = selectionArgs;
mSortOrder = sortOrder;
}
/**
* Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
* will be called on the UI thread. If a previous load has been completed and is still valid
* the result may be passed to the callbacks immediately.
*
* Must be called from the UI thread
*/
#Override
protected void onStartLoading() {
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
/**
* Must be called from the UI thread
*/
#Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
#Override
public void onCanceled(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
#Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
}
public Uri getUri() {
return mUri;
}
public void setUri(Uri uri) {
mUri = uri;
}
public String[] getProjection() {
return mProjection;
}
public void setProjection(String[] projection) {
mProjection = projection;
}
public String getSelection() {
return mSelection;
}
public void setSelection(String selection) {
mSelection = selection;
}
public String[] getSelectionArgs() {
return mSelectionArgs;
}
public void setSelectionArgs(String[] selectionArgs) {
mSelectionArgs = selectionArgs;
}
public String getSortOrder() {
return mSortOrder;
}
public void setSortOrder(String sortOrder) {
mSortOrder = sortOrder;
}
#Override
public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
super.dump(prefix, fd, writer, args);
writer.print(prefix); writer.print("mUri="); writer.println(mUri);
writer.print(prefix); writer.print("mProjection=");
writer.println(Arrays.toString(mProjection));
writer.print(prefix); writer.print("mSelection="); writer.println(mSelection);
writer.print(prefix); writer.print("mSelectionArgs=");
writer.println(Arrays.toString(mSelectionArgs));
writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder);
writer.print(prefix); writer.print("mCursor="); writer.println(mCursor);
}
}
I have a case where I need to filter a ObservableList<Item> based on some properties of the items (i.e. the condition is internal and not external). I found out that javafx has FilteredList so I tried it. I could set the predicate and filtering works, until the property value that determines the filtering changes. Setting the predicate is done now like following:
list.setPredicate(t -> !t.filteredProperty().get())
Since the predicate returns boolean and not BooleanProperty, the changes to that property are not reflected on the list.
Is there any easy solution to this? I could try to do some workarounds, e.g. create a separate list and sync that, or reset the predicate every time the property changes in one item hopefully retriggering the filtering, but I first wanted to ask in case someone knows a pretty solution as those workarounds certainly are not.
Create the underlying list with an extractor. This will enable the underlying list to fire update events when the filteredProperty() of any elements change. The FilteredList will observe these events and so will update accordingly:
ObservableList<Item> baseList = FXCollections.observableArrayList(item ->
new Observable[] {item.filteredProperty()});
FilteredList<Item> list = new FilteredList<>(baseList, t -> ! t.filteredProperty().get());
Quick demo:
import java.util.stream.IntStream;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
public class DynamicFilteredListTest {
public static void main(String[] args) {
ObservableList<Item> baseList = FXCollections.observableArrayList(item ->
new Observable[] {item.filteredProperty()});
FilteredList<Item> list = new FilteredList<>(baseList, t -> ! t.isFiltered());
list.addListener((Change<? extends Item> c) -> {
while (c.next()) {
if (c.wasAdded()) {
System.out.println(c.getAddedSubList()+ " added to filtered list");
}
if (c.wasRemoved()) {
System.out.println(c.getRemoved()+ " removed from filtered list");
}
}
});
System.out.println("Adding ten items to base list:\n");
IntStream.rangeClosed(1, 10).mapToObj(i -> new Item("Item "+i)).forEach(baseList::add);
System.out.println("\nFiltered list now:\n"+list);
System.out.println("\nSetting filtered flag for alternate items in base list:\n");
IntStream.range(0, 5).map(i -> 2*i).mapToObj(baseList::get).forEach(i -> i.setFiltered(true));
System.out.println("\nFiltered list now:\n"+list);
}
public static class Item {
private final StringProperty name = new SimpleStringProperty() ;
private final BooleanProperty filtered = new SimpleBooleanProperty() ;
public Item(String name) {
setName(name);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
public final BooleanProperty filteredProperty() {
return this.filtered;
}
public final boolean isFiltered() {
return this.filteredProperty().get();
}
public final void setFiltered(final boolean filtered) {
this.filteredProperty().set(filtered);
}
#Override
public String toString() {
return getName();
}
}
}
If you are using database loading function plus need to filter multiple fields, this solution will help.
ObservableList<PurchaseOrder> poData = FXCollections.observableArrayList();
FilteredList<PurchaseOrder> filteredData;
private void load() {
PurchaseOrder po = new PurchaseOrder();
try {
poData = po.loadTable("purchase_orders", beanFields); // Database loading data
} catch (SQLException ex) {
Logger.getLogger(PurchaseOrdersController.class.getName()).log(Level.SEVERE, null, ex);
}
filteredData = new FilteredList<>(poData, t -> true); //Encapsulate data with filter
poTable.setItems(filteredData); //Load filtered data into table
//Set event trigger for all filter textboxes
txtFilter.textProperty().addListener(obs->{
filter(filteredData);
});
txtFilter2.textProperty().addListener(obs->{
filter(filteredData);
});
}
private void filter(FilteredList<PurchaseOrder> filteredData)
{
filteredData.setPredicate(PurchaseOrder -> {
// If all filters are empty then display all Purchase Orders
if ((txtFilter.getText() == null || txtFilter.getText().isEmpty())
&& (txtFilter2.getText() == null || txtFilter2.getText().isEmpty())) {
return true;
}
// Convert filters to lower case
String lowerCaseFilter = txtFilter.getText().toLowerCase();
String lowerCaseFilter2 = txtFilter2.getText().toLowerCase();
//If fails any given criteria, fail completely
if(txtFilter.getText().length()>0)
if (PurchaseOrder.get("vendor_name").toLowerCase().contains(lowerCaseFilter) == false)
return false;
if(txtFilter2.getText().length()>0)
if (PurchaseOrder.get("PONumber").toLowerCase().contains(lowerCaseFilter2) == false)
return false;
return true; // Matches given criteria
});
}