Java fx Changing the Color of a String
In my Program i have to change the color of a sample string by clicking on the radio Buttons. But i keep getting errors every time i click on them. i keep getting this error. I am also using e(fx)clipse to code this.
Caused by: java.lang.ClassCastException:
javafx.graphics@10.0.2/javafx.scene.paint.Color cannot be cast to
javafx.graphics@10.0.2/javafx.scene.text.Text at
employee.view.MainController.colorRadioButtonSelected(MainController.java:83)
package employee.view;
import javafx.beans.value.ChangeListener;
import javafx.scene.paint.Paint;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
public class MainController {
@FXML
private BorderPane myPane;
@FXML
private RadioButton blackRadioButton;
@FXML
private ToggleGroup colorToggleGroup;
@FXML
private RadioButton redRadioButton;
@FXML
private RadioButton blueRadioButton;
@FXML
private RadioButton greenRadioButton;
@FXML
private ListView<String> mylistView;
@FXML
private CheckBox boldCheckBox;
@FXML
private CheckBox italicCheckBox;
String Text;
Text sample=new Text(50,300,"SAMPLE");
FontWeight weight = FontWeight.NORMAL; // FontWeight.BOLD is boldface
FontPosture posture = FontPosture.REGULAR; // FontPosture.ITALIC is italic
int size=18;
boolean fontBold = false;
boolean fontItalic = false;
public void initialize() {
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
myPane.getChildren( ).add( sample );
sample.setFont(Font.font("Verdana", weight, posture, size));
}
@FXML
void boldCheckBoxSelected(ActionEvent event) {
}
@FXML
void colorRadioButtonSelected(ActionEvent event) {
sample= (javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
}
@FXML
void italicCheckBoxSelected(ActionEvent event) {
}
}
java javafx
add a comment |
In my Program i have to change the color of a sample string by clicking on the radio Buttons. But i keep getting errors every time i click on them. i keep getting this error. I am also using e(fx)clipse to code this.
Caused by: java.lang.ClassCastException:
javafx.graphics@10.0.2/javafx.scene.paint.Color cannot be cast to
javafx.graphics@10.0.2/javafx.scene.text.Text at
employee.view.MainController.colorRadioButtonSelected(MainController.java:83)
package employee.view;
import javafx.beans.value.ChangeListener;
import javafx.scene.paint.Paint;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
public class MainController {
@FXML
private BorderPane myPane;
@FXML
private RadioButton blackRadioButton;
@FXML
private ToggleGroup colorToggleGroup;
@FXML
private RadioButton redRadioButton;
@FXML
private RadioButton blueRadioButton;
@FXML
private RadioButton greenRadioButton;
@FXML
private ListView<String> mylistView;
@FXML
private CheckBox boldCheckBox;
@FXML
private CheckBox italicCheckBox;
String Text;
Text sample=new Text(50,300,"SAMPLE");
FontWeight weight = FontWeight.NORMAL; // FontWeight.BOLD is boldface
FontPosture posture = FontPosture.REGULAR; // FontPosture.ITALIC is italic
int size=18;
boolean fontBold = false;
boolean fontItalic = false;
public void initialize() {
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
myPane.getChildren( ).add( sample );
sample.setFont(Font.font("Verdana", weight, posture, size));
}
@FXML
void boldCheckBoxSelected(ActionEvent event) {
}
@FXML
void colorRadioButtonSelected(ActionEvent event) {
sample= (javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
}
@FXML
void italicCheckBoxSelected(ActionEvent event) {
}
}
java javafx
Could you update the title to be more relevant to your problem?
– Gnas
Nov 19 '18 at 23:43
add a comment |
In my Program i have to change the color of a sample string by clicking on the radio Buttons. But i keep getting errors every time i click on them. i keep getting this error. I am also using e(fx)clipse to code this.
Caused by: java.lang.ClassCastException:
javafx.graphics@10.0.2/javafx.scene.paint.Color cannot be cast to
javafx.graphics@10.0.2/javafx.scene.text.Text at
employee.view.MainController.colorRadioButtonSelected(MainController.java:83)
package employee.view;
import javafx.beans.value.ChangeListener;
import javafx.scene.paint.Paint;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
public class MainController {
@FXML
private BorderPane myPane;
@FXML
private RadioButton blackRadioButton;
@FXML
private ToggleGroup colorToggleGroup;
@FXML
private RadioButton redRadioButton;
@FXML
private RadioButton blueRadioButton;
@FXML
private RadioButton greenRadioButton;
@FXML
private ListView<String> mylistView;
@FXML
private CheckBox boldCheckBox;
@FXML
private CheckBox italicCheckBox;
String Text;
Text sample=new Text(50,300,"SAMPLE");
FontWeight weight = FontWeight.NORMAL; // FontWeight.BOLD is boldface
FontPosture posture = FontPosture.REGULAR; // FontPosture.ITALIC is italic
int size=18;
boolean fontBold = false;
boolean fontItalic = false;
public void initialize() {
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
myPane.getChildren( ).add( sample );
sample.setFont(Font.font("Verdana", weight, posture, size));
}
@FXML
void boldCheckBoxSelected(ActionEvent event) {
}
@FXML
void colorRadioButtonSelected(ActionEvent event) {
sample= (javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
}
@FXML
void italicCheckBoxSelected(ActionEvent event) {
}
}
java javafx
In my Program i have to change the color of a sample string by clicking on the radio Buttons. But i keep getting errors every time i click on them. i keep getting this error. I am also using e(fx)clipse to code this.
Caused by: java.lang.ClassCastException:
javafx.graphics@10.0.2/javafx.scene.paint.Color cannot be cast to
javafx.graphics@10.0.2/javafx.scene.text.Text at
employee.view.MainController.colorRadioButtonSelected(MainController.java:83)
package employee.view;
import javafx.beans.value.ChangeListener;
import javafx.scene.paint.Paint;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
public class MainController {
@FXML
private BorderPane myPane;
@FXML
private RadioButton blackRadioButton;
@FXML
private ToggleGroup colorToggleGroup;
@FXML
private RadioButton redRadioButton;
@FXML
private RadioButton blueRadioButton;
@FXML
private RadioButton greenRadioButton;
@FXML
private ListView<String> mylistView;
@FXML
private CheckBox boldCheckBox;
@FXML
private CheckBox italicCheckBox;
String Text;
Text sample=new Text(50,300,"SAMPLE");
FontWeight weight = FontWeight.NORMAL; // FontWeight.BOLD is boldface
FontPosture posture = FontPosture.REGULAR; // FontPosture.ITALIC is italic
int size=18;
boolean fontBold = false;
boolean fontItalic = false;
public void initialize() {
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
myPane.getChildren( ).add( sample );
sample.setFont(Font.font("Verdana", weight, posture, size));
}
@FXML
void boldCheckBoxSelected(ActionEvent event) {
}
@FXML
void colorRadioButtonSelected(ActionEvent event) {
sample= (javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
}
@FXML
void italicCheckBoxSelected(ActionEvent event) {
}
}
java javafx
java javafx
edited Nov 20 '18 at 1:49
AS Mackay
2,0074819
2,0074819
asked Nov 19 '18 at 23:25
Jordan MJordan M
65
65
Could you update the title to be more relevant to your problem?
– Gnas
Nov 19 '18 at 23:43
add a comment |
Could you update the title to be more relevant to your problem?
– Gnas
Nov 19 '18 at 23:43
Could you update the title to be more relevant to your problem?
– Gnas
Nov 19 '18 at 23:43
Could you update the title to be more relevant to your problem?
– Gnas
Nov 19 '18 at 23:43
add a comment |
1 Answer
1
active
oldest
votes
You set the data as Color
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
But you try to cast it as Text
(javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
To fix this you can use a switch statement or multiple if statements to set sample
object depending on which colour is selected. Example if statement:
if ((Color) colorToggleGroup.getSelectedToggle().getUserData() == Color.BLUE) {
sample = new Text(50, 300, "Something");
}
Alternatively you can set the data for the radio buttons as Text
, for example:
blueRadioButton.setUserData(new Text(50, 300, "Something"));
add a comment |
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%2f53384097%2fjava-fx-changing-the-color-of-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You set the data as Color
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
But you try to cast it as Text
(javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
To fix this you can use a switch statement or multiple if statements to set sample
object depending on which colour is selected. Example if statement:
if ((Color) colorToggleGroup.getSelectedToggle().getUserData() == Color.BLUE) {
sample = new Text(50, 300, "Something");
}
Alternatively you can set the data for the radio buttons as Text
, for example:
blueRadioButton.setUserData(new Text(50, 300, "Something"));
add a comment |
You set the data as Color
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
But you try to cast it as Text
(javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
To fix this you can use a switch statement or multiple if statements to set sample
object depending on which colour is selected. Example if statement:
if ((Color) colorToggleGroup.getSelectedToggle().getUserData() == Color.BLUE) {
sample = new Text(50, 300, "Something");
}
Alternatively you can set the data for the radio buttons as Text
, for example:
blueRadioButton.setUserData(new Text(50, 300, "Something"));
add a comment |
You set the data as Color
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
But you try to cast it as Text
(javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
To fix this you can use a switch statement or multiple if statements to set sample
object depending on which colour is selected. Example if statement:
if ((Color) colorToggleGroup.getSelectedToggle().getUserData() == Color.BLUE) {
sample = new Text(50, 300, "Something");
}
Alternatively you can set the data for the radio buttons as Text
, for example:
blueRadioButton.setUserData(new Text(50, 300, "Something"));
You set the data as Color
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
But you try to cast it as Text
(javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
To fix this you can use a switch statement or multiple if statements to set sample
object depending on which colour is selected. Example if statement:
if ((Color) colorToggleGroup.getSelectedToggle().getUserData() == Color.BLUE) {
sample = new Text(50, 300, "Something");
}
Alternatively you can set the data for the radio buttons as Text
, for example:
blueRadioButton.setUserData(new Text(50, 300, "Something"));
answered Nov 19 '18 at 23:32
GnasGnas
50829
50829
add a comment |
add a comment |
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%2f53384097%2fjava-fx-changing-the-color-of-a-string%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
Could you update the title to be more relevant to your problem?
– Gnas
Nov 19 '18 at 23:43