I have a controller that has a combobox defined as so:
private ComboBox warehouseComboBox;
and a method that populates that combobox in that controller
#FXML
void createWarehouseInstance (ActionEvent event){
ArrayList<BikePart> inventory = new ArrayList<>(); //empty inventory
String whName = warehouseNameField.getText();
warehouse wh = new warehouse(whName, inventory); //create new instance
warehouseComboBox.getItems().add(wh);
warehouseArrayList.add(wh); //add instance to arrayList
warehouseNameField.clear(); //clear text box
}
What I'm trying to do is get the state of that combobox and that determines which instance of a class I would be manipulating.
My attempt is so with this method. Where I show a list of Bicycle parts depending on the instance selected with the combo box.
#FXML
void showParts(){
bikePartList.clear();
bikePartList.appendText("Name\tNumber\tListPrice\tSalePrice\tQuantity\tOnSale?\n");
Object selection = warehouseComboBox.getSelectionModel().getSelectedItem();
if (warehouseArrayList.contains(selection)){
bikePartList.appendText(warehouse.printAll()); //what do I do here?
}
}
For context, Warehouses hold BikeParts, and I'm figuring out that if I want to dynamically create warehouses, I can't make this static
If I understand what you're trying to do:
First declare your ComboBox with the proper type:
private ComboBox<Warehouse> warehouseComboBox;
and then you can do
#FXML
void showParts(){
bikePartList.clear();
bikePartList.appendText("Name\tNumber\tListPrice\tSalePrice\tQuantity\tOnSale?\n");
Warehouse selectedWarehouse = warehouseComboBox.getSelectionModel().getSelectedItem();
// I think this is what you're trying to do:
bikePartList.appendText(selectedWarehouse.printAll()); //what do I do here?
}
Note that I changed your class name to conform to standard naming conventions.
Related
I understand that the question turned out to be huge, but otherwise it will not be clear to everyone.
I am writing a document manager application for myself, what is happening to me there now - I use the ObservableList collection (located in the main Main class) that operates on variables from the MainData class (it turns out that each instance of this class is a new row in the table). I display all the information through the controller class, that is, I have the first table, where I display all my documents (works), depending on the selected document, I display the necessary information in the corresponding labels (works), but I also there is a second table (it does not work correctly), where, according to my logic, depending on the selected document, I display file names that relate to the selected document (i.e., if one document is highlighted, the second table can display 3 files, and if you select another document, 5 files can be displayed). To check the functionality, I add a couple of rows to the SimpleListProperty collection (which is located in MainData respectively) to display them to the table, but at least my added data in the designer is displayed in the table, they all go in one line, and besides the second table creates the same number of rows as in the first (three identical rows are created in qwerty, asdfgh, since I have three documents created), how to get rid of this dependency? Those. I want to bring my application to the logic that I described above.
Class of all variables
public class MainData {
private final StringProperty numberContract;
private final ListProperty<String> nameFiles; //here I put the names of the files that relate to the document
// further variables for Label
public MainData(String numberContract) {
this.numberContract = new SimpleStringProperty(numberContract);
this.nameFiles = new SimpleListProperty<>(FXCollections.observableArrayList());
this.nameFiles.add("qwerty");
this.nameFiles.add("asdfgh"); //i want to add only two rows to the second table
}
//Further, all getters and setters
Class-controller
public class MainController {
#FXML
private TableView<MainData> contractTable;
#FXML
private TableColumn<MainData, String> numberContractColumn;
#FXML
private TableView<MainData> filesTable;
#FXML
private TableColumn<MainData, ObservableList<String>> nameFilesColumn;
//далее снова все для Label
// Link to main application.
private Main main;
public void setMain(Main main) {
this.main = main;
// Adding to the table data from the observed list
contractTable.setItems(main.getContractData());
filesTable.setItems(main.getContractData());
}
#FXML
private void initialize() {
// Initialize the table.
numberContractColumn.setCellValueFactory(cellData -> cellData.getValue().numberContractProperty());
nameFilesColumn.setCellValueFactory(cellData -> cellData.getValue().getNameFiles());
// Listen to the selection changes, and when changing, display
// additional information about the addressee.
contractTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showContractDetails(newValue));
Main class
public class Main extends Application {
private Stage menuBar;
private BorderPane mainWindow;
/**
* Data, in the form of an observable list of documents.
*/
private ObservableList<MainData> contractData = FXCollections.observableArrayList();
/**
* Returns data in the form of an observable list of documents.
* #return
*/
public ObservableList<MainData> getContractData() {
return contractData;
}
//further FXML display
I am making a Java Desktop application. In my application, I am using a JCombobox. Here is an example:
I did not write anything the JCombobox.However, first film's name can be seen from the JComboBox.
Moreover, Application takes input from the user and according to that input, write something to the JCombobox.
public class Searchbar extends JComboBox
{
// PROPERTIES
AutoCompleteDecorator decorator;
private final ArrayList<Movie> movies;
private final DBConnectMovie movieConnection = new DBConnectMovie();
private final int totalCount = movieConnection.getTotalMovieCount();
private String item;
// CONSTRUCTOR
public Searchbar()
{
movies = movieConnection.getAllMovies();
this.insertItemAt("", 0);
this.setModel(new DefaultComboBoxModel(movies.toArray()));
AutoCompleteDecorator.decorate(this);
this.setPreferredSize(new Dimension(500, 40));
item = this.getEditor().getItem().toString();
}
// METHODS
public void changeComboBoxText()
{
System.out.println("Prints this out");
this.getEditor().setItem("C");
}
}
I can print out the text inside the changeComboBox method. However, I can not manage to change the JComboBox's text.
Have a beautiful day, thanks!
To change combo:
a. update movies
b.use this.setModel(new DefaultComboBoxModel(movies.toArray()));
My question is simple, is there any way of making a bidirectional bind between a choicebox's items and an arraylist (that can be added to and removed from, from another class)
My SettingsService contains a simple ArrayList containing User objects. New items can be added from other classes and places in my application.
If I add a user to this list, how can the new item automatically appear in the choicebox?
Example:
Viewmodel
public class ViewModel {
private SettingsService settings = new SettingsService();
public final ObjectProperty<ObservableList<User>> userChoiceBoxItems = new SimpleObjectProperty<>();
public ViewModel() {
userChoiceBoxItems.setValue(FXCollections.observableArrayList(settings.getUsers()))
}
}
View
public class View {
private ViewModel viewModel = new ViewModel();
#FXML
ChoiceBox<User> userChoiceBox;
#FXML
public void initialize() {
userChoiceBox.itemsProperty().bindBidirectional(viewModel.userChoiceBoxItems);
}
Thanks for the help
create a new ObservableList as
ObservableList<User> tempList = FXCollections.observableArrayList()
and add all items to the list as
templist.addAll(userChoiceBoxItems)
and then bidirectionally bind
I have a problem with a variable in MyFrame class. I want to have in MyFrame class the value of a variable that is defined in a combobox listener.
This is my situation: I have a combobox with some friends' name. I have put a listener to the combobox which has to return the surname of the selected friend.
I want to insert the value of surname in a command in MyFrame class, but there are some problems: once setted surname as final (because it has to be used in the Listener), I have an error that say:
The final local variable surname cannot be assigned, since it is defined in an enclosing type.
What is (or are) the matter(s)? Here I post my code:
public class MyFrame extends {
public static void main (String[] args)
{
//other
String [] names = {"john","al","jack"};
final String surname=null;
JLabel nameLbl = new JLabel("surname: " + surname);
JComboBox box = new JComboBox(names);
JPanel centralPnl = new JPanel();
centralPnl.add(nameLbl);
centralPnl.add(box);
box.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
{
// Here operations from database
//that return friends' surname under the variable name of "result"
surname = result;
}
}
});
}
}
You are trying to reassign a final variable, and thats the problem.
Also your final variable needs to be initialised in the first place.
Beyond the issues with the code already pointed out, I guess the question is do you need to store surname or are you just using it to update the label?
If you need to store the data, move your surname variable to the class level.
If you are simply updating the label, then do something like
nameLbl.setText("surname: " + result);
There are two things first one is that final variable must be initialized when it is declared and that final variable cannot be reassigned a value.
Unfortunately you are doing both of the mistakes.
Another problem is that you should post a Valid code; it will make others finding problems easily.
My program is based on a API. I got a JList with and a model that has a some names. And a selectListener to get the seleted item and a button to send that item to the other window which has another Here is my first list:
First List (window) and send the items to the other list.
final DefaultListModel<String> Names = new DefaultListModel<String>();
final JList<MyAPI> Places = new JList(Names);
private JList<MyAPI> locList;
private DefaultListModel<MyAPI> favourites;
public AddLocation(JList<MyAPI> locList, DefaultListModel<MyAPI> favourites){
this.locList = locList;
this.favourites = favourites;
}
addThis.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Object chose = Places.getSelectedValue();
favourites.addElement((MyAPI) chose); // error in this line
}
});
And this is the other window that selected items should be added to here:
final DefaultListModel<MyAPI> favourites;
final JList<MyAPI> locList;
favourites = new DefaultListModel<MyAPI>();
locList = new JList<MyAPI>(favourites);
So now both windows loads and the first list loads with its names in it. but when I press the button add this, it gives error and points to this line:
favourites.addElement((MyAPI) chose);
How can I solve it?
Your first model is defined like this:
final DefaultListModel<String> Names ...;
Your second model is defined like this:
final DefaultListModel<MyAPI> favourites;
Your first list model conains String instances, your second model contains MyAPI instances. So when this line is executed:
favourites.addElement((MyAPI) chose);
you are trying to make a MyAPI out of a String, which does not work and probably a ClassCastException is thrown.
Either you need to declare the second list model as final DefaultListModel<String> favourites; or you create an instance of MyAPI based on the selected String (new MyAPI(chose) ?).