I have a code. It has errors, I want to know how to fix the error. The error is corresponding to this line: public void start(Stage primaryStage)
The code is shown as follows:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class DescriptionPane extends BorderPane{
/** Label for displaying an image and a title */
private Label lblImageTitle = new Label();
/** Text area for displaying text */
private TextArea taDescription = new TextArea();
public DescriptionPane() {
// Center the icon and text and place the text under the icon
lblImageTitle.setContentDisplay(ContentDisplay.TOP);
lblImageTitle.setPrefSize(200, 100);
// Set the font in the label and the text field
lblImageTitle.setFont(new Font("SansSerif", 16));
taDescription.setFont(new Font("Serif", 14));
taDescription.setWrapText(true);
taDescription.setEditable(false);
// Create a scroll pane to hold the text area
ScrollPane scrollPane = new ScrollPane(taDescription);
// Place label and scroll pane in the border pane
setLeft(lblImageTitle);
setCenter(scrollPane);
setPadding(new Insets(5, 5, 5, 5));
}
/** Set the title */
public void setTitle(String title) {
lblImageTitle.setText(title);
}
/** Set the image view */
public void setImageView(ImageView icon) {
lblImageTitle.setGraphic(icon);
}
/** Set the text description */
public void setDescription(String text ) {
taDescription.setText(text);
}
#Override
public void start(Stage primaryStage) {
Scene scene = new Scene(taDescription, 400, 200);
primaryStage.setTitle("RadioButtonDemo");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
I also paste the errors here. The errors are shown as follows. Can anyone give me a help to fix the problem. Thank you so much!
Exception in thread "main" java.lang.RuntimeException: Error: class DescriptionPane is not a subclass of javafx.application.Application
at javafx.graphics/javafx.application.Application.launch(Unknown Source)
at DescriptionPane.main(DescriptionPane.java:69)
The code seems to be flawed. The implementation of the start-method and the calls of the setLeft-, setCenter- and setPadding-method exclude each other. The last three methods belong to the BorderPane-class and therfore require this class as base class. The BorderPane-class on the other hand has no start-method which could be overwritten.
Since this is a JavaFX application the class implementing the start-method must be derived from the Application-class as it is already stated in the comments.
Assuming this is a JavaFX-project, the easiest way to fix the problem is: Create a new public class, e.g. Main, which extends the Application class. Copy your start- and main-method inside this class. Inside the copied start-method replace also "Scene scene = new Scene(taDescription, 400, 200)" with "Scene scene = new Scene(new DescriptionPane(), 400, 200)". Remove the start- and main-method from your DescriptionPane-class.
Related
I have a flickering issue with RichTextFx StyleClassedTextArea and VirtualizedScrollPane. When you wrap text and keep typing, it flickers so much that you are unable to read while you type. This is a stark contrast to the regular TextArea scrolling. It's more pronounced than the gif above (gif fps doesn't give it justice).
I'm not sure if this is because of the GridPane...
Java8u212
<dependency>
<groupId>org.fxmisc.richtext</groupId>
<artifactId>richtextfx</artifactId>
<version>0.10.6</version>
</dependency>
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.StyleClassedTextArea;
public class NewFXMain extends Application
{
#Override
public void start(Stage primaryStage)
{
StyleClassedTextArea textArea = new StyleClassedTextArea();
textArea.setWrapText(true);
TextArea textAreaFx = new TextArea();
textAreaFx.setWrapText(true);
VirtualizedScrollPane scrollpane = new VirtualizedScrollPane(textArea);
scrollpane.setPrefWidth(300);
GridPane notePane = new GridPane();
notePane.setVgap(2);
notePane.setHgap(6);
notePane.add(textAreaFx, 0, 0);
notePane.add(scrollpane, 0, 1);
Scene scene = new Scene(notePane, 600, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Scroll flickering demo");
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Found out how to fix the text flickering, although the scrollbars still flicker which is way more acceptable:
textArea.requestFollowCaret();
In JavaFX, is there a way to "autofit" elements on a page so they take up the entire thing?
Currently, I'm trying to make the window have two buttons that together take up the entire canvas, but I am not sure how to do that, given that it is possible to stretch the window, etc. I've tried playing around with Button.setPrefSize, but the button size stays the same, it just shows you a window with two outsized buttons, the text of which is not visible.
What I currently have
What I want (but for any window size)
Here's one way (code here but also possible in Scene Builder and FXML):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class TestApplication extends Application {
#Override
public void start(Stage stage) throws Exception {
Button button1 = new Button("Button1");
HBox.setHgrow(button1, Priority.SOMETIMES);
button1.setMaxWidth(Double.MAX_VALUE);
button1.setMaxHeight(Double.MAX_VALUE);
Button button2 = new Button("Button2");
HBox.setHgrow(button2, Priority.SOMETIMES);
button2.setMaxWidth(Double.MAX_VALUE);
button2.setMaxHeight(Double.MAX_VALUE);
HBox hBox = new HBox(button1, button2);
AnchorPane.setLeftAnchor(hBox, 0.0);
AnchorPane.setRightAnchor(hBox, 0.0);
AnchorPane.setTopAnchor(hBox, 0.0);
AnchorPane.setBottomAnchor(hBox, 0.0);
AnchorPane rootContainer = new AnchorPane(hBox);
Scene scene = new Scene(rootContainer, 600, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
I am trying to make a program with a tabbed GUI where each tab's contents are their own class. I essentially want to do what is done here, but with tabs. Mere is the Manager class:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
public class Manager extends Application {
private TabPane tabPane;
private Tab dcTab;
public Manager() {
dcTab = new Tab();
tabPane = new TabPane();
DistributionCenter dcMenu = new DistributionCenter();
dcTab.setText("Distribution Center");
dcTab.setContent(dcMenu);
tabPane.getTabs().add(dcTab);
}
#Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Scene scene = new Scene(tabPane, 600, 500);
DistributionCenter dcMenu = new DistributionCenter();
root.getChildren().add(dcMenu);
dcTab.setContent(dcMenu);
primaryStage.setScene(scene);
primaryStage.setTitle("909th Wing");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And here is my DistributionCenter class:
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
public class DistributionCenter extends Pane {
private Button testBt1;
public DistributionCenter() {
Pane dcMenu = new Pane();
testBt1 = new Button("Button");
dcMenu.getChildren().addAll(testBt1);
testBt1.setLayoutX(30);
testBt1.setLayoutY(100);
}
}
Note that the contents of DistributionCenter are just a test at this point.
You are never adding your Button to the actual DistributionCenter pane. Instead, you are creating a new Pane within that class and adding the button to it.
Remember, DistributionCenter is a Pane so it has its own children. You do not need to create a new Pane within it.
Update your DistributionCenter class to just configure itself:
public DistributionCenter() {
testBt1 = new Button("Button");
getChildren().addAll(testBt1);
}
That will solve your problem, but you should never extend a class unless you need to implement different behavior. If you are just configuring a Node like this, you should just create an instance of a Pane and configure it properly.
I am trying to figure out how to center and bind a label perfectly at the bottom of a scene. I have a simple test application here to show what I am working with and what my issue is.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class LabelTest extends Application {
#Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 400, 400);
Label label = new Label("Testing testing 1 2 3");
label.layoutXProperty().bind(scene.widthProperty().divide(2).subtract(label.getWidth() / 2)); //Should align label to horizontal center, but it is off
label.layoutYProperty().bind(scene.heightProperty().subtract(label.getHeight() + 35)); //Aligns the label to bottom of scene
root.getChildren().add(label);
stage.setScene(scene);
stage.show();
}
}
The logic behind my positioning makes sense to me, so I am not sure why it is not horizontally centered. I have included a screenshot below to show what the output looks like:
And below is more of what I am wanting it to look like (still off by a bit, but you get the point)
Thanks in advance to anyone who helps me out!
Let layout managers do the layout for you:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LabelTest extends Application {
#Override
public void start(Stage stage) throws Exception {
Label label = new Label("Testing testing 1 2 3");
BorderPane root = new BorderPane();
//center label by
//BorderPane.setAlignment(label, Pos.CENTER);
//root.setBottom(label);
//OR
root.setBottom(new StackPane(label));
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The issue is you are taking the values of width/height at the time of binding. And at this instance it will be 0 as they are not yet rendered. You need bind those properties as well for computing.
label.layoutXProperty().bind(scene.widthProperty().divide(2).subtract(label.widthProperty().divide(2)));
label.layoutYProperty().bind(scene.heightProperty().subtract(label.heightProperty().add(35)));
I'm currently working on a capstone project for a Java class and a problem I'm coming across frequently is displaying a variable's value in a JavaFX scene. I need a kickstart to get me moving, my google searches aren't bearing any fruit.
Thanks all :)
You can use a Label. Attach it to your scene and call Label.setText(String text) with the string representation of your variable value. Here's a complete example, using a Label:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class main3 extends Application {
static Integer variable = 250; // The value will be displayed in the window
#Override public void start(Stage primaryStage) {
Label variableLabel = new Label();
variableLabel.setFont(new Font(30));
variableLabel.setText("" + variable);
variableLabel.setLayoutX(175);
variableLabel.setLayoutY(125);
Group group = new Group(variableLabel);
Scene scene = new Scene(group, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch();
}
}
Result