Good evening,
I am working on a project and I'm at lost at how to configure my tableviews to depend on each other. I would like for the parts in one tableview to depend on the products in the other. How would I go about doing that? I prefer to not use sql at the moment just to keep everything simple as possible. The code snippet is listed below:
public class maincontroller {
private ObservableList<Part> parts = FXCollections.observableArrayList();
private ObservableList<Product> products = FXCollections.observableArrayList();
//code to swap between controllers
#Override
public void initialize(URL url, ResourceBundle rb) {
partsID.setCellValueFactory(new PropertyValueFactory<>("partID"));
partsName.setCellValueFactory(new PropertyValueFactory<>("name"));
partsinvlevel.setCellValueFactory(new PropertyValueFactory<>("instock"));
partscost.setCellValueFactory(new PropertyValueFactory<>("price"));
parttable.setItems(parts);
productsID.setCellValueFactory(new PropertyValueFactory<>("productID"));
productsName.setCellValueFactory(new PropertyValueFactory<>("name"));
productsInvlevel.setCellValueFactory(new PropertyValueFactory<>("instock"));
productsprice.setCellValueFactory(new PropertyValueFactory<>("price"));
producttable.setItems(products);
parttable.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends Part> observable, Part oldValue, Part newValue) -> {
});
producttable.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends Product> observable, Product oldValue, Product newValue) -> {
});
}
}
I'm not too sure about the relationship between Part and Product. From the context, I would assume each specific Product would have its own list of Parts that is related to it. I would also assume that that list of Parts is stored within Product.
Product:
public class Product
{
private final ObservableList<Part> parts = FXCollections.observableArrayList();
public final ObservableList<Part> getParts() { return parts; }
........
}
This is what you can do:
producttable.getSelectionModel().selectedItemProperty().addListener(observable, oldProduct, newProduct) ->
{
if (newProduct != null)
{
parts.clear();
parts.addAll(newProduct.getParts());
}
});
Related
Can TableView commit changes to generic fields like String or Integer without extra step with setOnEditCommit?
Currently I use such structure:
#FXML
TableColumn<User, String> _tableStateUsername;
#Override
public void initialize(URL location, ResourceBundle resources) {
_tableStateUsername.setCellFactory(TextFieldTableCell.<User, String>forTableColumn(new DefaultStringConverter()));
_tableStateUsername.setCellValueFactory(new PropertyValueFactory<User, String>("username"));
_tableStateUsername.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<User, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<User, String> event) {
event.getRowValue().setUsername(event.getNewValue());
}
});
and it works fine, but I feel that it can be simplified (without using SimpleString)
I tried binding Value factory to field but no luck:
_tableStateUsername.setCellValueFactory(cellData -> Bindings.select(cellData.getValue(), "username"));
Is there any way to make cell set NewValue to bound field automatically?
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.)
I have combobox, tableview as TableView<Person> and 3 columns.
I want to populate the updated data in TableView as per item I select in combobox.
My Issue: every time I select the item in combobox, the data in table view is added. I want the details to be displayed for the selected item only.
Code:
public class MyTableController {
private TableView<Person> personTableView;
private ObservableList<Person> personTableViewData= FXCollections.observableArrayList();
initialize() {
comboBox.valueProperty().addListener(new ChangeListener<String>() {
#Override public void changed(ObservableValue ov, String oldValue, String newValue) {
List<Person> filteredPersonList =
personList.stream().filter(person -> person.getFirstName().contains(newValue)).collect(Collectors.toList());
personTableViewData.addAll(filteredPersonList);
personTableView.setItems(personTableViewData);
}
}
Use ObservableList.setAll instead of ObservableList.addAll or alternatively create a new ObservableList. Otherwise you're adding new items to the same list on every change instead of replacing the items:
initialize() {
personTableView.setItems(personTableViewData);
comboBox.valueProperty().addListener(new ChangeListener<String>() {
#Override public void changed(ObservableValue ov, String oldValue, String newValue) {
List<Person> filteredPersonList =
personList.stream().filter(person -> person.getFirstName().contains(newValue)).collect(Collectors.toList());
personTableViewData.setAll(filteredPersonList);
}
}
I am trying to update a text field through observer pattern. The update function in the observer (FXML controller) is called after clicking on a listItem in another controller class. And that works fine. The only problem is that my textfield won't update.
Here is my update function in the observer.
#Override
public void update(Observable o, final Object arg) {
System.out.println("test"); // works
firstNameTextField.setText("test"); // doesn't work (text field is still empty)
System.out.println(firstNameTextField.getText()); //works and shows me the word "test" on my console
}
The funny thing is, if I print the text from the text field on my console it's printing the word "test" on the console. It seems like the text field value is updated but it doesn't show up on the ui.
EDIT:
This is my MainController
public class MainController extends Observable implements Initializable {
private ObservableList<String> items = FXCollections.observableArrayList("item1", "item2");
private List<UserProfile> userProfiles = new ArrayList<UserProfile>();
private String[] tabTitles = { "Profile"};
#FXML
private TabPane tabPane;
#FXML
ListView<String> listView;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
for (String tabTitle : tabTitles) {
Tab tab = new Tab(tabTitle);
tab.setClosable(false);
tabPane.getTabs().add(tab);
}
tabPane.getSelectionModel().clearSelection();
for (Tab tab : tabPane.getTabs()) {
try {
String newStringValue = tab.getText();
Parent root = FXMLLoader.load(getClass().getResource("profile.fxml"));
tab.setContent(root);
FXMLLoader fxmlLoader = new FXMLLoader();
Object p = fxmlLoader.load(getClass().getResource("profile.fxml").openStream());
if (fxmlLoader.getController() instanceof ProfileController) {
ProfileController profileController = (ProfileController) fxmlLoader.getController();
this.addObserver(profileController);
}
} catch (IOException e) {
e.printStackTrace();
}
}
tabPane.getSelectionModel().selectFirst();
listView.setItems(items);
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
setChanged();
notifyObservers();
}
});
}
}
ProfileController
public class ProfileController implements Initializable, Observer {
#FXML
TextField firstNameTextField;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
#Override
public void update(Observable o, final Object arg) {
System.out.println("test"); // works
firstNameTextField.setText("test"); // doesn't work (text field is still empty)
System.out.println(firstNameTextField.getText()); //works and shows me the word "test" on my console
}
}
Can anybody help me out with this?
Thanks!
When you execute
Object p = fxmlLoader.load(getClass().getResource("profile.fxml").openStream());
if (fxmlLoader.getController() instanceof ProfileController) {
ProfileController profileController = (ProfileController) fxmlLoader.getController();
this.addObserver(profileController);
}
you load the structure represented by profile.fxml, and place that hierarchy (including firstNameTextField) in the object you called p. When you invoke update(...) on profileController, it changes the text in the text field that is part of the hierarchy of p. However, you never do anything with p: you don't display it in your UI. So when you change the text of the text field, the changes are of course invisible (because you are changing a text field that isn't displayed).
Presumably, since you said you have the text field displayed, somewhere in the code you couldn't be bothered to include you are loading profile.fxml and displaying the content in the UI. You need to get the reference to that controller, and register it as an observer. Registering an arbitrary instance of the same class will not have the desired effect.
I have implemented a NavigationDrawer with a few items in it. Every item represents a different Fragment and one of them has tabs. My problem is that every time I open this Fragment the tabs reloaded! and added to the previous tabs. Why is this happening and how can I solve it?
This is the Fragment with the tabs:
public class fragment_profilo_tabs extends Fragment implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
String[] tabs = { "Profilo aziendale", "Credenziali" };
/* (non-Javadoc)
* #see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
// Initilization
View view = View.inflate(getActivity(), R.layout.profilo_tabs, null);
viewPager = (ViewPager) view.findViewById(R.id.pager);
actionBar = getActivity().getActionBar();
mAdapter = new TabsPagerAdapter(getFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
return view;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
And this is my ViewPagerAdapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
/* (non-Javadoc)
* #see android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)
*/
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new fragment_profilo_modificaProfilo();
case 1:
// Games fragment activity
return new fragment_profilo_credenzialiAccesso();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
Ok there are multiple issues. First and foremost lets talk about your ViewPagerAdapter:
The first thing that caught my eye is this:
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
This is very bad for performance. I know that some posts on Stack Overflow suggest using this to "fix" a few things, but that is not the way to go. Remove this, you don't need it.
But the main issue is in your Fragment. First and foremost this:
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab()
.setText(tab_name)
.setTabListener(this));
}
You are adding tabs to the ActionBar. And the ActionBar is not part of you Fragment. Each time you display this Fragment you add the same tabs to the ActionBar but you never remove them again or anything.
My advice: Don't do this. Open a new Activity for the tabs Fragment. This should clearly not be in the same Activity. I cannot imagine any situation where suddenly adding tabs to the current Activity would satisfy the Android Platform Guidelines or provide good usability.
The general rule is that everything that has to do with the Activity itself - like things concerning the ActionBar or tabs in the ActionBar - should be handled in the Activity. Code which adds tabs to the ActionBar has no place inside a Fragment. So either add the tabs permanently in onCreate() of your Activity. Or create a new Activity with those tabs especially for the fragment_profilo_tabs.
As an aside: class names should never be Snake Case. Start class names with an uppercase letter and use Camel Case. Everything else will just confuse other programmers looking at your code