JavaFX RadioButton setSelected() not working - javafx-8

I'm creating a JavaFX FXML application and haven't been able to use the setSelected() method to set one of two radio buttons by default in a toggle group. The toggle group is functioning normally, so it appears the radio button variables are being set correctly. However, radio1 is not selected as expected.
Following are the relevant sections of my application.
System Information
Product Version: NetBeans IDE 8.1 (Build 201510222201)
Updates: NetBeans IDE is updated to version NetBeans 8.1 Patch 1
Java: 1.8.0_171; Java HotSpot(TM) 64-Bit Server VM 25.171-b11
Runtime: Java(TM) SE Runtime Environment 1.8.0_171-b11
System: Mac OS X version 10.13.4 running on x86_64; UTF-8; en_US (nb)
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="400.0"
xmlns="http://javafx.com/javafx/8.0.141"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="inventory.view.AddPartController">
<fx:define>
<ToggleGroup fx:id="toggleGroup" />
</fx:define>
<children>
<RadioButton fx:id="radio1" mnemonicParsing="false"
onAction="#handleRadio1" prefHeight="30.0" prefWidth="100.0"
text="radio1" toggleGroup="$toggleGroup"
AnchorPane.rightAnchor="125.0" AnchorPane.topAnchor="25.0" />
<RadioButton fx:id="radio2" mnemonicParsing="false"
onAction="#handleRadio2" prefHeight="30.0" prefWidth="100.0"
text="radio2" toggleGroup="$toggleGroup"
AnchorPane.rightAnchor="25.0" AnchorPane.topAnchor="25.0" />
</children>
</AnchorPane>
Controller
package inventory.view;
import inventory.InventorySystem;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
public class AddPartController {
#FXML
private ToggleGroup toggleGroup;
#FXML
private RadioButton radio1;
#FXML
private RadioButton radio2;
#FXML
private void initialize() {
toggleGroup = new ToggleGroup();
radio1 = new RadioButton();
radio1.setToggleGroup(toggleGroup);
radio1.setSelected(true);
radio2 = new RadioButton();
radio2.setToggleGroup(toggleGroup);
}
}
Where am I going wrong?

You are reinitializing the radio button. The FXML will inject the reference, you are then overriding it. You also define the toggle group in your FXML - so you don't need that either.
Change your code to look like this:
#FXML
private void initialize() {
radio1.setSelected(true);
}

Don't initialize the buttons again . These lines would be enough.
#FXML
private void initialize() {
radio1.setToggleGroup(toggleGroup);
radio1.setSelected(true);
radio2.setToggleGroup(toggleGroup);
}

Related

Creating a admin controller using adminhtml id

I'm trying to add a controller accessible on admin menu through url:
https://dev.m2t2.com/admin_k1tgag/admin/helloWorld/index/key/0195fab99cc865bb756a77e8fe5ceedb6f8eee97de91d569398d383cef4f0d81/
Generated by the XML code inserted below. But it keeps returning
Invalid security or form key. Please refresh the page.
Router.xml :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="adminhtml">
<module name="Study_Admin" before="Magento_Backend"/>
</route>
</router>
</config>
In menu i inserted:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Study_Admin::greetings" title="Greetings" translate="title" module="Study_Admin" parent="Magento_Backend::content" sortOrder="50" dependsOnModule="Study_Admin" resource="Study_Admin::greetings"/>
<add id="Study_Admin::greetings_helloworld" title="Hello World" translate="title" module="Study_Admin" parent="Study_Admin::greetings" sortOrder="10" dependsOnModule="Study_Admin" action="adminhtml/helloWorld" resource="Study_Admin::greetings"/>
</menu>
</config>
But when i access the controller through the menu i have no success. I started debugging and i checked that non-custom controllers extends \Magento\Backend\App\Action class to pass validations inside magento routing core flow. I did the same but i still have no success.
Below my controller class:
<?php
namespace Study\Controller\Adminhtml\HelloWorld;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\App\Action\Context;
use ‌Magento\Framework\App\ActionInterface;
class Index extends \Magento\Backend\App\Action
{
const MENU_Id = "Study_Admin::greetings_helloworld";
protected $resultPageFActory;
public function __construct(Context $context, PageFactory $resultPageFActory)
{
parent::__construct($context);
$this->resultPageFActory = $resultPageFActory;
}
public function execute()
{
$resultPage = $this->resultPageFActory->create();
$resultPage->setActiveMenu(static::MENU_Id);
$resultPage->getConfig()->getTitle()->prepend(__('Hello World'));
return $resultPage;
// TODO: Implement execute() method.
}
}
The file structure is :
Thanx in advance, and take care.
It works... was just my namespace in Controller's class that had an error
'namespace Study\Admin\Controller\Adminhtml\Helloworld;'

Cant compile JavaFX application with jdk11 on Ubuntu

I am trying to run a JavaFX application on eclipse. Easy code like printing out hello world or entering a name are working. But when I am trying to run a modified fmxl-file I am getting different errors.
At first I got the message:
Error occurred during initialization of boot layer
java.lang.module.FindException: Two versions of module jurt found in /usr/share/java (jurt-6.0.7.jar and jurt.jar)
But when deleting the versions which are double the errormessage changed
to:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x65d71619) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0x65d71619
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at application.Main.start(Main.java:17)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application application.Main
This is my main class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
//BorderPane root = new BorderPane();
Parent root = FXMLLoader.load(getClass().getResource("/application/scene1.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
And this my fxml-file:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.MainController">
<!-- TODO Add Nodes -->
<children>
<Button layoutX="185.0" layoutY="140.0" mnemonicParsing="false" onAction="#generateRandom" text="Münzwurf" />
<Label layoutX="14.0" layoutY="63.0" text="Kopf oder Zahl du hast die Wahl:" />
<TextField layoutX="241.0" layoutY="60.0" prefWidth="200.0" text="Kopf oder Zahl ?" />
</children>
</AnchorPane>
My class controller just create a random number which I want to be printed out.
I imported the libraries with external jars into my project.
I am running this on a ubuntu 18.4 maschine with jdk11, openjfx11 and jre1.8/11
My next step will be to try the code on jdk1.8 with openjfx8
Does anyone have a clue how to fix this ?

Proper way to import custom UI into scene builder?

I am trying to build an UI with custom control. But having trouble to make the scene builder wo work properly with the custom part.
My custom control:
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="800.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
...other code
</children>
</fx:root>
Room.fxml, which use the custom class:
...other code
<?import ag.ctrl.*?>
<?scenebuilder-classpath-element ../../target/classes/ag/ctrl?>
...other code
<BorderPane prefHeight="200.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<top>
<TeamArea />
</top>
</BorderPane>
Controller of the custom ui:
package ag.ctrl;
...
public class TeamArea extends AnchorPane {
...
public TeamArea() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"/ui/TeamArea.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
...
}
and all those files' path:
TeamArea.fxml: /resource/ui/TeamArea.fxml
Room.fxml: /resource/ui/Room.fxml
TeamArea.class: /target/classes/ag/ctrl/TeamArea.class
I try many different approach before.End up with scene builder's class not found error or others.
Currently I am having an unresolved class error at the TeamArea Node in scene builder.
Some stupid error must in somewhere...
Could any one help?
I think you link to the wrong classpath.
Take a look at this thread
Hope that solves your problem.
Your classpath needs to be:
<?scenebuilder-classpath-element ../../target/classes?>
However, I'm facing the same problem and it doesn't work that way either. Please drop me a comment if you got it working

How to make JavaFX Applications non-blurry on Retina Display

A JavaFX application appears blurry on the MacBook Pro Retina screen. Is there a way to display the application sharp? In a blog post/comment was mentioned that this is the case currently: http://fxexperience.com/2012/11/retina-display-macbook-pro/
This is the example fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<VBox xmlns:fx="http://javafx.com/fxml">
<MenuBar>
<Menu text="File">
<items>
<MenuItem text="Exit"/>
</items>
</Menu>
</MenuBar>
</VBox>
Example code:
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Scene
import javafx.scene.layout.VBox
import javafx.scene.paint.Color
import javafx.stage.Stage
object CreateFX extends App {
override def main(args: Array[String]) = {
println("CreateFX starting up...")
Application.launch(classOf[CreateFX], args: _*)
}
}
class CreateFX extends Application {
def start(stage: Stage): Unit = {
println("start "+stage)
stage.setTitle("CreateFX")
val root: VBox = FXMLLoader.load(getClass().getResource("MainScreenVBox.fxml"))//new VBox
val scene = new Scene(root, 800, 600)
scene.setFill(Color.GREEN)
stage.setScene(scene)
stage.show()
}
}
Outcome:
Java Version: 1.7.0_21
In the same article in the commentary Richard Blair stated that this was fixed in the latest JavaFX version (available in the EAP of Java 8, downloadable here)

How to import a custom class into mxml file in Flex? (Actionscript 3)

i need to import my own class to the mxml file, but allways comes an error that classes cant be nested. I dont know how to use my classes (example: NetConn.as). Can you help me?
<!--language:actionscript3>
<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" firstView="views.eazyRadioHomeView">
<fx:Style source="eazyRadio.css"/>
<fx:Script>
<![CDATA[
include "NetConn.as";
import myNetConn;
var easy=new NetConnectionEx();
]]>
</fx:Script>
<fx:Declarations>
<!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
</fx:Declarations>
</s:ViewNavigatorApplication>
-->
You need
import com.yournamespace.NetConn;
instead of
include "NetConn.as"