I am using Java Fx and Scene builder, and my code runs without exceptions, but the combobox remains empty, and I can't understand why. I set identity ID and OnAction field but still it doesn't works. Here is my controller:
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.String;
import java.sql.PreparedStatement;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
public class AdaugareSediuController {
#FXML
ComboBox<String> meniu;
public void meniuCombo() throws SQLException{
try {
if(Conexiune.conexiuneBd !=null){
String query = "SELECT * FROM maginfo";
PreparedStatement dindb = Conexiune.conexiuneBd.prepareStatement(query);
ResultSet rezultate = dindb.executeQuery();
while (rezultate.next()) {
String Nume = rezultate.getString("nume");
ObservableList<String> list = FXCollections.observableArrayList(Nume);
meniu.getItems().addAll(list);
}
dindb.close();
rezultate.close();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Related
I am making javafx cinema seat reservation system as semester project.I want to add .mp3 music so that it plays in background while application is running and performing its fuctions.I added music but what happens is when I run code applications starts and I can perform my seat selections in GUI but there is no background music.but when I click cross(close button) GUI closes and music starts playing...I only included the music code portion.have a look
import java.io.File;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Cinemafx extends Application{
public void start(Stage primaryStage) {
try {
FileInputStream fileInputStream = new FileInputStream("song.mp3");
Player player = new Player(fileInputStream);
System.out.println("Song is playing...");
player.play();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I am trying to get the view count of a youtube video in Jsoup. I started by getting the title which worked well but am having trouble getting the view count which is in a span class.
Here is my code so far:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class HigherOrLowerYoutube {
public static void main(String[] args) {
try {
Document doc =
Jsoup.connect("https://www.youtube.com/watchv=omlFsLz2WKM").get();
println(doc.title());
} catch (IOException e) {
e.printStackTrace();
}
}
private static void println(String string) {
System.out.println(string);
}
}
Try:
System.out.println(doc.select(".watch-view-count").first().text());
I'm currently creating a program where the user gives a text as input, and then gets displayed the same text with the Code 128 font to see his input as a barcode.
I already installed and located Code 128 and Code 39 under /usr/share/fonts/truetype/
Both fonts should be installed correctly because both work in Libre Office Writer. However, only Code 39 is displayed correctly in my JavaFx Application.
That's my code:
package sample;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
public class CreateBarcodeStage extends Stage {
VBox vbox;
TextField textFieldInput;
TextFlow textFlowOutput;
public CreateBarcodeStage() {
createUI();
this.setTitle("Create Barcode");
this.setScene(new Scene(vbox));
}
private void createUI() {
vbox = new VBox(10);
textFieldInput = new TextField();
textFieldInput.textProperty().addListener((ObservableValue<? extends String> observableValue, String s, String t1) -> {
String inputText = textFieldInput.getText();
textFlowOutput.getChildren().clear();
Text textReadable = new Text();
textReadable.setFont(Font.font("Verdana", 20));
textReadable.setText(inputText + "\n\n");
Text textBarcode = new Text();
textBarcode.setFont(Font.font("Code 128", 40));
textBarcode.setText(inputText);
textFlowOutput.getChildren().addAll(textReadable, textBarcode);
});
textFlowOutput = new TextFlow();
textFlowOutput.setPadding(new Insets(15));
textFlowOutput.setPrefWidth(200);
textFlowOutput.setPrefHeight(150);
vbox.getChildren().addAll(textFieldInput, textFlowOutput);
}
}
If I replace "Code 128" with "Code 39" everthing works fine.
This is the output with Code 39:
Code 39 output
And here is the output with Code 128:
Code 128 output
I don't know why Code 128 isn't displayed correctly.
EDIT:
Ok, so now I tried to add an asterix before and after the Code 39 text and in this case everything works. I tested it with a barcode scanner app on my smartphone and the result was right.
For Code 128 I enhanced my code with a checksum maker which I found on github:
github Code 128 barcode generator
Now my code looks like this:
package sample;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
public class CreateBarcodeStage extends Stage {
VBox vbox;
TextField textFieldInput;
TextFlow textFlowOutput;
public CreateBarcodeStage() {
createUI();
this.setTitle("Create Barcode");
this.setScene(new Scene(vbox));
}
private void createUI() {
vbox = new VBox(10);
textFieldInput = new TextField();
textFieldInput.textProperty().addListener((ObservableValue<? extends String> observableValue, String s, String t1) -> {
String inputText = textFieldInput.getText();
textFlowOutput.getChildren().clear();
Text textReadable = new Text();
textReadable.setFont(Font.font("Verdana", 20));
textReadable.setText(inputText + "\n\n");
Text textBarcode = new Text();
textBarcode.setFont(Font.font("Code 128", 40));
textBarcode.setText(inputText);
textFlowOutput.getChildren().addAll(textReadable, textBarcode);
});
textFlowOutput = new TextFlow();
textFlowOutput.setPadding(new Insets(15));
textFlowOutput.setPrefWidth(200);
textFlowOutput.setPrefHeight(150);
vbox.getChildren().addAll(textFieldInput, textFlowOutput);
}
private String createCode128(String text) {
return (char) 136 + text + checksum(text) + (char) 138;
}
char checksum(String text) {
int result = 104; // Code 128B start code
for (int i = 0; i < text.length(); i++) {
result += ((int) text.charAt(i) - 32) * (i + 1);
}
return (char) (result % 103 + 32); // Return the character value of the checksum.
}
}
However, it still doesn't work:
Code 128 with start/stop symbol and checksum
While the output may look fine using the Code 39 font, it isn't. It will not scan and is not recognizable as a Code 39 barcode. This is what the word "TEXT" looks like in Code 39:
And this is what "TEXT" looks like with Code 128:
It looks like, with Code 39, you are missing the start and stop characters because what you are displaying in the Code 39 example is actually a subset of the bars, like so:
I suspect that with Code 128, you might not be supplying the start, stop and checksum characters, and the render is failing because of it.
I've recently started playing around with JavaFX :) I'm working on a little pet project and one of the issues I'm currently having is animating the resize of a VBox when a child is added or removed to/from the VBox.
I've got "the kids" fading out and then being removed from the VBox already. Once that animation completes I need the VBox height to resize preferably as an animation and not like the instant change it currently does.
The other threads that I've found are similar but I think they aren't quite exactly what I'm looking for.
Animation upon layout changes
Adding Node's animated to a VBox
Main Class:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCombination;
import javafx.scene.paint.Color;
import application.notifier.*;
public class Main extends Application
{
#Override
public void start(Stage stage)
{
try
{
stage.setTitle("Test");
Group root = new Group();
Scene scene = new Scene(root, (Screen.getPrimary().getBounds().getWidth()-100), (Screen.getPrimary().getBounds().getHeight()-100), Color.WHITE);
Notifier notice = new Notifier();
Notifier.addNotice("Testing Add Notice");
Notifier.addNotice("Testing Add Notice again!");
root.getChildren().add(Notifier.container);
stage.setFullScreen(true);
stage.setScene(scene);
stage.setFullScreenExitHint("");
//stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.show();
Button test = new Button("Remove");
test.setLayoutX(500.0);
test.setLayoutY(500.0);
test.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
Notifier.removeNotice();
}
});
root.getChildren().add(test);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
Notifier Class:
import java.util.ArrayList;
import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.util.Duration;
public class Notifier
{
private static int maxVisibleNotices = 5;
private static int currentVisible = 0;
private static ArrayList<String> msgOverflow;
public static VBox container;
public Notifier()
{
BackgroundFill bkgrndFill = new BackgroundFill(Color.rgb(0, 0, 0, .65), new CornerRadii(10.0), new Insets(0));
Background bkgrnd = new Background(bkgrndFill);
Notifier.container = new VBox();
Notifier.container.backgroundProperty().set(bkgrnd);
Notifier.container.setAlignment(Pos.TOP_CENTER);
Notifier.container.setMinWidth(Screen.getPrimary().getBounds().getWidth() - 50);
Notifier.container.setMaxWidth(Screen.getPrimary().getBounds().getWidth() - 50);
Notifier.container.setLayoutX((Screen.getPrimary().getBounds().getWidth() - (Screen.getPrimary().getBounds().getWidth() - 50))/2);
Notifier.container.setLayoutY(5.0);
Notifier.container.setSpacing(5.0);
Notifier.container.setPadding(new Insets(5.0));
Notifier.msgOverflow = new ArrayList<String>();
}
public static void addNotice(String msg)
{
if(Notifier.currentVisible < Notifier.maxVisibleNotices)
{
Text txt = new Text(msg);
txt.setFill(Color.rgb(255,255,255));
Notifier.container.getChildren().add(txt);
Notifier.currentVisible++;
}
else
{
Notifier.msgOverflow.add(msg);
}
}
public static void removeNotice()
{
if(Notifier.currentVisible > 0)
{
FadeTransition ft = new FadeTransition(Duration.millis(1000), Notifier.container.getChildren().get(0));
ft.setFromValue(1.0);
ft.setToValue(0.0);
ft.setCycleCount(0);
ft.setAutoReverse(false);
ft.play();
ft.setOnFinished(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
Notifier.container.getChildren().remove(0);
Notifier.currentVisible--;
}
});
}
}
}
I hope this is clear enough.
And thanks in advance for help or suggestions.
Probably not very helpful any more. I am currently working on the same thing.
You just need:
1. Add the same animation(if they all supposed to be same) to each of the remaining children in the VBox.
2. Use ParallelAnimation to make them run at the same time.
3. Use again a SequentialAnimation for you "kids" fading out animation and the parallel animation. This ensures they will not happen concurrently since multiple threads could run simultaneously.
4. To reach still the same view as what the instant update does, use animation/timeline.setonFinished(EventHandler()) to updates the screen
I just started Java and getting some GUI with Fx but I get an null pointer exception when trying to replace my scenecontent.
In the main the Login.fxml is loaded without any problem but when I click to the button for switching to another fxml I get the error.
The main:
package application;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
private Stage stage;
#Override
public void start(Stage primaryStage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("Login.fxml"));
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void gotoMainView() {
try {
MainViewController profile = (MainViewController) replaceSceneContent("MainView.fxml");
profile.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
void Userlogin()
{
gotoMainView();
}
private void gotoLogin() {
try {
LoginController login = (LoginController) replaceSceneContent("Login.fxml");
login.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Initializable replaceSceneContent(String fxml) throws Exception {
FXMLLoader loader = new FXMLLoader();
InputStream in = Main.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Main.class.getResource(fxml));
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
Scene scene = new Scene(page, 800, 600);
stage.setScene(scene);
stage.sizeToScene();
return (Initializable) loader.getController();
}
}
The Logincontroller class:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
public class LoginController extends AnchorPane implements Initializable {
#FXML
static private Button btntext;
#FXML
static private TextField loginusr;
#FXML
static private PasswordField loginpwd;
private Main application;
public void setApp(Main application){
this.application = application;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
btntext.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
System.out.println(loginusr.getText());
System.out.println(loginpwd.getText());
application.Userlogin();
}
});
}
The error occurs at:
application.Userlogin();
Many thanks in advance
The initialize() method is called as soon as the FXML is loaded. Have a look here
Your initialize() is called before setApp(Main application). At this point of time, the application object has not been initialized and is null !
This means the initialize() method is called at
MainViewController profile = (MainViewController) replaceSceneContent("MainView.fxml");
whereas application object which is used inside the initialize() is instantiated after this line, when you are using
profile.setApp(this);
So when you are trying the code application.Userlogin(); inside initalize(), at the time of execution, application is null !