No content in javaFX tableview - javafx

So I checked the other No content in TableView but it was no help.
I have a database named ledger and I want to bring my transactions into view.
void buildData(){
final ObservableList<ObservableList<String>> data = null
try{
//ResultSet
ResultSet rs = sql.getTransactions(account.getValue().toString())
/**********************************
* TABLE COLUMN ADDED DYNAMICALLY *
**********************************/
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1))
col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString())
}
})
budgetTable.getColumns().addAll(col)
}
/********************************
* Data added to ObservableList *
********************************/
while(rs.next()){
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList()
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
//Iterate Column
row.add(rs.getString(i))
}
println("Row [1] added "+row )
data?.add(row)
}
//FINALLY ADDED TO TableView
budgetTable.setItems(data)
}catch(Exception e){
e.printStackTrace()
System.out.println("Error on Building Data")
}
}
I have 5 columns in the database that do comeback and are added to the tableview. These are date, from_account, to_account, amount & notes:
mysql> show columns from ledger;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| version | bigint(20) | NO | | 1 | |
| date | datetime | NO | | NULL | |
| notes | varchar(35) | NO | | NULL | |
| amount | double | NO | | NULL | |
| from_account | varchar(19) | NO | | NULL | |
| to_account | varchar(55) | YES | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
7 rows in set (0.45 sec)
I get no error or otherwise I would have a very good chance of solving it. At this point I don't know what the problem is. Just says "No content in table" upon build. The file is a groovy file so that's why it looks like python syntax.
Thank you in advance for your help, time and insights!
Be well!

I'm not very familiar with Groovy but I believe I know what the issue is. First off, you declare data as final and then assign null to it.
final ObservableList<ObservableList<String>> data = null;
This means it will be null when to go to set the items of the TableView. Basically, you're calling budgetTable.setItems(null). You would not normally reach this point because calling data.add(row) would throw a NullPointerException; except you don't use data.add(row) but rather data?.add(row). The ? here is the safe navigation operator.
The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return null instead of throwing an exception...
See this question for more.
Given all this, simply changing:
final ObservableList<ObservableList<String>> data = null;
To:
final ObservableList<ObservableList<String>> data = FXCollections.observableArrayList();
Should solve your problem.

Related

Update foreign key in Qt QSqlRelationalTableModel

I'm coding in python (PySide2), but the overall concept concerns the Qt Framework.
Here are two tables of mine:
Table "recordings":
| Column | Type |
| -------- | -------------------|
| id | int (primary key) |
| param1 | int |
| param2 | int |
| ... | int |
| paramN | int |
Table "analyzed_recs":
| Column | Type |
| -------- | -------------------|
| id | int (primary key) |
| rec_id | int (foreign key) | <-- Points to recordings.id
| paramN | int |
I need in my program to display param1 and param2 from the former. In Qt I used a QSqlRelationalTable to fulfill this objective:
def _init_db_models(self):
self.analyzed_recs_sql_model = QSqlTableModel(self, self.db)
self.analyzed_recs_sql_model.setTable("analyzed_recs")
rec_id = self.analyzed_recs_sql_model.fieldIndex('rec_id')
self.analyzed_recs_sql_model.setRelation(rec_id, QSqlRelation("recordings", "id", "param1"))
self.analyzed_recs_sql_model.setRelation(rec_id, QSqlRelation("recordings", "id", "param2"))
self.analyzed_recs_sql_model.select()
self.analyzed_data_table.setModel(self.analyzed_recs_sql_model)
This code works fine in displaying the desired fields.
However, when it comes to update a record in analyzed_recs:
record = self.analyzed_recs_sql_model.record()
record.remove(record.indexOf("id"))
record.setValue("rec_id", self.current_rec_id)
record.setValue("param1", self.param1)
record.setValue("param2", param2)
self.analyzed_recs_sql_model.insertRecord(-1, record)
self.analyzed_recs_sql_model.submitAll()
The column rec_id is not set (NULL) into the table (the other params are correctly inserted into the table).
On the contrary, if I avoid using QSqlRelationalTableModel and take QSqlTableModel instead, the insertion is performed correctly (as I expected), but I lose the INNER JOIN display feature.
I was thinking as a work around to create two distinct models, a QSqlRelationalTableModel only for displaying and a QSqlTableModel only for editing the data. However I don't like the extra workload of syncing the two.
I'm sure there is a Qt feature to achieve this, but unfortunately I'm missing it.
Any suggestion?
I've had the same problem using PYQT.
The record object returned by calling record() method has no fields named 'rec_id' because the QSqlRelationalTableModel changes it with the referenced field name 'param1'. We can verify the field names using:
fieldcount = record.count()
for i in range(fieldcount):
logging.info("field %s %s", i, record.fieldName(i))
so we need to add the field before assigning it:
record = self.analyzed_recs_sql_model.record()
record.remove(record.indexOf("id"))
record.append(QSqlField("rec_id"))
record.setValue("rec_id", self.current_rec_id)

SQLite sort by position in a String [] , when that String [] is a field

I have a table like below in Room, in a Android application, I use Raw Query to get data. Can it be sorted by second value in array sorting_field?
---------------------------------------------
| id | other_fields | sorting_field |
---------------------------------------------
| 1001 | … | ["24","0.02","2"] |
---------------------------------------------
Initially I did the sorting part in Repository with Transformations.switchMap, inside the function a MutableLiveData> and applied Collections.sort.
It worked like a charm:
Collections.sort(list, (o1, o2) -> Double.compare(Double.valueOf(o1.sorting_field().get(positionInList)), Double.valueOf(o2.sorting_field().get(positionInList))));
After Paging implementation, I took the sorting logic out, moved to queries builder and here I am.

How to write Kusto query to get results in one table?

I have 2 KQL queries and I want to combine them in order to display two rows as one result. Not just result of first query, then result of second query:
R_CL
| where isnotempty(SrcIP_s)
| project Message
| take 1;
R_CL
| where isempty(SrcIP_s)
| project Message
| take 1
See sample R_L below.I would like to see 2 rows as result, one with SrcIP_s not empty, and the second with SrcIP_s empty (in this case it will be always same one)
let R_CL = datatable ( SrcIP_s:string, Message:string)
["1.1.1.1" ,"one",
"" ,"two",
"2.2.2.2","three",
"3.3.3.3","four"];
R_CL
| project SrcIP_s, Message
A simple solution for this would be to use the union operator like this:
let query1 = R_CL
| where isnotempty(SrcIP_s)
| project Message
| take 1;
let query2 = R_CL
| where isempty(SrcIP_s)
| project Message
| take 1;
query1
| union query2;
I know this is an old request - but here's a sample query using views and a union for your single query:
Your two separate queries...
R_CL
| where isnotempty(SrcIP_s)
| project Message
| take 1;
R_CL
| where isempty(SrcIP_s)
| project Message
| take 1
would become:
let Query1 = view () {
R_CL
| where isnotempty(SrcIP_s)
| project Message
| take 1;
};
let Query2 = view () {
R_CL
| where isempty(SrcIP_s)
| project Message
| take 1
};
union withsource="TempTableName" Query1, Query2

What data type is returned by this LocalStorage command?

There is an LocalStorage example in the Qt documentation
function findGreetings() {
var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000);
db.transaction(
function(tx) {
// Some other commands
// Show all added greetings
var rs = tx.executeSql('SELECT * FROM Greeting');
}
)
}
What's the data type of rs?
See the Quick Local Storage QML module documentation:
results = tx.executeSql(statement, values)
This method executes a SQL statement, binding the list of values to
SQL positional parameters ("?").
It returns a results object, with the following properties:
| Type | Property | Value | Applicability |
-----------------------------------------------------------------------------------------
| int | rows.length | The number of rows in the result | SELECT |
-----------------------------------------------------------------------------------------
| var | rows.item(i) | Function that returns row i of the result | SELECT |
-----------------------------------------------------------------------------------------
| int | rowsAffected | The number of rows affected by a modification | UPDATE,DELETE |
-----------------------------------------------------------------------------------------
| string | insertId | The id of the row inserted | INSERT |
results = tx.executeSql(statement, values)
This method executes a SQL statement, binding the list of values to SQL positional parameters ("?").
It returns a results object, with the following properties: link
If all you want is to know the type of returned object, just do:
var rs = tx.executeSql(...);
console.log(rs);
qml: [object Object]

JavaFX: Disable multiple rows in TableView based on other TableView

I'm creating an application in JavaFx. Right now I have two tableviews next to each other:
------------------------------------------------------------------------------
| TableView 1 | TableView 2 |
| | |
| | Entry 1 |
| | Entry 2 |
| | Entry 3 |
| | Entry ... |
| | Entry N |
------------------------------------------------------------------------------
I would like to copy items from TableView 2 to TableView 1, but at the same time, the entries that have been copied from TableView 2 need to be disabled (disable the row with setDisable or something similar). I do know how to copy the items from one tableview to another. The problem is that I do not know how to disable multiple rows when one or multiple entries have been copied to TableView 1.
I tried this with a RowFactory, like this:
productsInTransaction.setRowFactory(tv -> {
TableRow<Product> row = new TableRow<>();
row.disableProperty().bind(???);
return row;
});
Any help is much appreciated!
I'm not quite sure of the logic you're wanting, but if your row factory is attached to table 1, and you are disabling the row when the item is present in table 2, do:
row.disableProperty().bind(Bindings.createBooleanBinding(() ->
table2.getItems().contains(row.getItem()), table2.getItems(), row.itemProperty()));

Resources