Runtime Class Building, Class Loading & Function Use in JavaFXML?
My program dynamically builds .fxml views and controller classes (.java) for each view upon input from a text file. It also generates input objects (ComboBoxes, TextFields, & Buttons) for each view based on what it reads.
Since the classes are being built and compiled at runtime, I also need to make handler functions for each input object at runtime.
The problem occurs with the 'onAction' keyword in the .fxml views. When I use this keyword for linking the functions, the views that it affects no longer appear. Other views that do not need handler functions remain visible.
Am I not linking my controller to each view correctly? Is there another method for use of dynamically-built functions from dynamically-built classes?
Do I need to set 'fx:controller="view#Controller"' for each view within its .fxml file? When I tried setting this value, I was no longer able to load the classes at runtime using this method.
My goal is to be able to use the functions for each button in each controller class.
My method for dynamically loading each controller class:
(Excerpt Of) mainController.java
public Object getClass(Path javaClass, String className){
try{
URL classURL = javaClass.getParent().toFile().toURI().toURL();
URLClassLoader classLoader = URLClassLoader.newInstance(new URL{classURL});
Class<?> clazz = Class.forName(className, true, classLoader);
return clazz.newInstance();
}
catch(MalformedURLException e){
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(IllegalAccessException e ){
e.printStackTrace();
}
catch(InstantiationException e){
e.printStackTrace();
}
return null;
}
view_One.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="First View"/>
</top>
<center>
<GridPane>
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
<Label text="Obj1" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<TextField id="OneTextField1" GridPane.columnIndex="2" GridPane.rowIndex="0"/>
<Button fx:id="OneButton1" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="0" onAction="#OneButton1Handle"/>
<Label text="Obj2" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField id="OneTextField2" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Button fx:id="OneButton2" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="1" onAction="#OneButton2Handle"/>
<Label text="Obj3" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<ComboBox id="OneButtonTextField3" GridPane.columnIndex="2" GridPane.rowIndex="2"/>
<Button fx:id="OneButton3" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="2" onAction="#OneButton3Handle"/>
</GridPane>
</center>
</BorderPane>
view_OneController.java
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
public class view_OneController {
public void initialize() {
}
@FXML
public void OneButton1Handle(){
OneButton1.setText("Pressed");
}
@FXML
public void OneButton2Handle(){
OneButton2.setText("Pressed");
}
@FXML
public void OneButton3Handle(){
OneButton3.setText("Pressed");
}
}
java fxml
add a comment |
My program dynamically builds .fxml views and controller classes (.java) for each view upon input from a text file. It also generates input objects (ComboBoxes, TextFields, & Buttons) for each view based on what it reads.
Since the classes are being built and compiled at runtime, I also need to make handler functions for each input object at runtime.
The problem occurs with the 'onAction' keyword in the .fxml views. When I use this keyword for linking the functions, the views that it affects no longer appear. Other views that do not need handler functions remain visible.
Am I not linking my controller to each view correctly? Is there another method for use of dynamically-built functions from dynamically-built classes?
Do I need to set 'fx:controller="view#Controller"' for each view within its .fxml file? When I tried setting this value, I was no longer able to load the classes at runtime using this method.
My goal is to be able to use the functions for each button in each controller class.
My method for dynamically loading each controller class:
(Excerpt Of) mainController.java
public Object getClass(Path javaClass, String className){
try{
URL classURL = javaClass.getParent().toFile().toURI().toURL();
URLClassLoader classLoader = URLClassLoader.newInstance(new URL{classURL});
Class<?> clazz = Class.forName(className, true, classLoader);
return clazz.newInstance();
}
catch(MalformedURLException e){
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(IllegalAccessException e ){
e.printStackTrace();
}
catch(InstantiationException e){
e.printStackTrace();
}
return null;
}
view_One.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="First View"/>
</top>
<center>
<GridPane>
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
<Label text="Obj1" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<TextField id="OneTextField1" GridPane.columnIndex="2" GridPane.rowIndex="0"/>
<Button fx:id="OneButton1" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="0" onAction="#OneButton1Handle"/>
<Label text="Obj2" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField id="OneTextField2" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Button fx:id="OneButton2" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="1" onAction="#OneButton2Handle"/>
<Label text="Obj3" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<ComboBox id="OneButtonTextField3" GridPane.columnIndex="2" GridPane.rowIndex="2"/>
<Button fx:id="OneButton3" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="2" onAction="#OneButton3Handle"/>
</GridPane>
</center>
</BorderPane>
view_OneController.java
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
public class view_OneController {
public void initialize() {
}
@FXML
public void OneButton1Handle(){
OneButton1.setText("Pressed");
}
@FXML
public void OneButton2Handle(){
OneButton2.setText("Pressed");
}
@FXML
public void OneButton3Handle(){
OneButton3.setText("Pressed");
}
}
java fxml
add a comment |
My program dynamically builds .fxml views and controller classes (.java) for each view upon input from a text file. It also generates input objects (ComboBoxes, TextFields, & Buttons) for each view based on what it reads.
Since the classes are being built and compiled at runtime, I also need to make handler functions for each input object at runtime.
The problem occurs with the 'onAction' keyword in the .fxml views. When I use this keyword for linking the functions, the views that it affects no longer appear. Other views that do not need handler functions remain visible.
Am I not linking my controller to each view correctly? Is there another method for use of dynamically-built functions from dynamically-built classes?
Do I need to set 'fx:controller="view#Controller"' for each view within its .fxml file? When I tried setting this value, I was no longer able to load the classes at runtime using this method.
My goal is to be able to use the functions for each button in each controller class.
My method for dynamically loading each controller class:
(Excerpt Of) mainController.java
public Object getClass(Path javaClass, String className){
try{
URL classURL = javaClass.getParent().toFile().toURI().toURL();
URLClassLoader classLoader = URLClassLoader.newInstance(new URL{classURL});
Class<?> clazz = Class.forName(className, true, classLoader);
return clazz.newInstance();
}
catch(MalformedURLException e){
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(IllegalAccessException e ){
e.printStackTrace();
}
catch(InstantiationException e){
e.printStackTrace();
}
return null;
}
view_One.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="First View"/>
</top>
<center>
<GridPane>
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
<Label text="Obj1" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<TextField id="OneTextField1" GridPane.columnIndex="2" GridPane.rowIndex="0"/>
<Button fx:id="OneButton1" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="0" onAction="#OneButton1Handle"/>
<Label text="Obj2" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField id="OneTextField2" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Button fx:id="OneButton2" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="1" onAction="#OneButton2Handle"/>
<Label text="Obj3" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<ComboBox id="OneButtonTextField3" GridPane.columnIndex="2" GridPane.rowIndex="2"/>
<Button fx:id="OneButton3" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="2" onAction="#OneButton3Handle"/>
</GridPane>
</center>
</BorderPane>
view_OneController.java
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
public class view_OneController {
public void initialize() {
}
@FXML
public void OneButton1Handle(){
OneButton1.setText("Pressed");
}
@FXML
public void OneButton2Handle(){
OneButton2.setText("Pressed");
}
@FXML
public void OneButton3Handle(){
OneButton3.setText("Pressed");
}
}
java fxml
My program dynamically builds .fxml views and controller classes (.java) for each view upon input from a text file. It also generates input objects (ComboBoxes, TextFields, & Buttons) for each view based on what it reads.
Since the classes are being built and compiled at runtime, I also need to make handler functions for each input object at runtime.
The problem occurs with the 'onAction' keyword in the .fxml views. When I use this keyword for linking the functions, the views that it affects no longer appear. Other views that do not need handler functions remain visible.
Am I not linking my controller to each view correctly? Is there another method for use of dynamically-built functions from dynamically-built classes?
Do I need to set 'fx:controller="view#Controller"' for each view within its .fxml file? When I tried setting this value, I was no longer able to load the classes at runtime using this method.
My goal is to be able to use the functions for each button in each controller class.
My method for dynamically loading each controller class:
(Excerpt Of) mainController.java
public Object getClass(Path javaClass, String className){
try{
URL classURL = javaClass.getParent().toFile().toURI().toURL();
URLClassLoader classLoader = URLClassLoader.newInstance(new URL{classURL});
Class<?> clazz = Class.forName(className, true, classLoader);
return clazz.newInstance();
}
catch(MalformedURLException e){
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(IllegalAccessException e ){
e.printStackTrace();
}
catch(InstantiationException e){
e.printStackTrace();
}
return null;
}
view_One.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="First View"/>
</top>
<center>
<GridPane>
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
<Label text="Obj1" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<TextField id="OneTextField1" GridPane.columnIndex="2" GridPane.rowIndex="0"/>
<Button fx:id="OneButton1" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="0" onAction="#OneButton1Handle"/>
<Label text="Obj2" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField id="OneTextField2" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Button fx:id="OneButton2" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="1" onAction="#OneButton2Handle"/>
<Label text="Obj3" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<ComboBox id="OneButtonTextField3" GridPane.columnIndex="2" GridPane.rowIndex="2"/>
<Button fx:id="OneButton3" text="Change" GridPane.columnIndex="3" GridPane.rowIndex="2" onAction="#OneButton3Handle"/>
</GridPane>
</center>
</BorderPane>
view_OneController.java
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
public class view_OneController {
public void initialize() {
}
@FXML
public void OneButton1Handle(){
OneButton1.setText("Pressed");
}
@FXML
public void OneButton2Handle(){
OneButton2.setText("Pressed");
}
@FXML
public void OneButton3Handle(){
OneButton3.setText("Pressed");
}
}
java fxml
java fxml
asked Nov 19 '18 at 17:29
freudWRDfreudWRD
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379819%2fruntime-class-building-class-loading-function-use-in-javafxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379819%2fruntime-class-building-class-loading-function-use-in-javafxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown