How can I scroll anchorPane in JAVAFX? - javafx

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.

Related

KeyValue class gives error in javafx? how i fix it

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;"

Snapshot Image can't be used as stage icon

I'm trying to set an image from a stage snapshot as the stage icon.
The following code demonstrates it:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
public class IconTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new StackPane(new ImageView(generateIcon(32))));
primaryStage.setScene(scene);
primaryStage.getIcons().setAll(generateIcon(32));
primaryStage.show();
}
private static Image generateIcon(int dimension) throws IOException {
Pane root = new Pane();
root.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
Scene scene = new Scene(root, dimension, dimension);
return scene.snapshot(new WritableImage(dimension, dimension));
}
}
Result:
As you can see, the stage icon is not a red square. Why is that? Loading from a file always works, generating the image using AWT works, too.
Workaround:
BufferedImage bimg = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(scene.snapshot(new WritableImage(dimension, dimension)), bimg);
SwingFXUtils.toFXImage(bimg, new WritableImage(dimension, dimension));

ControlsFx Decoration on a Label

I would like to apply ControlsFx decorations in a TableCell and as such would like to apply them to a Label.
This following does NOT apply the decoration to the Label. Should it?
import org.controlsfx.control.decoration.Decorator;
import org.controlsfx.control.decoration.GraphicDecoration;
import org.controlsfx.validation.decoration.GraphicValidationDecoration;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class LabelDecoration extends Application {
private static final Image REQUIRED_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/required-indicator.png").toExternalForm()); //$NON-NLS-1$
#Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Test");
Node requiredDecoration = new ImageView( REQUIRED_IMAGE );
Decorator.addDecoration( label, new GraphicDecoration( requiredDecoration, Pos.TOP_LEFT ));
primaryStage.setScene( new Scene( label, 100, 100 ));
primaryStage.show();
}
public static void main(String[] args) {
launch( args );
}
}
The Decorator tries to install a DecorationPane into the scene, which does not exist yet in your case.
Wrap the line Decorator.addDecoration(...) in a Platform.runLater(...) and it will work.

Create a webview from a button in JavaFX

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);
}
}

Putting an object on a stage when mouse clicked

package javafxapplication36;
/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Lighting;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
/**
* A sample that demonstrates how to add or remove a change listener on a node
* (for example, a Rectangle node) for some property (for example,
* Rectangle.hover). Once you add a listener, the text field shows the hover
* property change.
*
* #see javafx.beans.value.ChangeListener
* #see javafx.beans.InvalidationListener
* #see javafx.beans.value.ObservableValue
*/
public class ChangeListenerSample extends Application {
public static void main(String[] args) {
launch(args);
}
private void init(Stage primaryStage) {
final Group root = new Group();
primaryStage.setResizable(false);
Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
//rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
}
});
//root.getChildren().add(circle);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
I am trying to put an circle on stage when mouse is pressed, I am unable to make it happen. I tried setting the root at the beginning, but it doesn't happen.
Help appreciated !
I made just a few edits to your code and it seems to work perfectly !
Note : You need not set the root again inside the scene as you have already did it in the beginning !
Same goes for setting the scene to the Stage !
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
* A sample that demonstrates how to add or remove a change listener on a node
* (for example, a Rectangle node) for some property (for example,
* Rectangle.hover). Once you add a listener, the text field shows the hover
* property change.
*
* #see javafx.beans.value.ChangeListener
* #see javafx.beans.InvalidationListener
* #see javafx.beans.value.ObservableValue
*/
public class ChangeListenerSample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
final Group root = new Group();
primaryStage.setResizable(false);
Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
//rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
}
});
//root.getChildren().add(circle);
primaryStage.show();
}
}

Resources