KeyValue class gives error in javafx? I also implements all the abstract method but still face issue?
package at_collection;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
public class Scene1Controller implements Initializable {
#FXML
private StackPane parent;
#FXML
private AnchorPane child1;
#FXML
private Button btn1;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML
private void load1(ActionEvent event) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
Scene scene = btn1.getScene();
root.translateYProperty().set(scene.getHeight());
parent.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(root.translateYProperty(), 0, Interpolator.EASE_IN); // Here is the problem
KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
timeline.getKeyFrames().add(kf);
timeline.setOnFinished(t -> {
parent.getChildren().remove(child1);
});
timeline.play();
}
}
The stack trace is not print. Because of the compile-time error. i'm actually implements this "https://www.youtube.com/watch?v=cqskg3DYH8g"
Here is the output of this code
This is because of the Wrong Import "import javax.xml.crypto.dsig.keyinfo.KeyValue"
Import this one "import javafx.animation.KeyValue;"
Related
hi i write code like this in Below and how can i add Scroll in my project,**Note **The line shape added in the image is added by code and is not in scene builder space
`package schedul;
import edu.classes.drawInschedulClass;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
public class scene11 extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
AnchorPane root=FXMLLoader.
<AnchorPane>load(getClass().getResource("/schedul/scene1.fxml"));
Line line=new Line();
line.setStartX(50);
line.setStartY(50);
line.setEndX(800);
line.setEndY(50);
root.getChildren().add(text);
Scene scene=new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}`
How can i add scroll!??? Please Click to show pic
when i put ScrollPane in myCode,unfortunatly dose not have good performance and myscroll dosnt cover all my Line
this Code is Below
package schedul;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class scene11 extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
AnchorPane root=FXMLLoader.
<AnchorPane>load(getClass().getResource("/schedul/scene1.fxml"));
Line line=new Line();
Text text=new Text();
line.setStartX(50);
line.setStartY(50);
line.setEndX(800);
line.setEndY(50);
root.getChildren().add(line);
HBox content = new HBox(root);
ScrollPane scroller = new ScrollPane(content);
scroller.setFitToWidth(true);
Scene scene= new Scene(new BorderPane(scroller, null, null, null,
null));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I expected the scroll to be created automatically
I took your sample code and removed the irrelevant bits.
All you need to do to make an AnchorPane scrollable is put it in a ScrollPane. This is demonstrated by the sample below. The same will work for any other Pane type.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class ScrollApp extends Application {
#Override
public void start(Stage stage) {
stage.setScene(
new Scene(
new ScrollPane(
new AnchorPane(
new Line(50, 50, 800, 50)
)
)
)
);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
After the application launches, by default, it will size to fit all of the content, so you won't see the scroll bars. If you make the window smaller horizontally, you will see the horizontal scrollbar, and similarly for the vertical direction.
MY TASK : Write a JavaFX GUI application that allows the user to pick a set of pizza toppings using a set of check boxes. Assuming each topping cost 50 cents, and a plain pizza costs $10, display the cost of the pizza. Note that, once a topping is checked or unchecked, the cost of pizza should update automatically.
FIX REQUEST : my pizza cost calculator fails to add cost for topping properly
For example, Extracheese checked and unchecked resulted in below base price $10 (ie; $7)
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
class PizzaCost extends VBox {
private CheckBox ExtraCheeseCheckBox;
private CheckBox GreenPepperCheckBox;
private Label TotalPizzaCost;
public double pizzaCost = 10.0;
public PizzaCost(){
Font font = new Font(13);
ExtraCheeseCheckBox = new CheckBox("Extra Cheese");
ExtraCheeseCheckBox.setOnAction(this::processCheckBoxAction);
GreenPepperCheckBox = new CheckBox("GreenPepper");
GreenPepperCheckBox.setOnAction(this::processCheckBoxAction);
TotalPizzaCost = new Label(pizzaCost +"");
TotalPizzaCost.setFont(font);
HBox options = new HBox(ExtraCheeseCheckBox,GreenPepperCheckBox);
getChildren().addAll(options,TotalPizzaCost);
}
public void processCheckBoxAction (ActionEvent event){
if (ExtraCheeseCheckBox.isSelected()){
pizzaCost += 0.5;
}
else {
pizzaCost -= 0.5;
}
TotalPizzaCost.setText(pizzaCost+"");
}
}
The following code works for me. The pizza cost never goes below $10.
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class PizzaCost extends Application {
private static final double TOPPING_COST = 0.5d;
private static final NumberFormat COST_FORMAT = NumberFormat.getCurrencyInstance();
private static final String PIZZA_COST = "Pizza Cost: ";
private double pizzaCost = 10.0d;
private Label totalPizzaCostLabel;
#Override
public void start(Stage primaryStage) throws Exception {
CheckBox extraCheesCheckBox = getCheckBox("Extra Cheese");
CheckBox greenPepperCheckBox = getCheckBox("Green Pepper");
HBox hBox = new HBox(20.0d, extraCheesCheckBox, greenPepperCheckBox);
totalPizzaCostLabel = new Label(PIZZA_COST + COST_FORMAT.format(pizzaCost));
totalPizzaCostLabel.setFont(new Font(13.0d));
VBox root = new VBox(20.0d, hBox, totalPizzaCostLabel);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(40.0d));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Pizza Cost");
primaryStage.show();
}
private CheckBox getCheckBox(String text) {
CheckBox cb = new CheckBox(text);
cb.setOnAction(this::processCheckBoxAction);
return cb;
}
private void processCheckBoxAction(ActionEvent event) {
CheckBox cb = (CheckBox) event.getSource();
if (cb.isSelected()) {
pizzaCost += TOPPING_COST;
}
else {
pizzaCost -= TOPPING_COST;
}
totalPizzaCostLabel.setText(PIZZA_COST + COST_FORMAT.format(pizzaCost));
}
public static void main(String[] args) {
launch(args);
}
}
I'm currently following a tutorial on YouTube to make a photo slide show in JavaFX. Here is my controller code:
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML ImageView imageView;
int count;
public void slideshow()
{
ArrayList<Image> images= new ArrayList<Image>();
images.add(new Image("b1.jpg"));
images.add(new Image("b2.jpg"));
images.add(new Image("b3.jpg"));
Timeline timeline=new Timeline(new KeyFrame(Duration.ofSeconds(5), event->{
imageView.setImage(images.get(count));
count++;
if (count==3)
count=0;
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
#Override
public void initialize(URL location,ResourceBundle recourses)
{
slideshow();
}
}
I have an error on this line:
Timeline timeline=new Timeline(new KeyFrame(Duration.ofSeconds(5), event->{
I want the application to display the next image after every 5 seconds.
I don't understand why eclipse won't recognize the EventHandler since I have imported it. Here is my code:
package application;
import java.awt.event.ActionEvent;
import com.sun.glass.ui.Accessible.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.media.MediaPlayer;
public class MediaBar extends HBox {
Slider time =new Slider();
Slider vol =new Slider();
Button playButton=new Button("||");
Label volume=new Label("Volume");
MediaPlayer player;
public MediaBar(MediaPlayer play){
player=play;
setAlignment(Pos.CENTER);
setPadding(new Insets(5,10,5,10));
vol.setPrefWidth(70);
vol.setMin(30);
vol.setValue(100);
HBox.setHgrow(time, Priority.ALWAYS);
playButton.setPrefWidth(30);
getChildren().add(playButton);
getChildren().add(time);
getChildren().add(volume);
getChildren().add(vol);
playButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e){
Status status=player.getStatus();
if(status==Status.Playing){
if(player.getCurrentTime().greaterThanOrEqualTo(player.getTotalDuration())){
player.seek(player.getStartTime());
player.play();
}
else{
player.pause();
playerButton.setText(">");
}
}
if(status==Status.PAUSE ||status==Status.HALTED||status==Status.STOPPED){
player.play();
playButton.setText("||");
}
}
});
}
}
Here is the error message :
The type Accessible.EventHandler is not generic; it cannot be parameterized with arguments <ActionEvent>
What am I doing wrong?
Correct the import you are using JavaFX not Swing/AWT:
replace :
import java.awt.event.ActionEvent;
import com.sun.glass.ui.Accessible.EventHandler;
with :
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
Avoid automatic importing or pay attention to what you are importing !
I have the following simple code where I have an horizontal block where there are two buttons, the remain part is empty. Now, I would like to view a Webview inside the free part after the user press the "current" button. I already did something similar but I wasn't able to manage the layout because it was a Jframe + JavaFX. I thought to re-build totally in JavaFx , for this reason I want to put the webview in a jfxPanel(I think it's the best solution, any other suggest is really appreciate). I should see the webview when I press the button , in this case current, for this reason I create an handle. So, How can I do it?
Thanks in advance
import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Launcher extends Application {
public static void main(String[] args) {
launch(Launcher.class, args);
}
Button buttonCurrent;
static HBox web;
static MyBrowser browser_lau;
public void start(Stage stage) {
BorderPane border = new BorderPane();
HBox hbox = addHBox();
border.setTop(hbox);
Scene scene = new Scene(border);
stage.setScene(scene);
stage.setTitle("Layout Sample");
stage.show();
buttonCurrent.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
final JFXPanel jfxPanel = new JFXPanel();
//border.setStyle("-fx-background-color: #ff0000;");
}
});
}
public HBox addHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10); // Gap between nodes
hbox.setStyle("-fx-background-color: #336699;");
buttonCurrent = new Button("Current");
buttonCurrent.setPrefSize(100, 20);
Button buttonProjected = new Button("Projected");
buttonProjected.setPrefSize(100, 20);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);
return hbox;
}
}
}
Here is some sample code to get you started, it creates a couple of WebViews and toggles between them depending upon a radio button selection.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class CountThePeople extends Application {
private static final String CURRENT_URL =
"http://www.census.gov/popclock/";
private static final String PROJECTED_URL =
"http://www.sciencedaily.com/releases/2015/08/150810110634.htm";
#Override
public void start(final Stage stage) throws Exception {
WebView current = new WebView();
current.getEngine().load(CURRENT_URL);
WebView projected = new WebView();
projected.getEngine().load(PROJECTED_URL);
StackPane viewHolder = new StackPane();
ToggleGroup choice = new ToggleGroup();
RadioButton currentRadio = new RadioButton("Current");
currentRadio.setToggleGroup(choice);
RadioButton projectedRadio = new RadioButton("Projected");
projectedRadio.setToggleGroup(choice);
choice.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (projectedRadio == newValue) {
viewHolder.getChildren().setAll(projected);
} else {
viewHolder.getChildren().setAll(current);
}
});
choice.selectToggle(currentRadio);
VBox layout = new VBox(10, currentRadio, projectedRadio, viewHolder);
layout.setPadding(new Insets(10));
stage.setTitle("World Population");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}