How to use NetBeans's JProgressBar? - swing

I am using Netbeans gui, the fast easy drag and drop, lets say a created a JButton, named it "Start", I added an event (ActionPerformed) for this JButton, so I get the following method
private void JButtonActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
}
Then I added a JProgressBar, I want this progressbar to start when I click on this button, and be able to control its speed...etc..

You cannot directly set it's speed, you just have to set it's value. Read the docs for details. BTW JProgressBar is not really Netbeans specific, it is standard swing gui toolkit.

Related

JavaFX FXML Parameter passing from Controller A to B and back

I want to create a controller based JavaFX GUI consisting of multiple controllers.
The task I can't accomplish is to pass parameters from one Scene to another AND back.
Or in other words:
The MainController loads SubController's fxml, passes an object to SubController, switches the scene. There shall not be two open windows.
After it's work is done, the SubController shall then switch the scene back to the MainController and pass some object back.
This is where I fail.
This question is very similar to this one but still unanswered. Passing Parameters JavaFX FXML
It was also mentioned in the comments:
"This work when you pass parameter from first controller to second but how to pass parameter from second to first controller,i mean after first.fxml was loaded.
– Xlint Xms Sep 18 '17 at 23:15"
I used the first approach in the top answer of that thread.
Does anyone have a clue how to achieve this without external libs?
There are numerous ways to do this.
Here is one solution, which passes a Consumer to another controller. The other controller can invoke the consumer to accept the result once it has completed its work. The sample is based on the example code from an answer to the question that you linked.
public Stage showCustomerDialog(Customer customer) {
FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"customerDialog.fxml"
)
);
Stage stage = new Stage(StageStyle.DECORATED);
stage.setScene(
new Scene(
(Pane) loader.load()
)
);
Consumer<CustomerInteractionResult> onComplete = result -> {
// update main screen based upon result.
};
CustomerDialogController controller =
loader.<CustomerDialogController>getController();
controller.initData(customer, onComplete);
stage.show();
return stage;
}
...
class CustomerDialogController() {
#FXML private Label customerName;
private Consumer<CustomerInteractionResult> onComplete
void initialize() {}
void initData(Customer customer, Consumer<CustomerInteractionResult> onComplete) {
customerName.setText(customer.getName());
this.onComplete = onComplete;
}
#FXML
void onSomeInteractionLikeCloseDialog(ActionEvent event) {
onComplete.accept(new CustomerInteractionResult(someDataGatheredByDialog));
}
}
Another way to do this is to add a result property to the controller of the dialog screen and interested invokers could listen to or retrieve the result property. A result property is how the in-built JavaFX dialogs work, so you would be essentially imitating some of that functionality.
If you have a lot of this passing back and forth stuff going on, a shared dependency injection model based on something like Gluon Ignite, might assist you.
I've used AfterBurner.fx for dependency injection, which is very slick and powerful as long as you follow the conventions. It's not necessarily an external lib if you just copy the 3 classes into your structure. Although you do need the javax Inject jar, so I guess it is an eternal reference.
Alternately, if you have a central "screen" from which most of your application branches out you could use property binding probably within a singleton pattern. There are some good articles on using singleton in JavaFX, like this one. I did that for a small application that works really great, but defining all of those bindings can get out of hand if there are a lot of properties.
To pass data back, the best approach is probably to fire custom Events, which the parent controller subscribes to with Node::addEventHandler. See How to emit and handle custom events? for context.
In complex cases when the two controllers have no reference to each other, a Event Bus as #jewelsea mentioned is the superior option.
For overall architecture, this Reddit comment provides some good detail: https://www.reddit.com/r/java/comments/7c4vhv/are_there_any_canonical_javafx_design_patterns/dpnsedh/

MvvmCross with Template10

I'm attempting to create a version of the UWP app for the TipCalc sample here: https://github.com/MvvmCross/MvvmCross-Samples/tree/master/TipCalc
There already is a UWP version in the sample, which works fine. However I'm attempting to use Template10 (https://github.com/Windows-XAML/Template10) and I am having trouble getting the two libraries to work together.
MvvmCross wants me to modify the OnLaunched method, which has a reference to the root Frame. However, Template 10 instead abstracts this method exposing OnStartAsync which has no such reference...
There is an override in Template 10 for CreateRootFrame which seems like the right place to initialize the mvvmcross app, but this doesn't appear to work the way I expected...
Although the launched app DOES navigate to the appropriate page, and does also appear to initialize the view model (a breakpoint on the Start method in the associated VM does get hit), the page itself is blank.
comparing the Visual Tree of both apps reveals that while the existing UWP app from the sample has a Frame:
my Template10 App is loading a Modal Dialog:
I forked the original sample project and added the template 10 version, if you wish to try it for yourself: https://github.com/selaromdotnet/MvvmCross-Samples
Has anyone else been able to integrate MvvmCross with template 10? do you have any idea what i'm doing wrong, and any advice for the best practices in using both of these libraries together?
hmm it turns out that the ModalDialog is the expected behavior for Template10, according to the current docs here: https://github.com/Windows-XAML/Template10/wiki/Docs-|-Bootstrapper
I'm not familiar enough with Template10 to say why this is the case, but it does also say you can change this by overriding OnInitializeAsync, which I did, restoring the original frame in the same way the regular UWP project does:
public override async Task OnInitializeAsync(IActivatedEventArgs args)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
var setup = new Setup(rootFrame);
setup.Initialize();
}
await Task.CompletedTask;
}
This did the trick! I'm sure I still have a ways to go (I believe Template10 has it's own way of restoring state, so I probably shouldn't be doing it here)...
but this at least change finally got me to a working app. IF you know more about what I'm doing incorrectly here or what I should be doing instead, your comments would be greatly appreciated, thanks!

How to change access modifier in stetic gui designer

I'm using Monodevelop and Stetic Gui Designer on Ubuntu.
I want to set the cursor at the end of the text in an Entry-Object. When I try to use entry1.Position (-1)', I get an error "Gtk.Entry.Position is inaccessible due to its protection level". For this issue I found a possible answer here Why is the control inaccessible due to its protection level?. So I would like to try to change the access modifier. But I can't see where to do this in stetic gui designer.
Thank you!
I have the same issue, my answer is: declare public variables instead to try change the acces modifler; this is the only way I can pass data betwen the controls in xamarin; I wished give you a better answer.
O.o en your project folder there is one named gtk-gui inside there are files whith the same name of the windows/dialogs, in the begining lines are the acces modiflers of the controls
{// This file has been generated by the GUI designer. Do not modify.
namespace gtk2
{
public partial class Dialog
{
private global::Gtk.VBox vbox3;
private global::Gtk.Entry entry2;
private global::Gtk.Button buttonCancel;
private global::Gtk.Button buttonOk;
but seems like you can't change it

I need explanation on what RelayCommand is

I was reading a .net source code on MVVM in windows 8 development. I saw some funny objects Called RelayCommand. I tried to read up on it. But I still dont understand what RelayCommand is about. Any body help with a simple explanation of what it is and why I need to use it.
Thanks alot
i try to do my best to explain clearly.
In XAML and MVVM you have some properties called Commands in controls descending from ButtonBase. this Command property allows you to bind (using data binding {Binding propertyname}) to a property in your viewmodel of a type who implements the ICommand interface. Later, the button can execute that "Command" when the user makes click.
if you implements the ICommand interface in a class, call it HelloCommand for example, you have a CanExecute and Execute methods, CanExecute returns a bool and is used to evaluate if the command can be executed. Execute is the code the command is going to execute when the user clicks the button.
This means, for each button you need to create a class implementing ICommand. RelayCommand is a class who implements ICommand and allows you to pass as parameters in the constructor the methods to execute in the CanExecute and Execute ICommand methods. This way you don't need to implement over and over the ICommand interface.
Hope is clear enought...

Use javaJVMLocalObjectMimeType for local DnD and serialization for external drop

I'm working on an component in which smaller components are stacked. The user should be able to change the order of these components using drag and drop. I've made this work by implementing an TransferHandler that accepts a local reference DataFlavor (javaJVMLocalObjectMimeType) of the underlying data models. This works fine.
Now I would like to also run my application a second time and be able to drag on of my components from one application into the other. In this case I want to bundle the necessary data of the drag source into a serializable object to reconstruct the object at the drop application and use a serializable DataFlavor for that. I don't want to use object serialization in both situations.
How do I decide if my drag operation is originated in the same JVM so that I can decide to use the object reference or the serialized version of the data. The official swing DnD documentation mentions that it is possible to mix local and serialization flavors but it doesn't tell how to make best use of this.
Edit
Here's how I create the flavor in my DataModelTransferable
public static DataFlavor localFlavor;
static {
try {
localFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=\"" + ArrayList.class.getName() + "\"");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
...
#Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { localFlavor };
}
And in my TransferHandler I do this
#Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataModelTransferable.localFlavor);
}
As I said, this works fine locally, but if I drag from one to another instance the drag is accepted which leads to a java.io.IOException: Owner failed to convert data on the drop application and a java.io.NotSerializableException: alignment.model.DataModel on the drag source application. This is ok but the drag shouldn't be accepted on another app in the first place.
I'm using a ArrayList as I also want to be able to drag several objects at once, fyi.
I just found out myself what the problem was. I wrote a wrapper class DataModelListfor my ArrayList of DataModels that doesn't implement Serializable and modified the declaration of my data flavor to this:
localFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=\"" + DataModelList.class.getName() + "\"");
After that, this flavor is not considered to be equal between the drag source and the drop target if they are not within the same JVM.
I conclude that it's not possible to use local object reference flavors directly with classes that implement Serializable. If someone knows where this is documented I would be glad to hear about it.
Your object reference flavor will normally be different for each running JVM. So you first check whether the Transferable supports your 'object reference flavor' before you ask for the 'serialized dataflavor version'.