How to write data in table?
I have class Database which contains fieldLong id in database I have Map<Long, Product> where I want to store my products.
I also have class Product which contains fields String name; BigDecimal price; Category category;//category is Enum.
And in class Controller:
#FXML
private TableView<Map> tableWithProductsId;
#FXML
private TableColumn<Map, String> nameColumn;
#FXML
private TableColumn<Map, Long> productIdColumn;
#FXML
void initialize() {
BigDecimal price = new BigDecimal("0.50");
BigDecimal price1 = new BigDecimal("5.25");
BigDecimal price2 = new BigDecimal("0.79");
Product product = new Product("Apple", price, Category.FRUIT);
Product product1 = new Product("Mango", price2, Category.FRUIT);
Product product2 = new Product("Potatoe", price1, Category.VEGETABLE);
Database database = new Database();
database.saveProduct(product);
database.saveProduct(product1);
database.saveProduct(product2);
Map<Long, Product> allProductsFromDatabase = database.getAllProducts();
ObservableList<Map> listOfProducts = FXCollections.observableArrayList(allProductsFromDatabase);
tableWithProductsId.setItems(listOfProducts);
nameColumn.setCellValueFactory(new MapValueFactory<>("name"));//this not works
//how to setCellValueFactory
I tried this link also:docs.oracle.MapValueFactory but this won't work for me.
I want to achieve something like this:
Where productId I can get from class Database; with method getProductId(); and other fields I can get from class Product; with methods getProductName();... maybe I need to change my tableView field on #FXML private TableView<Map<Long, Product>> tableWithProductsId;
? and table columns also need to be changed on #FXML private TableColumn<Map<Long, Product>, String> nameColumn;? I don't understand how to do this correctly.
Related
I have two controllers, one controller has a tableview that lists all members and the other controller allows a member to be added. I am having trouble getting the table to update when a member is added. I use two models that are already given to me, so I don't change anything in the models.
Each member has an id and a profession, I use cellValueFactory for listing the members. I tried adding listeners to the cellValueFactoryProperties like this:
idColumn.cellValueFactoryProperty().addListener((obs, oldV, newV) -> memberTv.refresh());
But it still doesn't show new members
The Controller with the TableView:
#FXML
TableColumn<Member, Integer> idColumn;
#FXML
TableColumn<Member, String> profColumn;
#FXML
TableView<Member> memberTv;
#FXML
private void initialize() {
memberTv.setItems(getClub().getMembers());
idColumn.setCellValueFactory(cellData -> new
ReadOnlyObjectWrapper(cellData.getValue().getId()));
profColumn.setCellValueFactory(cellData -> new
ReadOnlyStringWrapper(cellData.getValue().getProf()));
}
public final Club getClub() {
\\\returns Club model
}
The Controller that adds members
private int getId() {
return Integer.parseInt(idTf.getText());
}
private void setId(String id) {
idTf.setText(id);
}
private String getProf() {
return profTf.getText();
}
private void setProf(String prof) {
profTf.setText("" + prof);
}
#FXML
public void handleAddMember(){
getClub().addMember(getId(), getProf());
}
When I add a member I want the TableView to show the added member, but it only shows members already added in the model.
I figured it out, I had a ClubController that sets the stage based after a menu button is clicked. The problem was that whenever I set a stage I used a new instance of teh Club model instead of using getKiosk(). I changed all the new Club()```` to getClub()``` and it everything works perfectly now.
in my code i have a tableview bound to a ListProperty.
I am able to retrieve all the data correctly but i need to apply a condition to this data set based on a specific property of each object in it.
Is it possible?
This is the model:
public class Ticket {
private String number;
private String sys_id;
private String short_description;
private String description;
private String opened_by;
private String assigned_to; // tabella esterna
private String assignment_group; // tabella esterna
private int incident_state; // 2: assigned - 3: wip - 4: hold
private String sys_created_on;
private Date sys_updated_on;
private int priority; // 1: critical
private boolean isVisible; // per nasconderlo nella tabella riassuntiva
private boolean isAlarmed; // per silenziare le notifiche
In the controller i have bound the tableview to a ListProperty (returned by ticketService.ticketsProperty()):
serviceNowTicket_tableView.itemsProperty().bindBidirectional(ticketService.ticketsProperty());
I just need to filter this by the ticket "isVisible" property.
Maybe via BooleanBinding?
I would like to know if it is possible to filter / scan based on attributes in a collection of DynamoDBDocuments. Lets say I have this:
#DynamoDBTable(tableName = "Orders")
public class Order {
#DynamoDBHashKey
#DynamoDBAutoGeneratedKey
private String id;
#DynamoDBAttribute
#DynamoDBTypeConverted(converter = LocalDateTimeConverter.class)
private LocalDateTime orderDate;
#DynamoDBAttribute
private Address shipTo;
#DynamoDBAttribute
private Address billTo;
#DynamoDBAttribute
private List<OrderItem> items;
}
#DynamoDBDocument
public class Address {
private String name;
private String street;
private String city;
private String state;
private String zip;
}
#DynamoDBDocument
public class OrderItem {
private String product;
private int qty;
private double itemCost;
}
Lets assume I want to find all orders which have one or more items where the product is "widgets". I believe the answer is no. With a relational DB I would do:
SELECT o FROM Orders o, OrderItems i WHERE o.id = i.id AND i.product="widgets"
Would a better practice be to put the order items in their own table and have a orderId attribute in it and then query order items based on the product and then grab the order based on the order ID?
If you use a map instead of a list for #DynamoDBAttribute items, where the key of the map is the product, then you can use a filter expression attribute_exists(items.widgets) to down-select.
I am using ControlFx PropertySheet in my project . I manage To get it running.
BeansObj:
public class BeansObj implements Serializable {
private String name;
private String mail;
private boolean smart;
private int age;
//Getters,Setters, Beans stuff....
}
ControllerClass:
public class Controller implements Initializable {
#FXML
private PropertySheet sheet;
#Override
public void initialize(URL location, ResourceBundle resources){
sheet.getItems().addAll(BeanPropertyUtils
.getProperties(new BeansObj("foo","foo#bar.foo",true,41)));
}
}
For the boolen field, the generated control is a checkbox with an empty text;
1-How to set this text value implicitly?
For the mail field(String)
2-how to add custom validation method?
For each field
3-how to add a css class|id to specific Control?
By default(I guess) all the controls belong to the Basic Category( PropertySheet.Mode)
4-how to set the category implicitly or explicitly ?
and, in case BeansObj has an Collection attribute
5-how to make it generate a tableView?
Thank you in advance.
In my Spring app i have Two entity:
#Entity
public class Category {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
....
#OneToOne
#Cascade({CascadeType.ALL})
#JoinColumn(name="page_id")
private Page page;
#OneToMany(fetch=FetchType.LAZY, mappedBy="category")
private List<Shop> shop;
..getters and setters
}
And
#Entity
public class Shop {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
....
#OneToOne(mappedBy="shop")
private Settings settings;
#ManyToOne
#Cascade({CascadeType.SAVE_UPDATE})
#JoinColumn(name = "category_id")
private Category category;
#OneToOne
#Cascade({CascadeType.ALL})
#JoinColumn(name = "page_id")
private Page page;
...getters and setters
}
And in my test jUnit i added some category and some shop with this category, when i want to access to list of shops current category i have a error
java.lang.NullPointerException ....
My test:
#ContextConfiguration(locations = {
"classpath:security-context.xml",
"classpath:dao-context.xml"
})
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
#Transactional
public class CategoryTest {
Shop shop1 = new Shop(....);
Shop shop1 = new Shop(....);
Category category1 = new Category(....);
Category category2 = new Category(....);
#Test
public void someTest(){
shop1.setCategory(category1);
shop2.setCategory(category2);
shopService.add(shop1);
shopService.add(shop2);
assertEquals(2, shopService.getAllShop().size());
assertEquals(2, categoryService.getAllShop().size());
//IN THIS LINE I HAVE A ERROR
assertEquals(1, categoryService.getCategory(category1.getId()).getShop().size());
}
}
For access to lazy atribute i added to my web.xml filtr: org.springframework.orm.hibernate3.support.OpenSessionInViewFilter This working properly in front app not in jUnit test.
Which point I make an error?
The lazy loading only takes place whist inside a transaction, when the session is open. So you get null in the test method unless you initialize the lazy loaded collection beforehand.
Another similar question