JavaFX Scene builder. how to get value from a Slider? - slider

Good evening, gentleman's.
I am learning to use Scene Builder to create a GUI from scratch, which means i did not create any object at main class; the Scene Builder itself created everything at the FXML file.
So, in this window of mine i have two sliders and a start button, who will require the int values of both sliders to run.
start method would be like
public void start(slider1 int value, slider2 int value){ code }
Sliders in the fxml file, they have a id:
<Slider fx:id="escritoresQuant" blockIncrement="1.0" layoutX="85.0" layoutY="314.0" majorTickUnit="1.0" max="4.0" minorTickCount="0" prefHeight="14.0" prefWidth="71.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" />
<Slider fx:id="leitoresQuant" blockIncrement="1.0" layoutX="85.0" layoutY="349.0" majorTickUnit="1.0" max="6.0" minorTickCount="0" prefHeight="14.0" prefWidth="71.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" />
How can i get int value from both those sliders?
Thanks a lot

Set an eventListener for when the user changes the slider, if you haven't already.
onMouseReleased="#onSliderChanged"
//in the FXML
<Slider fx:id="escritoresQuant" blockIncrement="1.0" majorTickUnit="5.0"
max="26.0" min="-26.0"
minorTickCount="1" onMouseReleased="#onSliderChanged"
prefHeight="38.0" prefWidth="287.0" showTickLabels="true"
showTickMarks="true" snapToTicks="true"
style="-fx-background-color: white;" value="3.0"
BorderPane.alignment="BOTTOM_CENTER" />
//in the Controller class
#FXML
private Slider escritoresQuant;
#FXML
public void onSliderChanged() {
int sliderValue = (int) escritoresQuant.getValue();
System.out.println(sliderValue + " ");
txtOut.setText(escritoresQuant.getValue()+" ");
}

I think that this is what you are looking for.
You should be able to get the value as a double by calling escritoresQuant.getValue()

Related

How to programatically scroll a ScrollViewer to the top in uwp xaml?

I have applied ScrollViewer to a user control which automatically scrolls to the bottom if height exceeds.
Now my page is closing having the scrollbar at the bottom position and it remains at the bottom when I reopen the page.
How can I reset the scrollbar to the top every time I open that page.
<ScrollViewer x:Name="myScrollViewer">
<Grid Margin="0,0,0,40">
</Grid>
</ScrollViewer>
ScrollViewer provides the ChangeView method, which can be used to adjust the scrolling state:
// scroll to top
myScrollViewer.ChangeView(0, 0, 1);
You can call this method after the page is loaded to scroll the ScrollViewer to the top.
Update
If you want to access the control on the page in the ViewModel, there are two ways:
1. Public control
This method means that you need to create a public page instance, and then expose the control in XAML, as shown below:
ScrollViewerPage.xaml
<ScrollViewer x:Name="myScrollViewer" x:FieldModifier="public">
<!-- your code -->
</ScrollViewer>
ScrollViewerPage.xaml.cs
public sealed partial class ScrollViewerPage : Page
{
public static ScrollViewerPage Current;
public ScrollViewerPage()
{
this.InitializeComponent();
Current = this;
}
}
After the page is loaded, you can use this code to access the ScrollViewer.
ScrollViewerPage.Current.myScrollViewer.ChangeView(0, 0, 1);
2. Define control variable/property in ViewModel
You can define a variable or property (public) of type ScrollViewer in the ViewModel, and then assign a value to the property when the page loads, and then you can access the ScrollViewer by accessing the property in the ViewModel.
MyViewModel.cs
public class MyViewModel
{
public ScrollViewer MyScrollViewer { get; set; }
// Other code
}
ScrollViewerPage.xaml.cs
public ScrollViewerPage()
{
this.InitializeComponent();
var vm = new MyViewModel();
vm.MyScrollViewer = myScrollViewer;
}

how can I manipulate the View (XAML) from my ViewModel? (Xamarin Forms)

how can I manipulate the View (XAML) from my ViewModel?
For example, I have a detail view for an object. The view have a grid. Depending of the object, the Grid should have different count of rows and cols. The number of cols and rows is set in the object details. Is it possible to do that from the ViewModel or I have to do that in the View .cs?
I open the detail view from a ListView (OnItemSelect):
await Navigation.PushAsync(new AlgoDetailPage(new AlgoDetailViewModel(algo)));
AlgoDetailPage - Here I want to add the cols and rows, depending of the object.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NotsanHessen.Views.AlgoDetailPage"
Title="{Binding Algo.Title}">
<StackLayout>
<Grid>
</Grid>
</StackLayout>
</ContentPage>
The AlgoDetailPage.cs:
public partial class AlgoDetailPage : ContentPage
{
AlgoDetailViewModel viewModel;
public AlgoDetailPage(AlgoDetailViewModel viewModel)
{
InitializeComponent();
BindingContext = this.viewModel = viewModel;
}
public AlgoDetailPage()
{
InitializeComponent();
BindingContext = viewModel;
}
}
The ViewModel:
public class AlgoDetailViewModel : BaseViewModel
{
public Algo Algo { get; set; }
public AlgoDetailViewModel(Algo algo = null)
{
this.Algo = algo;
// Rows: algo.Rows
// Cols: algoCols
}
}
you don't manipulate the View from the VM. Instead, the View should use the VM's properties to determine it's layout. In this case, you would add the rows and cols in the View based on the data from the VM.
Technically, you don't have to do that from View itself. If you want to be exactly orthodox you can create the custom control that will handle the binding for you. Personally I wouldn't recommend this level of following some theory, but if you want it it is possible.
Other than that you may try to take a look at CollectionView that will appear in Xamarin 4.0 it may be close to what you have requested (you haven't specified exactly how column width is to be handled). Also there could be some 3rd party components that can handle this.
If it is a complex grid, have two different content views. In your xaml, based on the condition show or hide the content views. In this way, you can keep the code manageable any time.
And if you REALLY necessarily want to access the View from ViewModel, you can Bind View's Load Event to a Command and pass the View itself as a Command Parameter.

In Xaml, how do you change the value of the SolidColorBrush programmatically?

Hi I have a xaml app that has one particular color used in many places on a page. I want to programmatically change the value of this color. I don't want to have to update every object's color individually. I have tried this:
<Grid
Background="{Binding GalleryViewBrush, Mode=TwoWay}"
Grid.Row="0"
Grid.Column="0">
Then in codebehind:
public Brush GalleryViewBrush { get; set; }
GalleryViewBrush = new SolidColorBrush(Colors.Red);
But the color never works. I have tried xbind too but no luck
thanks
Your view model needs to implement INotifyPropertyChanged, and then fire the PropertyChanged event when your brush property changes. Conventionally that's done in the setter for the property.
private Brush _myBrush;
public MyBrush {
get { return _myBrush; }
set {
_myBrush = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyBrush));
}
}
If your get/set for GalleryViewBrush in your question is what's actually in the code sample in your question, that's likely to be the issue.
Mind typos in my code sample above, I'm hammered ATM.

How to make an animated ListView?

I realised that storyboard only working with silverlight Wp 8.1 apps. So what about non-silverlight? I've want to make an animation of smooth appearing elements of listview after page transition. How can i make it? I check VisualStateGroup, but to make animation like that i should make a code behind. Is it possible to make only by XAML?
If you want to use XAML you can use the built in transitions which you can choose from already predefined ones. One drawback is that you can't create your own transitions this way.
To change the item transitions change the ItemContainerTransitions property on your ListView:
<ListView x:Name="MyListView">
<ListView.ItemContainerTransitions>
<TransitionCollection>
<!--<ContentThemeTransition HorizontalOffset="0"/>-->
<!--<EntranceThemeTransition />-->
<!-- <AddDeleteThemeTransition /> -->
<!-- <RepositionThemeTransition/> -->
<!-- <ReorderThemeTransition /> -->
<!-- <PopupThemeTransition/> -->
<!-- <EdgeUIThemeTransition Edge="Top"/> -->
</TransitionCollection>
</ListView.ItemContainerTransitions>
</ListView>
You can combine them if you want. Just uncomment any of the above code and use which suits you best.
You can create custom item animations when you extend ListView and override the GetContainerForItemOverride or PrepareContainerForItemOverride methods. You can use XAML if you want for example a Storyboard defined in as a StaticResource. Here is an example:
public class MyListView : ListView
{
protected override DependencyObject GetContainerForItemOverride()
{
var container = base.GetContainerForItemOverride();
var da = new DoubleAnimation();
// init da...
var sb = new Storyboard();
// initi sb with container
sb.Begin();
return container;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
var da = new DoubleAnimation();
// init da...
var sb = new Storyboard();
// initi sb with element
sb.Begin();
}
}

Eclipse plugin development - How to add Radio buttons in WizardNewFileCreationPage

I am using WizardNewFileCreationPage to create a New File
public void addPages() {
mainPage = new WizardNewFileCreationPage("FILE", getSelection());
mainPage.setTitle("New File");
mainPage.setDescription("Add new file");
addPage(mainPage);
}
I want to add some Radio Buttons to it representing file extensions in this wizard so that users can select one of them as a file extension.
The WizardNewFileCreationPage is not meant to be extended with custom controls. From its JavaDoc:
Subclasses may override
getInitialContents
getNewFileLabel
Subclasses may extend
handleEvent
If you still want to add the radio buttons 'at your own risk', you can try to override createAdvancedControls and append you controls to the parent after calling super.
protected void createAdvancedControls(Composite parent) {
super.createAdvancedControls( parent );
Button radioButton = new Button( parent, SWT.RADIO );
// ...
}
Note that the layout of parent (currently) is a single-columned GridLayout, set the layout data accordingly.