I've been looking at tutorials, and I can't seem to get a table to populate.
I'm using net beans and scenebuilder too.
Any help would be greatly appreciated! been struggling for 5 hours.
Here is my code for the Controller class:
public class FXMLDocumentController implements Initializable {
#FXML
private TableView<Table> table;
#FXML
private TableColumn<Table, String> countriesTab;
/**
* Initializes the controller class.
*/
ObservableList<Table> data = FXCollections.observableArrayList(
new Table("Canada"),
new Table("U.S.A"),
new Table("Mexico")
);
#Override
public void initialize(URL url, ResourceBundle rb) {
countriesTab.setCellValueFactory(new PropertyValueFactory<Table, String>("rCountry"));
table.setItems(data);
}
}
Here is my code for the Table
class Table {
public final SimpleStringProperty rCountry;
Table(String country){
this.rCountry = new SimpleStringProperty(country);
}
private SimpleStringProperty getRCountry(){
return this.rCountry;
}
}
Here is my main:
public class Assignment1 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
For PropertyValueFactory to find the property the item class (i.e. Table in this case) needs public as access modifier, not package private. The method returning the property needs to be public as well.
Furthermore the correct name for the method returning the property itself is <nameOfProperty>Property according to the conventions required for PropertyValueFactory to work.
Also since the actual type of the property is an implementation detail, it would be better design to use StringProperty as return type instead of SimpleStringProperty
public class Table {
private final SimpleStringProperty rCountry;
public Table(String country){
this.rCountry = new SimpleStringProperty(country);
}
public StringProperty rCountryProperty() {
return this.rCountry;
}
}
In case you used these modifiers to prevent write access to the property, you can still achieve this effect by using a ReadOnlyStringWrapper and return a ReadOnlyStringProperty:
public class Table {
private final ReadOnlyStringWrapper rCountry;
public Table(String country){
this.rCountry = new ReadOnlyStringWrapper (country);
}
public ReadOnlyStringProperty rCountryProperty() {
return this.rCountry.getReadOnlyProperty();
}
}
In case there is no write access to the property at all, simply using a getter for the property is enough. You do not need to use a StringProperty at all in this case:
public class Table {
private final String rCountry;
public Table(String country){
this.rCountry = country;
}
public String getRCountry() {
return this.rCountry;
}
}
Related
I have a problem with javafx. It doesn't show any data from my DB I tried to enter some data manually but it doesn't work neither. I saw all the post around this issue but none of them bring me a fix.
Thanks a lot for your help and sorry for my English.
Main class :
private ObservableList<Index> LstClient = FXCollections.observableArrayList();
public MainClass {
LstClient.add(new Index("Lesinge"));
}
public ObservableList<Index> getLstClient(){return LstClient;}
Here my controller :
#FXML
private TableView<Index> personneTable;
#FXML
private TableColumn<Index, String> NomColonne;
private Mainclass main;
public PersonneMapping() { }
public void setMainApp(Mainclass mainApp) {
this.main = mainApp;
personneTable.setItems(main.getLstClient());
#FXML
public void Initialize () {
NomColonne.setCellValueFactory(cellData -> cellData.getValue().getNom())
}
Here my Index
private StringProperty Nom = new SimpleStringProperty();
public Index( String Nom ) {
this.Nom.set(Nom);
}
public StringProperty getNom() {
return Nom;
}
public void setNom(StringProperty Nom) {
this.Nom = Nom;
}
}
My fxml files is good it has the good controller and fx id on each columns
I am having trouble understanding how to apply the mvc pattern with JavaFX.
Here are my questions with respect to the code below, since I need to follow the pattern given in the code:
a) How can I attach an event handler of the button which is present in my ViewA to the code in my ControllerA (specifically, attachEventHandlers() method). For example, I want my button to populate the comboBox in ViewA with the results of getModelItems() method from controller.
Note that the method getModelItems() is private.
b) I would have multiple buttons and event handlers in my view. How will I bind each one of them uniquely to the controller?
c) I want to invoke setName(String name) on my model in the controller, and the parameter I want to pass is the selected value on the comboBox in viewA. How can I achieve this?
Thank you so much for any help!
Below is the code referred in the description.
Controller:
import model.ModelA;
import view.ViewA;
public class ControllerA {
private ViewA view;
private ModelA model;
public ControllerA(ViewA view, ModelA model) {
//initialise model and view fields
this.model = model;
this.view = view;
//populate combobox in ViewB, e.g. if viewB represented your ViewB you could invoke the line below
//viewB.populateComboBoxWithCourses(setupAndRetrieveCourses());
this.attachEventHandlers();
}
private void attachEventHandlers() {
}
private String[] getModelItems() {
String[] it = new String[2];
it[0] = "0";
it[1] = "1";
return it;
}
}
Model:
public class ModelA {
private String name;
public Name() {
name = "";
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "Name = " + name;
}
}
View:
import javafx.scene.layout.BorderPane;
//You may change this class to extend another type if you wish
public class ViewA extends BorderPane {
public BorderPane bp;
public ViewA(){
this.bp = new BorderPane();
ComboBox comboBox = new ComboBox();
Button button1 = new Button("Populate");
bp.setTop(button1);
bp.setBottom(comboBox);
}
}
Loader:
public class ApplicationLoader extends Application {
private ViewA view;
#Override
public void init() {
//create model and view and pass their references to the controller
ModelA model = new ModelA();
view = new ViewA();
new ControllerA(view, model);
}
#Override
public void start(Stage stage) throws Exception {
//whilst you can set a min width and height (example shown below) for the stage window,
//you should not set a max width or height and the application should
//be able to be maximised to fill the screen and ideally behave sensibly when resized
stage.setMinWidth(530);
stage.setMinHeight(500);
stage.setTitle("Final Year Module Chooser Tool");
stage.setScene(new Scene(view));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You add delegates to your ViewA to allow for access:
public class ViewA extends BorderPane {
ComboBox comboBox;
Button button1;
public ViewA(){
comboBox = new ComboBox();
button1 = new Button("Populate");
setTop(button1);
setBottom(comboBox);
}
// Add delegates for all functionality you want to make available through ViewA
public ObservableList<String> getItems() { return comboBox.getItems(); }
public void setOnButton1Action(...) { ... }
public void setOnButton2Action(...) { ... }
...
}
You can go as broad or as narrow as you like, based on how much you want to manage through ViewA.
What I'm trying to do is have a single class that maintains a static ObservableList of countries. I want to display these countries in a ComboBox. I've got this part working fine. Now, I also want to enable the user to add new countries to the list. So, there is a button beside the combo box that will show another dialog allowing entry of another country name. After the user enters the country name and clicks save, I would like the single static ObservableList to be updated with the new country and then it show up in the ComboBox. This part is not happening.
I'll show what DOES work, and what does not.
Saving a reference to the static list and updating that works. Like so:
public class CustomerController implements Initializable {
private ObservableList<Country> countryList;
#Override
public void initialize(URL url, ResourceBundle rb) {
countryList = Country.getCountryList();
comboCountry.setItems(countryList);
}
...
// Fired when clicking the "new country" button
#FXML
void handleNewCountry(ActionEvent event) {
Country country = new Country();
country.setCountry("Austria");
countryList.add(country);
}
}
This is what I would like to do, however it does not work:
public class CustomerController implements Initializable {
#FXML
private ComboBox<Country> comboCountry;
#Override
public void initialize(URL url, ResourceBundle rb) {
comboCountry.setItems(Country.getCountryList());
}
#FXML
void handleNewCountry(ActionEvent event) {
showScene("Country.fxml", "dialog.newCountry");
}
private void showScene(String sceneResource, String titleResource) {
try {
FXMLLoader loader = new FXMLLoader(
getClass().getResource(sceneResource),
resourceBundle
);
Scene scene = new Scene(loader.load());
getNewStage(resourceBundle.getString(titleResource), scene).showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
private Stage getNewStage(String title, Scene scene) {
Stage stage = new Stage();
stage.setTitle(title);
stage.setResizable(false);
stage.setScene(scene);
stage.initOwner(rootPane.getScene().getWindow());
stage.initModality(Modality.APPLICATION_MODAL);
return stage;
}
}
The Country class:
public class Country extends BaseModel {
private int countryID;
private StringProperty country;
private static ObservableList<Country> countryList; // The static observable list
public Country() {
countryList = FXCollections.observableArrayList();
country = new SimpleStringProperty();
}
public int getCountryID() {
return countryID;
}
public void setCountryID(int countryID) {
this.countryID = countryID;
}
public StringProperty countryProperty() {
return this.country;
}
public String getCountry() {
return this.country.get();
}
public void setCountry(String country) {
this.country.set(country);
}
public boolean equals(Country country) {
if (this.getCountry().compareToIgnoreCase(country.getCountry()) != 0) {
return false;
}
return true;
}
public static ObservableList<Country> getCountryList() {
if (countryList.size() < 1) {
updateCountryList();
}
return countryList;
}
public static void updateCountryList() {
countryList.clear();
ArrayList<Country> daoList = CountryDao.listCountries();
for (Country country : daoList) {
countryList.add(country);
}
}
#Override
public String toString() {
return this.getCountry();
}
}
And the dialog for entering a new country:
public class CountryController implements Initializable {
#FXML
private TextField textCountry;
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#FXML
void handleSave(ActionEvent event) {
Country country = new Country();
country.setCountry(textCountry.getText().trim());
CountryDao.insert(country); // Insert the country into the database
Country.updateCountryList(); // Update the static ObservableList
close();
}
#FXML
void handleCancel() {
close();
}
void close() {
final Stage stage = (Stage) textCountry.getScene().getWindow();
stage.close();
}
}
So, my theory is that somehow the ComboBox is creating a new instance of the ObservableList when setItems is called. I'm really not sure though. A static object should only have one instance, so updating it from anywhere should update that ComboBox. Anyone know what's up with this?
You're creating a new ObservableList instance every time the Country constructor is invoked. This way a list different to the one used with the ComboBox is modified.
If you really need to keep the list of countries in a static field (this is considered bad practice), you should make sure to only create a single ObservableList:
private static final ObservableList<Country> countryList = FXCollections.observableArrayList();
(Remove the assignment of this field from the constructor too.)
How can I fill ChoiceBox with e.g. a StringProperty from my custom class?
I have simply design in SceneBuilder with a ChoiceBox and I have a Person class with my data.
public class Person{
private final StringProperty firstName;
public Person(){
this(null);
}
public Person(String fname){
this.firstName = new SimpleStringProperty(fname);
}
public String getFirstName(){
return this.firstName.get();
}
public void setFirstName(String fname){
this.firstName.set(fname);
}
public StringProperty firstNameProperty(){
return this.firstName;
}
}
In main class I have:
private ObservableList<Person> personList = FXCollections.observableArrayList();
this.personList.add(new Person("Human1"));
RootController controller = loader.getController();
controller.setChoiceBox(this);
public ObservableList<Person> getPersonList(){
return this.personList;
}
And in my controller:
public class RootController {
#FXML
private ChoiceBox personBox;
public RootController(){
}
#FXML
private void initialize(){
}
public void setChoiceBox(App app){
personBox.setItems(app.getPersonList());
}
}
But this code fill my ChoiceBox by function name(??) or something like that.
How can I fill it with the firstName property?
Note that you've created yourself a big problem by making the firstName property mutable here.
AFAIK it's not possible to make ChoiceBox listen to modifications of that property (at least not without replacing the skin, which would be awfully complicated).
This could be done with a ComboBox however.
You just need to use a custom cellFactory:
private ListCell<Person> createCell(ListView<Person> listView) {
return new ListCell<Person>() {
#Override
protected void updateItem(Person item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
textProperty().unbind();
setText("");
} else {
textProperty().bind(item.firstNameProperty());
}
}
};
}
ComboBox<Person> cb = new ComboBox<>(personList);
cb.setCellFactory(this::createCell);
cb.setButtonCell(createCell(null));
...
For your problem I would suggest to use the 'easiest way'.The ChoiceBox uses the toString() method of the Person class resulting in something like choiceBox.Person#18f8865.
By overriding the toString() method you can define what the ChoiceBox will display. In your case return the value of the firstName property:
#Override
public String toString() {
return firstName.get();
}
I try set edit cell on run program. Set table editable, cellfactory and other.
I can edit the cell, when clicked with the mouse. But the call edit() method of TableView does not create Textfield.
What have I missed?
public class Main extends Application {
TableView <TestClass> tableView;
TableColumn <TestClass, String> stringColumn;
TableColumn <TestClass, String> editColumn;
ObservableList<TestClass> items;
#Override
public void start(Stage primaryStage) throws Exception{
makeTestData();
tableView = new TableView();
tableView.setEditable(true);
stringColumn = new TableColumn<>("Col1");
editColumn = new TableColumn<>("Col2");
tableView.getColumns().addAll(stringColumn, editColumn);
stringColumn.setCellValueFactory(cell -> cell.getValue().stringProperty());
editColumn.setCellValueFactory(cell -> cell.getValue().editProperty());
editColumn.setCellFactory(TextFieldTableCell.<TestClass>forTableColumn());
tableView.setItems(items);
tableView.getSelectionModel().select(1);
tableView.getSelectionModel().focus(1);
tableView.edit(1, editColumn); // !!! not create textfield ???
BorderPane pane = new BorderPane();
pane.setCenter(tableView);
primaryStage.setScene(new Scene(pane));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void makeTestData(){
items = FXCollections.observableArrayList(
new TestClass("str1", "edit1"),
new TestClass("str2", "edit2"),
new TestClass("str3", "edit3")
);
}
public class TestClass{
StringProperty string = new SimpleStringProperty();
StringProperty edit = new SimpleStringProperty();
public TestClass() {}
public TestClass(String string, String edit) {
this.string = new SimpleStringProperty(string);
this.edit = new SimpleStringProperty(edit);
}
public String getString() { return string.get();}
public StringProperty stringProperty() { return string; }
public void setString(String string) { this.string.set(string);}
public String getEdit() { return edit.get();}
public StringProperty editProperty() { return edit;}
public void setEdit(String edit) { this.edit.set(edit);}
}
}
yes im also getting this problem. the way i solved it is by putting the edit method call inside another fx thread.
Platform.runLater(() -> {
tableView.edit(row, editColumn);
});