Trying to figure out on how can i disable the reordering of table columns in javafx 2?
Here's the solution:
tblView.getColumns().addListener(new ListChangeListener() {
#Override
public void onChanged(Change change) {
change.next();
if(change.wasReplaced()) {
tblView.getColumns().clear();
tblView.getColumns().addAll(column1,column2...);
}
}
});
After much waste of time, I've found the following, very simple, solution:
TableHeaderRow header = (TableHeaderRow) myTableView.lookup("TableHeaderRow");
header.setMouseTransparent(true);
Related
I've got a tableview with a column that binds to a SimpleObjectProperty (LocalDate) variable:
tableview(Model) {
column("DATE",Model::date).makeEditable()
...
}
I would like to make it a date picker to allow easy access for the user, something like the following:
tableview(Model) {
column("DATE",Model::date).makeEditable().datepicker()
...
}
Although I'm not sure what the right syntax is, any help would be appreciated!
If your domain object has an id, you can create a cached cell with a datepicker in it like this:
column("DATE", Model::dateProperty) {
cellFormat {
graphic = cache(rowItem.id) {
datepicker(rowItem.dateProperty)
}
}
}
You can also do this without the use of a cache, but performance won't be great for big datasets.
I am using rxjava2 for the first time on an Android project, and am doing SQL queries on a background thread.
However I am having trouble figuring out the best way to do a simple SQL query, and being able to handle the case where the record may or may not exist. Here is the code I am using:
public Observable<Record> createRecordObservable(int id) {
Callable<Record> callback = new Callable<Record>() {
#Override
public Record call() throws Exception {
// do the actual sql stuff, e.g.
// select * from Record where id = ?
return record;
}
};
return Observable.fromCallable(callback).subscribeOn(Schedulers.computation());
}
This works well when there is a record present. But in the case of a non-existent record matching the id, it treats it like an error. Apparently this is because rxjava2 doesn't allow the Callable to return a null.
Obviously I don't really want this. An error should be only if the database failed or something, whereas a empty result is perfectly valid. I read somewhere that one possible solution is wrapping Record in a Java 8 Optional, but my project is not Java 8, and anyway that solution seems a bit ugly.
This is surely such a common, everyday task that I'm sure there must be a simple and easy solution, but I couldn't find one so far. What is the recommended pattern to use here?
Your use case seems appropriate for the RxJava2 new Observable type Maybe, which emit 1 or 0 items.
Maybe.fromCallable will treat returned null as no items emitted.
You can see this discussion regarding nulls with RxJava2, I guess that there is no many choices but using Optional alike in other cases where you need nulls/empty values.
Thanks to #yosriz, I have it working with Maybe. Since I can't put code in comments, I'll post a complete answer here:
Instead of Observable, use Maybe like this:
public Maybe<Record> lookupRecord(int id) {
Callable<Record> callback = new Callable<Record>() {
#Override
public Record call() throws Exception {
// do the actual sql stuff, e.g.
// select * from Record where id = ?
return record;
}
};
return Maybe.fromCallable(callback).subscribeOn(Schedulers.computation());
}
The good thing is the returned record is allowed to be null. To detect which situation occurred in the subscriber, the code is like this:
lookupRecord(id)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Record>() {
#Override
public void accept(Record r) {
// record was loaded OK
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) {
// there was an error
}
}, new Action() {
#Override
public void run() {
// there was an empty result
}
});
I have an applications that hosts a TableView. Whenever the user sorts the rows by clicking the header of a particular column, I need to
Save the current order of items,
Do the actual sorting,
Save the new order of items.
I was able to spot this:
this.tableView.sortPolicyProperty().set(t -> {
System.out.println("saving source order");
... // Saving
FXCollections.sort(tableView.getItems(), t.getComparator());
System.out.println("saving target order");
... // Saving
return true;
});
However, this throws ClassCastException pretty often. Is there a better way of saving the item permutations before and after sorting?
You could listen to it using the ListChangeListener the better way :)
tv.getItems().addListener(new ListChangeListener<T>(){
#Override
public void onChanged(javafx.collections.ListChangeListener.Change<
? extends T> c) {
while(c.next()){
if(c.wasPermutated()){
System.out.println("is permuated");
}
}
}
});
Hope it helps.
I'm using a SmartGWT ListGrid with a DataSource. I'm successfully using a CellFormatter to display numeric file size data as mixed text / data (i.e. "10 GB" rather than 10737418240). I have filtering set up.
What I'd like to do is to let the user filter on the CellFormatter output, rather than on the underlying data. IOW, let the user type "GB" into the filter box, and get all the files with sizes in the GB range. The DataSource is cached locally, so I don't have issues about going back to the server to get data.
Edit: the reason why I'm using a CellFormatter is because it want sorting to be correct, IOW when sorting in increasing order I want 200 KB to come before 10 GB, not after (and in a text sort they're reversed). Sorting is more important to me than filtering, so if I have to have both sorting and filtering target the same representation of the data, I'll just give up on filtering working.
Any help would be greatly appreciated. Thank you,
Greg
You have two options to do this. First is to return already modified values from your datasource, so instead of 10737418240 it should return "10 GB" string value.
The second approach seems better for me - you should use SimpleType functionality. There is an example for you:
public class PopulationType extends SimpleType {
public PopulationType() {
super("population", FieldType.TEXT);
// format values in the grid
this.setSimpleTypeValueExtractor(new SimpleTypeValueExtractor() {
#Override
public Object getAtomicValue(Object value) {
if (value instanceof Integer && ((Integer) value) > 1000000) {
return ((Integer) value) / 1000000 + " Mln";
}
return "" + value;
}
});
}
}
public void onModuleLoad() {
final ListGrid countryGrid = new ListGrid();
countryGrid.setWidth100();
countryGrid.setHeight100();
countryGrid.setAutoFetchData(true);
countryGrid.setShowFilterEditor(true);
countryGrid.setShowAllRecords(true);
WorldXmlDS ds = WorldXmlDS.getInstance();
ds.getField("population").setType(new PopulationType());
countryGrid.setDataSource(ds);
countryGrid.draw();
}
You set your SimpleType instance to a field you want to format and set SimpleTypeValueExtractor to override getAtomicValue which is used for showing,filtering,sorting.
There are other methods you could override - e.g. if you need to edit values in your grid you should probably set SimpleTypeValueUpdater as well.
I'm using Telerik OpenAccess and SQL Server on a project and I need to be able to search by what someone's age will be on a certain date. The problem that I am running into is that the person's date of birth is stored in one table and the date to compare to is in another table, which prevents me from using a computed column. They are, however, joined together so that I can calculate the age by creating my own non-persistent property in the partial class like so:
public partial class Student
{
[Telerik.OpenAccess.Transient]
private int? _ageUponArrival;
public virtual int? AgeUponArrival
{
get
{
try
{
var dob = DateTime.Parse(this.StudentProfiles.First().Person.YearOfBirth);
var programStart = (DateTime)(this.StudentPrograms.First().ProgramStart);
this._ageUponArrival = programStart.Year - dob.Year;
if (dob > programStart.AddYears(-(int)(this._ageUponArrival)))
{
(this._ageUponArrival)--;
}
}
catch (Exception e)
{
this._ageUponArrival = null;
}
return _ageUponArrival;
}
set { }
}
}
Please ignore how bad the tables are set up, it's something that I inherited and can't change at this point. The problem with this approach is that the property is not available to search on with Linq. I know that I could create a view that would do this for me, but I would much rather not have to maintain a view just for this. Is there any way at all to create a calculated property through Telerik that would be calculated on the db server in such a way as to be searchable?
It appears that this is not possible at this point. http://www.telerik.com/community/forums/orm/linq-questions/dynamic-query-with-extended-field.aspx