i have create a TableView with Columns and fill it with my sql database table data.
my qz is :
when i change the cell value in column 0 to XXX i want to cell value in column 1 to change with YYYY value
how can i get the cell 1 index to insert the new value after edit commit cell 0 >?
I had a similar "problem".
TablePosition pos = tableView.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
int col = pos.getColumn();
TableColumn column = pos.getTableColumn();
Object o = column.getCellData(row); //getting the Value from the specific Cell
I used this method, after double clicking a cell. You have to modfiy this solution that it fits to your problem, but it should help you.
To edit the specific cell i used the method setCellFactory from the TableColumn class.
column.setCellFactory(factory -> new TableCell<ObservableList<Object>, Object>() {
#Override
protected void updateItem(Object item, boolean empty) {
//Do something
}
});
At least at the end and after a week of searching for an answer, I found a solution and it is:
TablePosition TPOS = (TablePosition) table.getSelectionModel().getSelectedCells().get(5);
int rowIndex = TPOS.getRow();
int colIndex = TPOS.getColumn();
TableColumn COLP = (TableColumn) table.getColumns().get(5);
TableCell cell = (TableCell)table.queryAccessibleAttribute(AccessibleAttribute.CELL_AT_ROW_COLUMN,rowIndex,colIndex);
cell.setText(String.valueOf(getPrice()));
// getPrice() is the return method to find the price of edited item cell value
Related
I currently have two tableviews in one screen, which results in both TableViews have rows which the user can select.
Now I want only one row to be selected at the same time (doesn't matter which TableView it is selected from). I was thinking about some kind of listener which deselects the other row when a row is selected. This is my initial setup:
Step 1
Search for a way to bind a method to the selection of a row (there is not something like tableview.setOnRowSelected(method))
Step 2
Create the method which acts like a kind of listener: when a row is selected, deselect the other row (I know how to do this part)
Class1 selectedObject1 = (Class1)tableview1.getSelectionModel().getSelectedItem();
Class2 selectedObject2 = (Class2)tableview2.getSelectionModel().getSelectedItem();
if(selectedObject1 != null && selectedObject2 != null) {
tableview1.getSelectionModel().clearSelection();
}
So, step one is the problem. I was thinking of an observable list on which a listener can be created, and then add the selected row to the list. When this happens, the listener can call the method.
Anyone any clue how to make this?
Any help is greatly appreciated.
The selectedItem in the selection model is an observable property, so you should be able to achieve this with:
tableview1.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
if (newSelection != null) {
tableview2.getSelectionModel().clearSelection();
}
});
tableview2.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
if (newSelection != null) {
tableview1.getSelectionModel().clearSelection();
}
});
My solution would be creating custom cell factory for table and set it for each table columns.
Callback<TableColumn<..., ...>, TableCell<..., ...>> value = param -> {
TextFieldTableCell cell = new TextFieldTableCell<>();
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
//your code
}
);
return cell;
};
packageName.setCellFactory(value);
table1.column1.setCellFactory();
table2.column1.setCellFactory();
...
I use it for deleting the chosen row.
public void ButtonClicked()
{
ObservableList<Names> row , allRows;
allRows = table.getItems();
row = table.getSelectionModel().getSelectedItems();
row.forEach(allRows::remove);
}
This question helped me but during experiment in javafx and jfoenix this also works for me.
deleteSingle.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
StringProperty selectedItem = table.getSelectionModel().getSelectedItem().getValue().link1;
System.out.println("That is selected item : "+selectedItem);
if (selectedItem.equals(null)) {
System.out.println(" No item selected");
} else {
System.out.println("Index to be deleted:" + selectedItem.getValue());
//Here was my database data retrieving and selectd
// item deleted and then table refresh
table.refresh();
return;
}
});
In case you need not only the row, but the x|y position of the table cell, do this:
table.getFocusModel().focusedCellProperty().addListener(
new ChangeListener<TablePosition>() {
#Override
public void changed(ObservableValue<? extends TablePosition> observable,
TablePosition oldPos, TablePosition pos) {
int row = pos.getRow();
int column = pos.getColumn();
String selectedValue = "";
if (table.getItems().size() > row
&& table.getItems().get(row).size() > column) {
selectedValue = table.getItems().get(row).get(column);
}
label.setText(selectedValue);
}
});
In this example, I am using a "classic" TableView with List<String> as column model. And, of course, that label is just an example from my code.
I have a qtablewidget with one column with a widget and others with data. The only column with widget is shown and all other columns are hidden.
foreach (BillHeader *billHeader, billHeaderList)
{
m_pBillTable->insertRow(i);
itemWidget = new LookupItem;
itemWidget->setImage(1);
...
m_pBillTable->setCellWidget(i, 0, itemWidget);
tableItem = new QTableWidgetItem(billHeader->billNumber);
tableItem->setTextAlignment(Qt::AlignCenter);
m_pBillTable->setItem(i, 1, tableItem);
...
m_pBillTable->hideColumn(1);
...
I have a signal slot connected as below:
connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(handleOkClick()));
when ok button click i try to get the selected item and get data from the widget set to it
void OrderLookup::handleOkClick()
{
qDebug()<<Q_FUNC_INFO<<"Invoked";
QList<QTableWidgetItem*> itemList = m_pBillTable->selectedItems();
qDebug()<<Q_FUNC_INFO<<itemList.count();
if (!itemList.isEmpty())
{
int row = itemList.at(0)->row();
qDebug()<<Q_FUNC_INFO<<row;
LookupItem *item = (LookupItem*)m_pBillTable->cellWidget(row, 0);
if (NULL != item)
{
QString billNumber = item->getBillNumber();
emit orderLookupComplete(billNumber);
accept();
}
}
qDebug()<<Q_FUNC_INFO<<"Exits";
}
But i am getting the list count as zero.
The row is getting selected and gets highlighted.
I’ve set some properties to table widget as below:
m_pBillTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_pBillTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pBillTable->setSelectionMode(QAbstractItemView::SingleSelection);
m_pBillTable->setFocusPolicy(Qt::NoFocus);
Please can someone help me to know why list count is empty..
The issue got solved ..
QItemSelectionModel *itemModel = m_pBillTable->selectionModel();
QModelIndexList indexList = itemModel->selectedRows();
qDebug()<<Q_FUNC_INFO<<"IndexList Count"<<indexList.count();
if (!indexList.isEmpty())
{
int row = indexList.at(0).row();
I have a tableview that has columns of types (SimpleStringProperty, SimpleIntegerProperty)
STRING1 STRING2 INTEGER1 INTEGER2
####### ####### ######## ########
a b 9 10
a c 9 12
b d 0 0
Now, in the 3rd row, the 3rd and 4th column have values as 0. The type of which is SimpleIntegerProperty.
I would like it if the 0s don't show up in the table and the cell appears empty.
Can you please advice on how i may do this?
P.S: I am using property listeners for making an editable table.
Strings are initialized to null and hence the table is blank but integers are getting initialized to 0.
I think you have to extend from ListCell and override the updateItem method.
In that method you have to check if the SimpleIntegerProperty equals 0 and probably call setText("") then.
You have to provide a TableCellFactory which returns your customized TableCell-Object. In the TableCell's updateItem()-method you can call setText() as suggested by Lukas Leitinger.
TableColumn columnInteger2 = new TableColumn("Integer2");
columnInteger2.setCellValueFactory(
new PropertyValueFactory<MyVo,String>("fieldInteger2"));
columnInteger2.setCellFactory(new Callback<TableColumn, TableCell>() {
#Override
public TableCell call(TableColumn tableColumn) {
return new TableCell<String, Integer>(){
#Override
protected void updateItem(Integer integer, boolean b) {
if (integer != null) {
super.setText(integer == 0?"":integer.toString());
}
}
};
}
});
I have a form that has 4 TextFields which I'm trying to track with an ObservableList that has 5 columns. The TableView has an extra column to hold a calculated value (the 5th column in my ObservableList).
The data is dumping fine from the 4 TextFields, but the calculated column comes out blank. I assume this is a problem with my getters and setters, because the value is calculated before I pass it to my data model, AND I just tested the data model, and it is GETTING the value (passed as a parameter).
To not put extraneous code here, I think these are the relevant parts:
// This is (part of) my data model
public static class ItemSale {
private ItemSale (Double barC, String itemm, Double pricee,
Integer quant, Double totsP) {
this.barCode = new SimpleDoubleProperty(barC);
this.item = new SimpleStringProperty(itemm);
this.price = new SimpleDoubleProperty(pricee);
this.quantity = new SimpleIntegerProperty(quant);
this.rowPrice = new SimpleDoubleProperty(totsP);
System.out.println(totsP); // this (also) prints the correct value to the screen
// price * quantity = rowPrice, the calculated value that doesn't show up later
// getter & setter for quantity (works, is a textfield in my form)
public SimpleIntegerProperty getQuantity() {
return quantity;
}
public void setQuantity(Integer quant) {
quantity.set(quant);
}
// getter & setter for rowPrice (doesn't work, is calculated, see below)
public SimpleDoubleProperty getRowPrice(Double totsP) {
return rowPrice;
}
public void setRowPrice(Double totsP) {
rowPrice.set(totsP);
}
// in the Add button action handler, I have this:
Double rowPP;
rowPP = qua * pr; //qua = variable for quantity, pr = variable for price
System.out.println(rowPP); //prints to screen fine
data.add(new ItemSale(
bcode,
item.getText(),
pr,
qua,
rowPP
));
Ooops...got it figured out. I was researching another problem and found the answer on JavaFx - How to display SimpleStringProperty value in TableView and, in going through my code, I noticed I had the getter with parameters. Removed the parameters and BOOM! it worked.
I've three lookupedit repositories (for three columns) in a devexpress xtragrid control. I want to bind lookupedit2 based on a value selected from lookupedit1 and lookupedit3 based on lookupedit2. I can only see the populated elements when I click on the lookupedit. However, it doesn't display the selected element on the grid.
Here is the code i used:
void repositoryLookupEdit1_EditValueChanged(object sender, EventArgs e) {
LookUpEdit edit = gridView.ActiveEditor as LookUpEdit;
int val = Convert.ToInt32(edit.EditValue);
if (!val.Equals(0)) {
var elements = from e in dc.Elements select e;
repositoryLookupEdit1.DisplayMember = "paymentType";
repositoryLookupEdit1.ValueMember = "paymentTypeID";
repZone.DataSource = bindData(elements);
}
}
public BindingSource bindData(object obj) {
BindingSource ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
What could be the possible problem?
I believe this example will be helpful:
How to filter a second LookUp column based on a first LookUp column's value