JavaFX How to get all values of one column from TableView? - javafx

I would like to get all data of one column from my TableView after Button click.
I found this code
TablePosition pos = table.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
// Item here is the table view type:
Item item = table.getItems().get(row);
TableColumn col = pos.getTableColumn();
// this gives the value in the selected cell:
String data = (String) col.getCellObservableValue(item).getValue();
But this is code for selected cell and i have button and all cells of one column.
Can you help me pls?
Thankyou.

Just do the same thing for all elements of table.getItems():
TableColumn<MyDataType, String> column = ... ; // column you want
List<String> columnData = new ArrayList<>();
for (MyDataType item : table.getItems()) {
columnData.add(col.getCellObservableValue(item).getValue());
}
where MyDataType is the data type of the TableView.

Related

How to add after selected row in tableview?

I have selected row, and I need to add some data right below it. Row is selected with OrderTable selectedOrderTable = orderTableId.getSelectionModel().getSelectedItem(); and new one is added with data.add(new OrderTable( ... )) It's adding OK to table, just at bottom of it, and I need to add it right below selected row selectedOrderTable.
Just call the add method taking an index:
int index = orderTableId.getSelectionModel().getSelectedIndex();
data.add(index + 1, new OrderTable(...));

Get selected items of menubutton

I have 3 MenuButtons in my JavaFX-Application, each one filled with CheckMenuItems.
I want to get the Text of every selected Item of the MenuButton. How shall i proceed?
Take all MenuItems, filter selected and map them to String.
MenuButton mb = new MenuButton();
mb.getItems().addAll(new CheckMenuItem("One"), new CheckMenuItem("Two"), new CheckMenuItem("Three"));
mb.getItems().stream().forEach((MenuItem menuItem) -> menuItem.setOnAction(ev -> {
final List<String> selectedItems = mb.getItems().stream()
.filter(item -> CheckMenuItem.class.isInstance(item) && CheckMenuItem.class.cast(item).isSelected())
.map(MenuItem::getText)
.collect(Collectors.toList());
System.out.println(selectedItems);
}));

Java equals string and object from ObservableList

I have a big problem. My code:
TablePosition pos = (TablePosition)
tableView.getSelectionModel().getSelectedCells().get(0);
Object item = tableView.getItems().get(pos.getRow());
Object e = ((List<ObservableList>) item).get(0);
String new_status = "textExample";
and how can I use equals ? new_status.equals(e) ? I don't have any idea on how to convert this.
p.s. in console e = "textExample".
I need this to update a sql row.
If you want to get selected items, you can just use getSelectedItems() (or getSelectedItem() if you only want the latest selection). You don't need to work with selected cells, table positions and rows.
Assuming a TableView<ObservableList<String>> (which you may or may not have). Code to retrieve the value of the first element of the selected row (probably the value represented in the first column), would be as follows:
final String NEW_STATUS = "textExample";
final TableView<ObservableList<String>> tableView = new TableView<>();
final ObservableList<String> selectedItem =
tableView.getSelectionModel().getSelectedItem();
if (selectedItem != null
&& !selectedItem.isEmpty()
&& NEW_STATUS.equals(selectedItem.get(0))) {
// do work here.
}
As your type definitions may be different you may need to adapt the code above to fit your situation as required. If you still can't work it out, you will need to edit the question to provide an mcve to get further assistance.

Get The Row,Column values from a cell in QT

I have a problem, i want to return the selected Rows values, and the columns separately, i found a method to return both of them using the function cell(row, column), but i want to get them separately
Here is my code :
QTableWidgetItem *c = new QTableWidgetItem();
QMap<QString,int> lists;
for(i=0;i<range.rowCount();++i){
for(int j=0;j<range.columnCount();++j){
c=item(i,j);// here i can return the Rows, Columns Data
QMessageBox::information(this,"",c->text());
}
}
As you can see this code is working, but i just want to return the Rows and the Columns separately so i can put them in my QMap<QString,int> list.
And the purpose of all this is to try to draw a piechart from the selected rows and columns
So Any help please
Here is what I understood from the comments, feel free to correct me and I'll update my answer if necessary.
COL1 | COL2
NAME | VALUE
So when you select a cell, you actually care about the whole row, a.k.a the name of the row and the value associated. If this is the case, it would make more sense to only allow the user to select whole rows, instead of cells. setSelectionBehavior(QAbstractItemView::SelectRows); should do the trick.
Provided that the name of the dataset is always in column 1, and the value in column 2, you should update your code with the snippet:
QTableWidgetItem *c; //Deleted memory leak in your code.
QMap<QString,double> myMap; //Don't name it a list if it is explicitly a map.
for(i=0;i<range.rowCount();++i){
QString dataName = item(i,0)->text();
int dataValue;
for(int j=1;j<range.columnCount();++j){
c=item(i,j);// here i can return the Rows, Columns Data
dataValue += c->text().toDouble();
//If you always have 2 columns only, dataValue will be the value you are looking for.
//If you can have more than 2 columns, dataValue will be the sum of all the cells located after the column 0, on the same row.
//Change this depending on how you want to treat those values.
QMessageBox::information(this,dataName,c->text());
}
myMap[dataName]=dataValue;
}
EDIT for QPieSeries, following this example:
QPieSeries *series = new QPieSeries();
QMap<QString,double>::iterator it = myMap.begin();
QMap<QString,double>::iterator end = myMap.end();
for(; it!=end; ++it){
series->append(it->key(), it->value());
}
QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("My Data");
chart->legend()->hide();
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
/*change with your window here*/
yourWindow.setCentralWidget(chartView);

Calculate the sum of a datagrid's rows

I have this datagrid which has columns with a NumericStepper as an itemEditor where the user can change the number of items he wants to buy. How can I calculate the sum of all cells when the user updates datagrid's values?
EDIT 1
My datagrid is about colors and sizes of a product. The columns are generated dynamically from this function
private function getColumn(dataField:String, headerText:String,editable:Boolean) : DataGridColumn
{
var column:DataGridColumn = new DataGridColumn(dataField);
column.headerText = headerText;
column.width = 20;
editable ? column.editable = true : column.editable = false;
column.editorDataField = "value";
column.itemEditor = new ClassFactory(ColorSizesFormRenderer);
return column;
}
and then I loop my xml http result
for each (var thisSize:XML in result.product.(#id == pId)..size) {
_columns.push(getColumn("size"+thisSize,thisSize,true));
}
So how can I find the sum of all cells?
EDIT 2
I use this code to loop all my cells
protected function color_sizes_itemFocusOutHandler(event:DataGridEvent):void
{
total = 0;
for each(var row:Object in color_sizes.dataProvider) {
total += row.size28;
}
}
the row.size28 is generated dynamically. So it could be row.size29,row.size30 etc.
How can I loop through all my cells without knowing their data.property?
Thanks in advance
Your numeric stepper should show a bindable property value from the [data] row you display.
Then you just parse the dataProvider of the datagrid and make the sum of that data.property.
for ex:
class MyObject
{
[Bindable]
var itemsNo:int = 0;
var productName:String = "";
}
The datagrid will have a NumericStepper custom column that will take the value of {data.itemsNo}
At the end just count all itemsNo from the datagrid.dataProvider items

Resources