javafx populate listview dynamically - java

Javafx newbie here.. I need help with the best way to populate listview. Here is my setup..
I am developing UI tool that is supposed to track the number of virtual machines running in my environment. I get a callback whenever a machine comes up or goes down. How do I update listview based on that data. Controller code -
public class MainOverviewController implements Initializable
{
#FXML
private ListView<String> devicesListView; // Points to the listview
#Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<String> items = FXCollections.observableArrayList("Machines connected");
devicesListView.setItems(items);
...
}
Callback code where I am getting the virtual machine notifications -
class VMChangeListener extends vmlistener
{
...
#Override
public void vmStarted(VM vm)
{
vms.add(vm);
}
#Override
public void vmDisconnected(VM vm)
{
vms.remove(vm);
}
Now my question is, whats the best way to update observablelist, items, from vmStarted and vmDisconnected functions. I could pass the observablelist to the VMChangeListener or use some sort of callbacks? Should I do this in seperate thread?

public class MainOverviewController extends vmListener implements Initializable
{
#FXML
private ListView<VM> devicesListView; // Points to the listview
#Override
public void vmStarted(final VM vm)
{
Platform.runLater(new Runnable()
{
#Override
public void run()
{
devicesListView.getItems().add(vm);
}
});
}
#Override
public void vmDisconnected(final VM vm)
{
Platform.runLater(new Runnable()
{
#Override
public void run()
{
devicesListView.getItems().remove(vm);
}
});
}
...
}

Related

Class A function calls a function in Class B , Once it's completed how to get notification to Class A (Android Java)

Here is the thing that I need to do.
When the user click on a button on an activity , the app must call a function in different class and sent back a notification to the activity. Then the activity shows those information in the main screen.
(Let's say the function is to receive firebase data and add it to a sqlite database. Once the data retrieval is complete ,I want to populate those information in the activity )
Is there any way to do this without using Room database
Currently I am writing the method in the activity class and redirect from there to populate data. Here is a example how I currently use it
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int counter = dataSnapshot.size();
for(int i =0; i<counter;i++){
Post post = dataSnapshot.getValue(Post.class);
// Add to sqlite data
if(i=counter) {
// Populate Data in activity
}
}
}
The Thing i want to do it ,I want to take this function code to a sepeate class and run from the activity and get a callback.
I am a newbie and don't have a idea how to do this. Thank you
I found a solution myself.
first you need to create interface , as an example let's take this >
public interface ActionListenerCallback {
public void onActionSuccess(String successMessage);
public void onActionFailure(Throwable throwableError);
}
After that you need to implement this in the activity where you called the function
public class Act_Reps extends AppCompatActivity implements ActionListenerCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
RealtimeDB RDB= new RealtimeDB(this,Id);
RDB.setCallback(this);
RDB.callingFunction();
}
#Override
public void onActionSuccess(String successMessage) {
Log.d("Log", "Message "+ successMessage);
}
#Override
public void onActionFailure(Throwable throwableError) {
}
}
Here is the class where the function is called
public class RealtimeDB {
ActionListenerCallback callback;
public void setCallback(ActionListenerCallback callback) {
this.callback = callback;
}
public void callingFunction(){
handler.postDelayed(new Runnable() {
#Override
public void run() {
callback.onActionSuccess("Done");
}
}, 5000);
}
}

javafx db load progress indicator

private Popup popupProgressIndicator;
public void showProgressIndicator(ScreensController myController) {
if (popupProgressIndicator == null) {
ProgressIndicator indicator = new ProgressIndicator(-1.0);
indicator.setPrefHeight(200);
indicator.setPrefWidth(200);
indicator.setLayoutX(0);
indicator.setLayoutY(0);
popupProgressIndicator = new Popup();
popupProgressIndicator.getContent().add(indicator);
}
if (!popupProgressIndicator.isShowing()) {
popupProgressIndicator.show(primaryStage);
popupProgressIndicator.centerOnScreen();
}
myController.setDisable(true);
}
public void hideProgressIndicator(ScreensController myController) {
if (popupProgressIndicator.isShowing())
popupProgressIndicator.hide();
myController.setDisable(false);
}
Hi I'm using above code for creating progress indicator in javafx app.
I want to show a progress indicator while loading data from db or saving data to db.
#FXML
private void addDesignationSelect() {
showProgressIndicator(myController);
Task<Void> task = new Task<Void>() {
#Override
protected Void call() throws Exception {
Platform.runLater(new Runnable() {
#Override
public void run() {
// doing some heavy db process
}
});
return null;
}
#Override
protected void succeeded() {
hideProgressIndicator(myController);
}
#Override
protected void failed() {
super.failed();
hideProgressIndicator(myController);
}
};
new Thread(task).start();
}
I have used Task for loading progress bar, but it is not showing the progress bar. What I am doing wrong?
You call show your method showProgressIndicator before you start your Thread/Task. This is the reason why your window is not shown (or shown for a very short time)
You could open the progressBar and after myController.setDisable(true);, you can wait if the task returns a specific value (use Task<Boolean> for example).
The method isDone() will tell you when your Task has end and when to call hideProgressIndicator.
Another option would be to do the UI-Modifications from your Task itself, see the example here Task

Callback inside anonymous functions, not getting the syntax propely

I am trying to implement a void method callback inside an anonymous class and I am a bit lost with the syntax since I working on a large android code-base. I have set a listener to and image button like so:
MyImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OnFollowingClick click;
click = new OnMyClick(param1, param2); // irrelevant for this thread
click.onClick(view); // irrelevant for this thread
// my problem is here!
click.setCallback( what do I pass in here? and where can define my callback?);
}
});
Here is the OnMyClick class and the void callback interface I defined:
public interface CallbackInterface {
public void Callback();
}
public class OnMyClick implements View.OnClickListener {
// I added this
CallbackInterface mCallBack;
public void setCallback(CallbackInterface callback) {
this.mCallBack = callback;
}
// Please call my callback
public void onFollowingChanged() {
if (mCallBack != null) {
mCallBack.Callback();
}
}
// a bunch of code in here that does not matter
// ...
I have to create callback somewhere, so I am guessing I need something like:
public class SomeClass implements CallbackInterface {
#Override
public void Callback() {
System.out.println("Hey, you called.");
}
}
The problem is that listener is an anonymous function, so I don't want to define a separate class somewhere just to serve as a callback.
I looked at this thread but did not give me much info:
How can an anonymous class use "extends" or "implements"?
is there a way to make the anonymous function implement my interface inside the scope, like:
MyImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void Callback() {
// please call me!
}
Not sure if it made sense, buy any help is greatly appreciated.
thx!
Just define the callback anonymously like you are already doing with the View.OnClickListener.
MyImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OnFollowingClick click;
click = new OnMyClick(param1, param2); // irrelevant for this thread
click.onClick(view); // irrelevant for this thread
// your solution is here!
click.setCallback(new CallbackInterface() {
#Override
public void Callback() {
System.out.println("Hey, you called.");
}
});
}
});
If this is not what you meant then please clarify and I will try and help further.

org.eclipse.ui.popupMenus link is grayed out

I created my own eclipse view and is trying to add a link in a popup menu. I am able to add the link but it's grayed out. I wonder how I can activate the link. I just want to be able to click on the link and trigger run(). DeleteAction is the class i want to trigger. SegmentReferencesView is the view I created. Would be very thankful for help.
This is from the plugin.xml:
<extension point="org.eclipse.ui.popupMenus">
<viewerContribution
id="se.test.views.categories.segmentreferences.ui.views"
targetID="se.test.views.categories.segmentreferences.ui.views.SegmentReferencesView">
<action
class="se.test.views.categories.segmentreferences.ui.views.DeleteAction"
enablesFor="1"
icon="icons/Delete.gif"
id="se.test.views.categories.segmentreferences.ui.views.DeleteReferenceAction"
label="Do action"
menubarPath="additions-ext">
</action>
</viewerContribution>
This is the Java class:
public class DeleteAction implements IViewActionDelegate {
#Override
public void init(org.eclipse.ui.IViewPart view) {
super.init(view);
};
#Override
public void run(IAction action) {
}
}
Your view must set the view site 'Selection Provider'. This is used by the menu system to find out what is selected. If you are using a TableViewer or TreeViewer you can just do:
getSite().setSelectionProvider(viewer);
in the view code immediately after you have created viewer (which should be the TableViewer or TreeViewer).
I managed to get the link to work by extending org.eclipse.core.commands.AbstractHandler in the DeleteAction class. I don't know if this is the best way to do it but it's working for now.
public class DeleteAction extends AbstractHandler implements IViewActionDelegate {
#Override
public void init(org.eclipse.ui.IViewPart view) {
// Not used
}
#Override
public void run(IAction action) {
System.out.println("run"); //$NON-NLS-1$
}
#Override
public void selectionChanged(IAction action, ISelection selection) {
// Not used
}
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
return null;
}
}

PopupDateField Listener

I've got a PopupDateField with a ValueChangeListener.
Is there a way to differentiate if the Event was fired from an .setValue()-Call or an User-Input?
I want the Event to be excuted only if the user changes the value, not if it was changed by program.
I've made a dirty Workaround by removing the ValueChangListener before setting a new Value and adding it afterwards, but I'm thankfull for better solutions...
private static void setDateFieldValue(final PopupDateField dateField, Date value) {
Collection<Property.ValueChangeListener> listeners = (Collection<Property.ValueChangeListener>)dateField.getListeners(Property.ValueChangeEvent.class);
listeners.forEach(new Consumer<Property.ValueChangeListener>() {
#Override
public void accept(Property.ValueChangeListener listener) {
dateField.removeValueChangeListener(listener);
}
});
dateField.setValue(value);
listeners.forEach(new Consumer<Property.ValueChangeListener>() {
#Override
public void accept(Property.ValueChangeListener listener) {
dateField.addValueChangeListener(listener);
}
});
}

Categories