I'm exploring JavaFX and I must say I'm little disappointed with the lack of learning material. I'm rookie to JavaFX but I have some experience with Swing.
How can I place the slider vertically?
And one more question,I want the Thumb to be draggable only to the Tick marks,not in the intermediate space,how can I achieve that?
How can I place the slider vertically?
Use setOrientation
slider.setOrientation(Orientation.VERTICAL);
I want the Thumb to be draggable only to the Tick marks,not in the intermediate space,how can I achieve that?
Use setSnapToTicks after setting an appropriate tick unit, count and block increment.
slider.setMajorTickUnit(0.25f);
slider.setMinorTickCount(1);
slider.setBlockIncrement(0.125f);
slider.setSnapToTicks(true);
Sample app:
import static javafx.application.Application.launch;
import javafx.application.*;
import javafx.event.*;
import javafx.geometry.Orientation;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class VerticalSliderSample extends Application {
#Override public void start(Stage stage) {
Slider slider = new Slider(0, 1, 0.5);
slider.setShowTickMarks(true);
slider.setShowTickLabels(true);
slider.setMajorTickUnit(0.25f);
slider.setMinorTickCount(1);
slider.setBlockIncrement(0.125f);
slider.setSnapToTicks(true);
slider.setOrientation(Orientation.VERTICAL);
VBox layout = new VBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().setAll(slider);
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
I'm exploring JavaFX and I must say I'm little disappointed with the luck of learning material.
Email (constructive) feedback on Oracle developed JavaFX documentation to:
jfx-docs-feedback_ww#oracle.com
With Java 8 and with it JavaFX 8 and its corresponding SceneBuilder coming (at this time of post its still Java 7 GA with SceneBuilder 2.2), people should get more familiar using it. It is a powerful tool for people who don't know each special attribute from its hat off and want to practice the Inversion of Control principle the new JavaFX and its FXMLs offer us.
Here is an example out of one of my projects to show the most popular
properties on each Node
Though I can sometimes understand you, for being frustrated especially if some new features come out, that some higher class examples what this Node can do and maybe find it easier, but as a programmer you need to learn to work with the corresponding API. It would be nice to have a part of each API documentation like in the PHP community where you can maintain it as a wiki.
Related
I am developing an interactive program to simulate power grid switching and I have it working beautifully with the rendering tools in the Processing library. Now I want to add a user interface with menus and buttons, etc. I found the ControlP5 library and it seems like what I need, but I am having a hard time importing it into my project. I have the most current ControlP5 folders/files on my machine and I have added them as a project library. IntelliJ is recoginizing my import statement, but it won't let me declare a variable using the ControlP5 class.
My import statement seems good to go... it is greyed out as an unused import.
But the very last line in the code copied here generates an error "Cannot resolve symbol 'ControlP5'"
import processing.core.PApplet;
import processing.core.PConstants;
import processing.event.*;
import controlP5.*;
public class Main extends PApplet {
Viewport viewport = new Viewport();
Click click = new Click();
UserInterface ui = new UserInterface();
ControlP5 cp5;
Here is a screen shot of my libraries. I have the Processing Core library which I am using for drawing tools, and I want to also use classes from the ControlP5 library which I believe I have correctly linked as an external library here.
Here is a screen shot of my module dependencies.
Here is a screen shot of the bottom of my project tree. I can see that Processing is correctly shown, but I do not see the ControlP5 library here.
I have tried multiple different methods of adding just certain subfolders of the "controlp5-master" folder which I downloaded with no luck.
I have also searched through Google, Processing forum, and Stack Overflow and can't find an answer.
Any advice?
My problem was solved on the Processing Forum. I simply referenced the wrong file when establishing my external library. The ControlP5 download package includes a jar file that is buried several folders deep. Once I pointed the library to that jar file, I was in business.
I am trying to use the virtual robot simulator designed by team Beta#8397 and got the simulator working. Now I am trying to put my teams code in so that we can test it. I am getting a bunch of errors that I'm pretty sure all stem from one error that some of the imports aren't importing. Also, my teams code builds fine when not combined with the simulator. I'm not amazing a java, but do know how most of my teams code works. Here is the code for the imports
package org.firstinspires.ftc.teamcode.Robovines;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.robot.Robot;
import com.qualcomm.robotcore.util.Hardware;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.util.Range;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DigitalChannel;
The errors are on lines 4,5 and 13 with the package being the first line. On line 4 the error is that com.qualcomm.robotcore.robot.Robot does not exist. On lines 5 and 13 the error is that symbol "Hardware" and "DigitalChannel" respectively cannot be found.
Here is the whole project
#BookNinja S : The link you provided doesn't take me to your code.
The virtual robot simulator provides a subset of the functionality of the FTC SDK.
That subset has increased over time, but as commented by #CrazyCoder, the Controller module does not include the com.qualcomm.robotcore.robot.Robot, com.qualcomm.robotcore.util.Hardware, or com.qualcomm.robotcore.hardware.DigitalChannel classes.
With regard to DigitalChannel, this type of sensor is not part of the virtual robot functionality. But, it would be easy to add a dummy DigitalChannel class to the controller (so, getState would always return false, and setState would do nothing).
Dummy classes could probably be used for Hardware and Robot as well, but that would depend upon how you are using those classes in your op modes.
If you submit this as an issue on the virtual_robot github repository, with a link to your project or an explanation of how you are using the Hardware and Robot classes, we (ftc team Beta 8397) can probably add the needed functionality to the virtual_robot project.
I've got 2 Pages: My Main-Page and my Settings-Page - both in XAML and xaml.cpp as code behind. I've also got a class where I want to work with the values of the Settings-Class. (As I already asked in another post) I can save the values from the Settings-Page in the cache using the XAML-tag NavigationCacheMode="Enabled". Using this my SliderValue stays the same even when I switch to my Main-Page and back again to Settings-Page.
What I want to do now: Save the value of the slider as soon as it is changed and store this value to a variable to which I have access from a third Class.
What I've got so far (not working, always showing initialvalue '70'):
Settings.xaml.cpp:
UWPApp::Settings::Settings()
{
InitializeComponent();
quality = sldQuality->Value;
}
int Settings::getQuality() {
return quality;
}
void DirectX12XamlApp::Settings::sldQuality_ValueChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::Primitives::RangeBaseValueChangedEventArgs^ e)
{
quality = sldQuality->Value;
}
In Settings.xaml.h:
public:
Settings();
int getQuality();
private:
int quality;
In Settings.xaml:
<Slider x:Name="sldQuality" ValueChanged="sldQuality_ValueChanged" HorizontalAlignment="Left" Margin="38,325,0,0" VerticalAlignment="Top" Width="168" Value="70"/>
//initial-Value = 70, which is ALWAYS showing when using getQuality() in an other Class
In my logicclass I want to use it like this:
int quality = Settings::getQuality();
Quick answer: save the value of the slider in a public static property, ideally in the separate class.
Recommended answer:
You should use MVVM structure in your app, so you would have a separate layer for storing your settings data. You can save the value of the slider in the singleton service, ideally injected through the IoC container. You may also want to save it in the local storage so the settings are saved when the user closes the app.
MVVM is really a standard of XAML-based apps, and the concept is not difficult to learn and implement.
Sounds like you're looking for a way to store and retrieve settings data for your app. Features for that have been built into the UWP. Please refer to this article from the Windows Dev Center. Examples on that page are for C#, but all the links to UWP API references contain code snippets for C++ as well.
I am new in kotlin. I have found and tried to use synthetic method instead of annoying method findViewById in my Activity class, but I have found "If we want to call the synthetic properties on View (useful in adapter classes), we should also import kotlinx.android.synthetic.main.view.*." But I can't figure out how it exactly works? Is there any examples?
Simple example from https://github.com/antoniolg/Kotlin-for-Android-Developers
import kotlinx.android.synthetic.item_forecast.view.*
class ForecastListAdapter() : RecyclerView.Adapter<ForecastListAdapter.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindForecast(forecast: Forecast) {
itemView.date.text = forecast.date.toDateString()
}
}
}
No need to write
val view = itemView.findViewById(R.id.date) as TextView
view.text = forecast.date.toDateString()
Just
itemView.date.text = forecast.date.toDateString()
Simple and effective!
Kotling 1.1.4 out
Further information : https://antonioleiva.com/kotlin-android-extensions/
You need to enable Kotlin Android Extentions by adding this to your build.gradle:
apply plugin: 'org.jetbrains.kotlin.android.extensions'
androidExtensions {
experimental = true
}
Since this new version of Kotlin, the Android Extensions have incorporated some new interesting features: caches in any class (which interestingly includes ViewHolder)
Using it on a ViewHolder (or any custom class). Note that this class should implement LayoutContainer interface:
class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView),
LayoutContainer {
fun bind(title: String) {
itemTitle.text = "Hello Kotlin!"
}
}
You need
import kotlinx.android.synthetic.row_wall.view.*
And later something along the lines of:
convertView.titleText.text = item.title
The point is that the view.* introduces extensions to the View class.
Try
class CustomViewModel(val baseView: View) {
val firstName = baseView.firstName
val lastName = baseView.lastName
}
View object exposes the views
ref:https://discuss.kotlinlang.org/t/unable-to-use-kotlin-android-extension-in-adapter-class/2890
If you are using the latest version l;.you don't have to add experimental = true to it.
in Project level Gradle
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21'
And in app level Gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions' //These should be on the top of file.
and in dependencies..
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21'
and import below as
import kotlinx.android.synthetic.main.your_layout_file_name.view.*
and example
import kotlinx.android.synthetic.main.item_animal.view.*
class AnimalVH(parent: ViewGroup, layoutID: Int) : BaseViewHolder<Animal>(parent, layoutID) {
override fun bindData(animal: Animal) {
itemView.tv_animal.text = animal.title
}
}
where BaseViewHolder is
abstract class BaseViewHolder<T>(parent: ViewGroup, layoutID: Int) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(layoutID, parent, false)
) {
abstract fun bindData(model: T)
}
It means you have to place this line at the beginning of your source file:
import kotlinx.android.synthetic.main.view.*
So now instead of, for example, findView(R.id.textView) as TextView you would write just textView. The latter is a synthetic extension property located in the package kotlinx.android.synthetic.main.view, that's why you have to import everything from it.
There's a tutorial on the official site, take a look.
FYI: Data binding is recommended over synthetic for view lookups.
Comment from a DA for Android from Google on Reddit
Hey! Developer Advocate for Android at Google here!
I wanted to add a bit of background here. Kotlin Extensions with
synthetic views was never intentionally “recommended” though that
shouldn’t be taken as a recommendation to not use them. If they're
working for you please feel free to continue using them in your app!
We’ve been shifting away from them (e.g. we don’t teach them in the
Udacity course) because they expose a global namespace of ids that’s
unrelated to the layout that’s actually inflated with no checks
against invalid lookups, are Kotlin only, and don't expose nullability
when views are only present in some configuration. All together, these
issues cause the API to increase number of crashes for Android apps.
On the other hand, they do offer a lightweight API that can help
simplify view lookups. In this space it's also worth taking a look at
Data Binding which also does automatic view lookups - as well as
integrates with LiveData to automatically update your views as data
changes.
Today, there's a few options in this space that work:
Data Binding is the recommendation for view lookup as well as binding,
but it does add a bit of overhead when compared to Android Kotlin
Extensions. It's worth taking a look to see if this is a good fit for
your app. Data Binding also allows you to observe LiveData to bind
views automatically when data changes. Compared to Kotlin Extensions,
it adds compile time checking of view lookups and type safety. Android
Kotlin Extensions is not officially recommended (which is not the same
as recommendation against). It does come with the issues mentioned
above, so for our code we're not using them. Butter Knife is another
solution that is extremely popular and works for both Kotlin and the
Java Programming Language. Reading through the comments here there's a
lot of developers that are having great luck with Kotlin Extensions.
That's great - and something we'll keep in mind as we look at ways to
continue improving our APIs. If you haven't taken a look at Data
Binding, definitely give it a shot.
As an aside, our internal code style guide is not intended to be
directly applied outside of our codebase. For example, we use
mPrefixVariables, but there's no reason that every app should follow
that style.
I am using Qt5 beta and trying to embed a QWidget-based object into QML. The goal is to use QML as much as possible, and only use QWidget objects where QML does not do what I need. I found a link explaining how to do this for Qt4.7, but I have not found any information explaining how to do this in Qt5.
http://doc.qt.digia.com/4.7/declarative-cppextensions-qwidgets.html
The same example is also available in the Qt5 examples folder under:
examples\qtquick1\declarative\cppextensions\qwidgets
Unfortunately, this example uses QtQuick 1, rather than QtQuick 2, and I would like to use the new features of Qt5. I actually want to embed a qwt widget, but as a first step I would be happy to embed any simple QWidget-based object.
Can anybody help me get the example working under Qt5 / QtQuick 2 ?
Qt Quick 2 uses a scene graph for efficient rendering on the GPU. Unfortunately this makes it impossible to embed classic widgets into the scene. The old approach to embed such widgets with the help of QGraphicsProxyWidget works only with Qt Quick 1, because internally it uses a QGraphicsView for all the heavy lifting and QGraphicsProxyWidget is meant to be used with it.
As of now there are no plans to enable embedding classic QWidgets into the scene graph I know of. I think this is rather unlikely to change, because the concepts of QPainter, the painting framework used for the classic widgets, and the new scene graph doesn't play well with each other.
There some efforts to develop new widgets sets specifically tailored for the needs of QML, but none of them are as powerful and mature as the classic widgets. The most prominent ones are the QML Quick Controls, bundled with Qt since version 5.1.
If you really depend on QWT my advice would be to stick with Qt Quick 1.1 for now. It's still bundled with Qt 5, probably for cases like yours. That way you won't take advantage of the new scene graph, though.
You can embed QWidget to QML by using QQuickPaintedItem class:
http://doc.qt.io/qt-5/qquickpainteditem.html
Qt5 has an example:
http://doc.qt.io/qt-5/qtquick-customitems-painteditem-example.html
You should implement an inherent of QQuickPaintedItem with private widget attribute, that you want to embed. Provide paint method, that just render the QtWidget and provide mouse and other event transmitting from inherit of QQuickPaintedItem to embed QtWidget.
There's also QSG (Qt scene graph API), but my experience with that thing wasn't smooth. I believe the clue in multithreading (performing rendering in the different thread (not the Qt GUI thread one, however on Windows that's not true and all is done in main GUI thread).
I've implemented embedding of QCustomPlot, here's link: github.com/mosolovsa/qmlplot
What could be done is to render the widget to an image and upload as texture.For interaction someone needs to forward events like mouseClick or keyPressed from the sceneGraph, translate to widget coordinates, pass on, render and upload texture again. Just an idea :)
The recommended approach is to stay with a QWidget based application and embed the QML parts using QWidget::createWindowContainer.
Further to Julien's answer - a simple way to achieve this is to use QQuickWidget to display the QML scene, and then add a regular QWidget as a child of the QQuickWidget. You can also add a simple intermediate QObject to anchor the QWidget to an item in the scene.
E.g.:
In main.qml:
Item {
... // layouts, extra items, what have you
Item
{
objectName: "layoutItem"
anchors.fill: parent
}
... // more layouts, extra items, etc.
}
widgetanchor.h:
class WidgetAnchor: public QObject
{
ptr<QWidget> _pWidget;
QPointer<QQuickItem> _pQuickItem;
public:
WidgetAnchor(QWidget* pWidget, QQuickItem* pItem)
: QObject(pWidget), _pWidget(pWidget), _pQuickItem(pItem)
{
connect(_pQuickItem, &QQuickItem::xChanged, this, &WidgetAnchor::updateGeometry);
connect(_pQuickItem, &QQuickItem::yChanged, this, &WidgetAnchor::updateGeometry);
connect(_pQuickItem, &QQuickItem::widthChanged, this, &WidgetAnchor::updateGeometry);
connect(_pQuickItem, &QQuickItem::heightChanged, this, &WidgetAnchor::updateGeometry);
updateGeometry();
}
private:
void updateGeometry()
{
if (_pQuickItem)
{
QRectF r = _pQuickItem->mapRectToItem(0, QRectF(_pQuickItem->x(), _pQuickItem->y(), _pQuickItem->width(), _pQuickItem->height()));
_pWidget->setGeometry(r.toRect());
}
}
};
In main.cpp:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto pqw = new QQuickWidget;
pqw->setSource(QUrl::fromLocalFile("main.qml"));
pqw->setResizeMode(QQuickWidget::SizeRootObjectToView);
pqw->setAttribute(Qt::WA_DeleteOnClose);
auto pOwt = new MyWidget(pqw);
if (auto pOverlayItem = pqw->rootObject()->findChild<QQuickItem*>("overlayItem"))
new WidgetAnchor(pOwt, pOverlayItem);
pqw->show();
return app.exec();
}
The documentation states that using QQuickWidget has advantages over QQuickView and QWidget::createWindowContainer, such as no restrictions on stacking order, but has a 'minor performance hit'.
Hope that helps.