I am working with the Javafx GUI but i also require the same level of functionality from the command line. I am wondering what the best way to make a main class which has functionality for both command line and Javafx at the same time so you can do one thing on the GUI and then do the next thing on command line. Command line would also update the GUI display.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I think this is borderline to be too broad. One part of that is: your requirements are unclear. Do you intend to you use the command line like:
So - you invoke java repeatedly, and whatever.jar basically is a client that takes to some "server" that does the real work, or do you envision
Obviously, that makes a huge difference here.
But in the end, it also tells us where a solution is: by de-coupling these clients from the actual execution.
Meaning: you should do two things
Avoid baking all of these different aspects into one single main() method!
(Really, this question is off-topic, as it is too broad. It was interesting enough, though, for me to try a proof of concept of the approach that seemed natural to me, so I answered it anyway.)
You essentially need two things here:
start()
method, running it in a background thread so that it doesn't block the UI. You just need to make sure there that the model makes updates on the correct (i.e. the FX Application) thread.Here's a simple example, which just computes the total of a list of integers. Here's the model, which stores the list and the total. It has methods to add a new value, or clear the list. Note how those methods execute their changes on the UI thread:
Here's the UI code. First a view, in FXML:
and a controller, which observes and updates the model:
Now a simple command line interpreter, which reads from the command line and references the model. It supports either integer entry (add value to the model), or the commands
total
,show
, orclear
:Finally, the JavaFX application which assembles all these. Note that the same model instance is passed to both the CLI and the UI controller, so both are updating the same data. You can enter some values in the text field, then type "show" in the command line, and you'll see the values. Type "clear" in the command line, and the values will be removed from the UI, etc.
You can, of course, just create the UI without the CLI, or create the CLI without the UI; both are independent of each other (they just both depend on the model).
Everything on the GUI is Event based. This means that methods get called when you press a button or interact with a JavaFX Window in another way like selecting an item on a list.
I suggest keeping your internal logic and GUI logic seperated. When clicking on a button you call a handleButton(ActionEvent actionEvent) method that is linked to the button. This method should call a method in one of your other classes that actually contains the logic.
You can get user input through the command line with a scanner:
You can now check this user input string and connect the corresponding method with a switch(s) statement.
I'm not sure WHEN you want to get this input through the command line, but I suggest adding a developer button in your Stage that calls
getUserInput()
when pressed.