Подскажите в чем может быть проблема, кучу времени уже сижу втыкаю: есть обычный TableView с редактируемыми ячейками + в классе, который реализует само редактирование, есть функция, создающая TextField, на который повешан листенер, для того, что бы при потере фокуса вызывался setOnEditCommit ячейки.
Проблема в том, что он не вызывается.
Кроме него есть листенеры на нажатие Enter'а Escape - тут все ок.
Под спойлером код, подскажите пожалуйста, в чем проблема :(
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380. 381. 382.
package javafxapplication6;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Callback;
public class FXMLDocumentController
implements Initializable {
@FXML private TableView<MyTable> table;
@FXML private TableColumn<MyTable,Integer> id;
@FXML private TableColumn<MyTable,Integer> razdel;
@FXML private TableColumn<MyTable,Boolean> razdelItog;
@FXML private TableColumn<MyTable,Boolean> razdelItog1;
@FXML private TableColumn<MyTable,String> name;
@FXML private TableColumn<MyTable,String> pIndex;
@FXML private TableColumn<MyTable,Integer> count;
@FXML private TableColumn<MyTable,Integer> priceUsd;
@FXML private TableColumn<MyTable,Integer> priceRur;
@FXML private TableColumn<MyTable,Boolean> arnd;
@FXML private TextField usd;
private final List<MyTable> list = new LinkedList();
private final ObservableList<MyTable> tableData = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
id.setCellValueFactory(new PropertyValueFactory<MyTable,Integer>("id"));
razdel.setCellValueFactory(new PropertyValueFactory<MyTable,Integer>("razdel"));
razdelItog.setCellValueFactory(new PropertyValueFactory<MyTable,Boolean>("razdelItog"));
razdelItog1.setCellValueFactory(new PropertyValueFactory<MyTable,Boolean>("razdelItog1"));
name.setCellValueFactory(new PropertyValueFactory<MyTable,String>("name"));
pIndex.setCellValueFactory(new PropertyValueFactory<MyTable,String>("pIndex"));
count.setCellValueFactory(new PropertyValueFactory<MyTable,Integer>("count"));
priceUsd.setCellValueFactory(new PropertyValueFactory<MyTable,Integer>("priceUsd"));
priceRur.setCellValueFactory(new PropertyValueFactory<MyTable,Integer>("priceRur"));
arnd.setCellValueFactory(new PropertyValueFactory<MyTable,Boolean>("arnd"));
Callback<TableColumn<MyTable, Integer>, TableCell<MyTable, Integer>> editingIntegerCellFactory =
new Callback<TableColumn<MyTable, Integer>, TableCell<MyTable, Integer>>() {
@Override
public TableCell call(TableColumn p) {
return new EditingIntegerCell();
}
};
Callback<TableColumn<MyTable, Boolean>, TableCell<MyTable, Boolean>> checkBoxCellFactory =
new Callback<TableColumn<MyTable, Boolean>, TableCell<MyTable, Boolean>>() {
@Override
public TableCell call(TableColumn<MyTable, Boolean> p) {
return new CheckBoxTableCell();
}
};
Callback<TableView<MyTable>,TableRow<MyTable>> stylingTableRow =
new Callback<TableView<MyTable>,TableRow<MyTable>>() {
@Override
public TableRow<MyTable> call(TableView<MyTable> p) {
return new StylingTableRow();
}
};
EventHandler<TableColumn.CellEditEvent<MyTable, Integer>> calcPrice =
new EventHandler<TableColumn.CellEditEvent<MyTable, Integer>>() {
@Override
public void handle(TableColumn.CellEditEvent<MyTable, Integer> t) {
MyTable row = t.getTableView().getItems().get(t.getTablePosition().getRow());
System.out.println("EventHandler");
switch(t.getTableColumn().getId()){
case "countColumn":
row.setCount(t.getNewValue());
break;
case "priceUsdColumn":
row.setPriceUsd(t.getNewValue());
break;
default:
break;
}
row.setPriceRur(row.getCount() * (row.getPriceUsd() * Integer.parseInt(usd.getText())));
table.getColumns().get(8).setVisible(false);
table.getColumns().get(8).setVisible(true);
}
};
table.setRowFactory(stylingTableRow);
count.setCellFactory(editingIntegerCellFactory);
count.setOnEditCommit(calcPrice);
priceUsd.setCellFactory(editingIntegerCellFactory);
priceUsd.setOnEditCommit(calcPrice);
arnd.setCellFactory(checkBoxCellFactory);
tableData.add(new MyTable(1, 1,false,false,"Dldfjkdfjkdfg","a",0,300,0,false));
tableData.add(new MyTable(2, 1,false,false,"sgfds fds g","f",0,300,0,false));
tableData.add(new MyTable(3, 1,false,false,"adfWEFRGD S","x",0,300,0,false));
tableData.add(new MyTable(4, 1,false,false,"sfdhf jj k","x",0,300,0,false));
tableData.add(new MyTable(5, 1,false,false,"xc xvb cvn c","x",0,300,0,false));
tableData.add(new MyTable(6, 1,false,false,"yttyui ru uy ","x",0,300,0,false));
tableData.add(new MyTable(7, 1,false,false,"reh b ru uy ","x",0,300,0,false));
tableData.add(new MyTable(8, 1,false,false,"ewqewqe we ru uy ","x",0,300,0,false));
tableData.add(new MyTable(0, 1,true,false,"Итого:","x",0,0,0,false));
tableData.add(new MyTable(9, 2,false,false,"wev e g","x",0,300,0,false));
tableData.add(new MyTable(10, 2,false,false,"thrhf hg ","x",0,300,0,false));
tableData.add(new MyTable(11, 2,false,false,"dsa ds f","x",0,300,0,false));
tableData.add(new MyTable(12, 2,false,false,"D fd sfg","x",0,300,0,false));
tableData.add(new MyTable(13, 2,false,false,"sd hf fd j","x",0,300,0,false));
tableData.add(new MyTable(14, 2,false,false,"d fhd fgh ","x",0,300,0,false));
tableData.add(new MyTable(15, 2,false,false,"qw esf fg","x",0,300,0,false));
tableData.add(new MyTable(0, 2,true,false,"Итого:","x",0,0,0,false));
tableData.add(new MyTable(0, 2,false,true,"Итого по оборудованию:","x",0,0,0,false));
tableData.add(new MyTable(16, 3,false,false,"D fd sfg","f",0,300,0,false));
tableData.add(new MyTable(17, 3,false,false,"sd hf fd j","a",0,300,0,false));
tableData.add(new MyTable(18, 3,false,false,"d fhd fgh ","f",0,300,0,false));
tableData.add(new MyTable(0, 3,true,false,"Итого:","x",0,0,0,false));
table.setItems(tableData);
}
public class MyTable {
private final SimpleIntegerProperty id;
private final SimpleIntegerProperty razdel;
private final SimpleBooleanProperty razdelItog;
private final SimpleBooleanProperty razdelItog1;
private final SimpleStringProperty name;
private final SimpleStringProperty pIndex;
private final SimpleIntegerProperty count;
private final SimpleIntegerProperty priceUsd;
private final SimpleIntegerProperty priceRur;
private final SimpleBooleanProperty arnd;
public MyTable(Integer _id,Integer _razdel,Boolean _razdelItog,Boolean _razdelItog1,String _name,String _pIndex,Integer _count,Integer _priceUsd,Integer _priceRur,Boolean _arnd) {
this.id = new SimpleIntegerProperty(_id);
this.razdel = new SimpleIntegerProperty(_razdel);
this.razdelItog = new SimpleBooleanProperty(_razdelItog);
this.razdelItog1 = new SimpleBooleanProperty(_razdelItog1);
this.name = new SimpleStringProperty(_name);
this.pIndex = new SimpleStringProperty(_pIndex);
this.count = new SimpleIntegerProperty(_count);
this.priceRur = new SimpleIntegerProperty(_priceRur);
this.priceUsd = new SimpleIntegerProperty(_priceUsd);
this.arnd = new SimpleBooleanProperty(_arnd);
}
public Integer getId() { return id.get(); }
public void setId(Integer _id) { id.set(_id); }
public Integer getRazdel() { return razdel.get(); }
public void setRazdel(Integer _razdel) { razdel.set(_razdel); }
public Boolean getRazdelItog() { return razdelItog.get(); }
public void setRazdelItog(Boolean _razdelItog) { razdelItog.set(_razdelItog); }
public Boolean getRazdelItog1() { return razdelItog1.get(); }
public void setRazdelItog1(Boolean _razdelItog1) { razdelItog1.set(_razdelItog1); }
public String getName() { return name.get(); }
public void setName(String _name) { name.set(_name); }
public String getPIndex() { return pIndex.get(); }
public void setPIndex(String _pIndex) { pIndex.set(_pIndex); }
public Integer getCount() { return count.get(); }
public void setCount(Integer _count) { count.set(_count); }
public Integer getPriceUsd() { return priceUsd.get(); }
public void setPriceUsd(Integer _priceUsd) { priceUsd.set(_priceUsd); }
public Integer getPriceRur() { return priceRur.get(); }
public void setPriceRur(Integer _priceRur) { priceRur.set(_priceRur); }
public Boolean getArnd() { return arnd.get(); }
public void setArnd(Boolean _arnd) { arnd.set(_arnd); }
}
class EditingIntegerCell
extends TableCell<MyTable, Integer> {
private TextField textField;
@Override
public void startEdit() {
if (!isEmpty()) {
MyTable row = getTableView().getItems().get(getTableRow().getIndex());
if(!row.getId().equals(0) & !row.getPIndex().equals("f") & !row.getPIndex().equals("a")) {
super.startEdit();
createTextField();
setText(null);
setGraphic(textField);
Platform.runLater(new Runnable(){
@Override
public void run() {
textField.selectAll();
textField.requestFocus();
}
});
}
}
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText(String.valueOf(getItem()));
setGraphic(null);
}
@Override
public void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
}
else {
if (isEditing()) {
if (textField != null) {
textField.setText(getItem() == null ? "" : getItem().toString());
}
setText(null);
setGraphic(textField);
}
else {
setText(getItem() == null ? "" : getItem().toString());
setGraphic(null);
}
}
}
private void createTextField() {
textField = new TextField(getItem() == null ? "" : getItem().toString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
textField.focusedProperty().addListener(new ChangeListener<Boolean>(){
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
if (!arg2) {
if (textField.getText() != null) { commitEdit(Integer.parseInt(textField.getText())); }
}
}
});
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
if (textField.getText() != null) { commitEdit(Integer.parseInt(textField.getText())); }
else { cancelEdit(); }
}
else if (t.getCode() == KeyCode.ESCAPE) { cancelEdit(); }
}
});
}
}
class CheckBoxTableCell
extends TableCell<MyTable, Boolean> {
private final CheckBox checkBox;
public CheckBoxTableCell() {
checkBox = new CheckBox();
checkBox.setFocusTraversable(false);
checkBox.setCursor(Cursor.HAND);
checkBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
MyTable row = getTableView().getItems().get(getTableRow().getIndex());
if(checkBox.isSelected()) {
list.remove(row);
row.setArnd(true);
}
else {
list.add(row);
row.setArnd(false);
}
}
});
setGraphic(checkBox);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setEditable(true);
}
@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if(!empty & item != null) {
MyTable row = getTableView().getItems().get(getTableRow().getIndex());
if((!row.getRazdel().equals(1) & !row.getRazdel().equals(2)) | row.getId().equals(0)) {
checkBox.setVisible(false);
}
else {
checkBox.setVisible(true);
}
checkBox.setAlignment(Pos.CENTER);
checkBox.setSelected(item);
setAlignment(Pos.CENTER);
setGraphic(checkBox);
}
}
}
class StylingTableRow
extends TableRow<MyTable> {
private final TableRow<MyTable> row;
StylingTableRow() {
this.row = new TableRow();
}
@Override
protected void updateItem(MyTable item, boolean empty) {
super.updateItem(item, empty);
if(!isEmpty()) {
switch (getItem().getId()) {
case 0:
getStyleClass().add("itogoTableRow");
break;
default:
getStyleClass().removeAll("itogoTableRow");
break;
}
switch (getItem().getPIndex()) {
case "f":
getStyleClass().add("pIndexTableRow");
getStyleClass().removeAll("pIndex1TableRow");
break;
case "a":
getStyleClass().add("pIndex1TableRow");
getStyleClass().removeAll("pIndexTableRow");
break;
default:
getStyleClass().removeAll("pIndexTableRow");
getStyleClass().removeAll("pIndex1TableRow");
break;
}
}
}
}
}
|