Unable to settext on javafx text at runtime - javafx

please i am trying to settext on a textfield after it as been open from login window but the changes it not displaying pls am new to java and javafx below is the code wich open the new window
try {
Statement stmnt= conn.createStatement();
rs=stmnt.executeQuery(sql);
if(rs.next()==true){
String name = null;
Image img = null;
name = rs.getString("name");
img = new Image(rs.getBinaryStream("photo"));
System.out.println(name);
Stage primaryStage = new Stage();
/*FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("main.fxml").openStream());
mainController mnctrl = (mainController) loader.getController();
mnctrl.dispOpInfo(name, img);*/
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = (Parent)fxmlLoader.load();
mainController controller = fxmlLoader.<mainController>getController();
controller.dispOpInfo(name, img);
primaryStage.show();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("BECE 2017 Validation");;
primaryStage.getIcons().add(new Image(this.getClass().getResourceAsStream("icon.png")));
primaryStage.show();
//
}
and below is the code for the newly open window of it textfield is not changing at runtime
#FXML TextField txtSchName = new TextField();
public void populateCanInfo (String canSchool, String canName, String canState,String canGender, Image canPhoto){
System.out.println(canGender );
txtCanName.setText(canName);
txtSchName.setText(canSchool);
imgViewCan.setImage(canPhoto);
// int index=cmbCanState.getValue().indexOf(canState);
// cmbCanState.getSelectionModel().select(index);
if(canGender.equalsIgnoreCase(canGender)){
rbtMale.setSelected(true);
}else{
rbtFemale.setSelected(true);
}
}

Related

How do I make my buttons all the same size?

Sorry, I have just begun learning javaFX and cannot figure out how to get all of my icons on buttons the same size. I tried a couple things but could not get it working, all help is appreciated. Thank you!
Wont let me post my question unless I add more details but I cant think of anything else to put so just ignore this whole paragraph as I ramble on so I can post my question and continue coding my game.
public class Main extends Application {
Stage window;
Button button;
Scene scene1, scene2;
public static final int ROCK = 0;
public static final int PAPER = 1;
public static final int SCISSORS = 2;
public static int userChoice;
public static void main(String [] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception
{
window = primaryStage;
//Layout 1
VBox layout = new VBox(20);
Label label = new Label("Rock Paper Scissors");
Button myButton = new Button("Start");
myButton.setOnAction(e -> window.setScene(scene2));
Button exit = new Button("Exit");
exit.setOnAction(e -> System.exit(0));
layout.getChildren().addAll(label, myButton, exit);
layout.setAlignment(Pos.CENTER);
scene1 = new Scene(layout, 300, 300);
//Layout 2
BorderPane border = new BorderPane();
VBox layout1 = new VBox(10);
Label label1 = new Label("Choose One");
layout1.setAlignment(Pos.CENTER);
//Layout 3
HBox layout2 = new HBox(10);
//Rock Image Button
Image rockIm = new Image(getClass().getResourceAsStream("Rock2.png"));
Button rock = new Button();
rock.setGraphic(new ImageView(rockIm));
rock.setOnAction(e -> userChoice = ROCK);
//Paper Image Button
Image paperIm = new
Image(getClass().getResourceAsStream("Paper2.png"));
Button paper = new Button();
paper.setGraphic(new ImageView(paperIm));
paper.setOnAction(e -> userChoice = PAPER);
//Scissor Image Button
Image scissorIm = new
Image(getClass().getResourceAsStream("Scissor2.png"));
Button scissors = new Button();
scissors.setGraphic(new ImageView(scissorIm));
scissors.setOnAction(e -> userChoice = SCISSORS);
Button quit = new Button("Return");
quit.setOnAction(e -> window.setScene(scene1));
layout2.getChildren().addAll(rock, paper, scissors, quit);
layout2.setAlignment(Pos.CENTER);
scene2 = new Scene(layout2, 300, 300);
window.setTitle("Rock Paper Scissors");
window.setScene(scene1);
window.show();
}
}

javafx stagestyle UNDECORATED

I'm using a translator.
enter image description here
Windows desktop appears unexpectedly when you click on the image.
Scrollpane -> BorderPane
Normal at first Occurs like an image later
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/Main_fx.fxml"));
Parent root = loader.load();
st = new Stage();
final UndecoratorScene undecorator = new UndecoratorScene(st, (Region) root);//NewMainScene lib 사용(프로젝트 및 패키지 명 :Newtable)
undecorator.getStylesheets().add(getClass().getResource("/View/winDec.css").toExternalForm());
st.setScene(undecorator);
stage = (Stage) lb.getScene().getWindow();
st.getIcons().add(new Image(config2.class.getResourceAsStream("/View/207411.jpg" )));
st.initStyle(StageStyle.TRANSPARENT); //스타일 미적용.
st.setResizable(resize);
st.setMaximized(maximized);
st.setTitle(judul);
st.sizeToScene();
// Undecorator undecor = undecorator.getUndecorator();
//최소로 줄이수 있는 화면 크기 값 .
st.setMinWidth(350);
st.setMinHeight(200);
GetStage gs = new GetStage();//트레이 창으로 보낼때 현재 Stage 값 전달.
gs.SetStage(st);
st.toFront();
st.show();
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
st.setX((primScreenBounds.getWidth() - st.getWidth()) / 2);
st.setY((primScreenBounds.getHeight() - st.getHeight()) / 2);
createTrayIcon(st);//트레이 창 이벤트 메소드
firstTime = true;
Platform.setImplicitExit(false);
stage.close();//controllSplash stage 종료
Hander_Main controller = (Hander_Main)loader.getController();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
Date sys_date = new Date();
String now= formatter.format(sys_date);
controller.setPrimaryStage(st);
controller.setScene(undecorator);
controller.setFirstDate(now);
st.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we)
{
hide(st);
}
});
} catch (Exception e) {
st.hide();
dialog(Alert.AlertType.ERROR,"Main_fx Stage ERR\n"+e);
System.exit(0);
}

How to get data from Tab in Tabpane JavaFX

I want get data from Tab in Tabpane JavaFX
I have 2 Tab in Tabpane, And each Tab I have a TextArea, I want click Button will get data from 2 tab
Here's my code:
btnThem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
try {
i++;
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("/fxml/tab.fxml"));
Parent parent = (Parent) fxmlLoader.load();
Tab tab = new Tab("Điểm " + i);
tab.setContent(parent);
tab.setClosable(true);
tabPane.getTabs().add(tab);
controllerTab = (ControllerTab) fxmlLoader.getController();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
});
Your question is ambiguous but it seems you want to get data from each textArea in tab.To do this you should get nodes (children) from each tab and by using lookup() we confirm and we parse node to textArea.I tried to figure your scene and made this example to help you :
public class example extends Application {
TextArea textArea = new TextArea();
TextArea textArea1 = new TextArea();
Button button = new Button("button");
#Override
public void start(Stage primaryStage) {
Tab tab1 = new Tab("tab1");
Tab tab2 = new Tab("tab2");
tab1.setContent(textArea);
tab2.setContent(textArea1);
TabPane pane = new TabPane();
pane.getTabs().addAll(tab1, tab2);
Node node1 = tab1.getContent();
Node node2 = tab2.getContent();
button.setOnAction((ActionEvent event) -> {
if (node1.lookup("TextArea") != null && node2.lookup("TextArea") != null) {
TextArea area1 = (TextArea) node1.lookup("TextArea");
TextArea area2 = (TextArea) node2.lookup("TextArea");
System.out.println(area1.getText() + " " + area2.getText());
}
});
VBox root = new VBox();
root.setAlignment(Pos.TOP_RIGHT);
root.getChildren().add(pane);
root.getChildren().add(button);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
And you can see the result :
Hello ,tab1 Hello ,tab2
Deleting directory C:\Users\Electron\Documents\NetBeansProjects\Buttono\dist\run341573612
jfxsa-run:
BUILD SUCCESSFUL (total time: 22 seconds)

TableView is not visible until click

I am using JavaFX and I have a strange problem. The follwing code is in the constructor of a class, which inherits Stage. The code is work in progress.
// this works fine, the chart is displayed
Scene scene = new Scene(lineChart, 1000, 600);
// table is not shown until I click on the window
Scene scene = new Scene(bidTable, 1000, 600);
this.setTitle("Auction chart");
this.setScene(scene);
What do I wrong? No errors and table with content is shown after I click on the window. Before it's just a white window.
EDIT:
I removed the chart just the table:
public class AuctionChart extends Stage {
private final TableView<Bid> bidTable = new TableView<Bid>();
private final TableColumn<Bid, String> bidColumn = new TableColumn<Bid, String>("Bid");
private final TableColumn<Bid, String> utilityColumn = new TableColumn<Bid, String>("Utility");
private final TableColumn<Bid, String> profitColumn = new TableColumn<Bid, String>("Profit");
private final NumberFormat fm = NumberFormat.getCurrencyInstance();
public AuctionChart() {
initialize();
bidTable.getColumns().addAll(bidColumn, utilityColumn, profitColumn);
Scene scene = new Scene(bidTable, 1000, 600);
this.setTitle("Auction chart");
this.setScene(scene);
}
public void setTowns(List<Town> towns) {
bidTable.setItems(FXCollections.observableArrayList(towns.get(0).getGebote()));
}
private void initialize() {
bidColumn.setCellValueFactory(cellData -> new SimpleStringProperty(fm.format(cellData.getValue().getBid())));
utilityColumn
.setCellValueFactory(cellData -> new SimpleStringProperty(fm.format(cellData.getValue().getUtility())));
profitColumn
.setCellValueFactory(cellData -> new SimpleStringProperty(fm.format(cellData.getValue().getProfit())));
}
}
Startup:
AuctionChart stage = new AuctionChart();
stage.setTowns(towns);
stage.show();
Thank you, it isn't a JavaFx Problem. It's an issue with concurrent threads in the application's backend.

Set image on left side of dialog

I created this very simple example for JavaFX alert dialog for JavaFX8u40.
public class MainApp extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
private Stage stage;
#Override
public void start(Stage primaryStage) throws Exception
{
Button create = new Button("Create Alert");
create.setTooltip(new Tooltip("Create an Alert Dialog"));
create.setOnAction(e ->
{
createAlert();
});
primaryStage.setScene(new Scene(create));
primaryStage.show();
stage = primaryStage;
}
protected Alert createAlert()
{
Alert alert = new Alert(AlertType.WARNING);
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
alert.setGraphic(imageView);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
}
I'm interested how I can set the image on the left side of the dialog.
Did someone manage to change the side of the image?
If you have a look at how the header is constructed, you'll find a GridPane node to layout a Label on the left and a StackPane for the icon.
If you want to reverse the cells order by code, you can do it, but it will be overriden every time updateHeaderArea() is called.
My suggestion is using this public API:
dialogPane.setHeader(Node header);
dialogPane.setGraphic(Node graphic);
providing a header with an icon on the left and a label, and a null graphic.
Using the same approach as DialogPane, we could add another GridPane as header:
protected Alert createAlert(){
Alert alert = new Alert(AlertType.WARNING);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
DialogPane dialogPane = alert.getDialogPane();
GridPane grid = new GridPane();
ColumnConstraints graphicColumn = new ColumnConstraints();
graphicColumn.setFillWidth(false);
graphicColumn.setHgrow(Priority.NEVER);
ColumnConstraints textColumn = new ColumnConstraints();
textColumn.setFillWidth(true);
textColumn.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().setAll(graphicColumn, textColumn);
grid.setPadding(new Insets(5));
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
imageView.setFitWidth(64);
imageView.setFitHeight(64);
StackPane stackPane = new StackPane(imageView);
stackPane.setAlignment(Pos.CENTER);
grid.add(stackPane, 0, 0);
Label headerLabel = new Label("Warning");
headerLabel.setWrapText(true);
headerLabel.setAlignment(Pos.CENTER_RIGHT);
headerLabel.setMaxWidth(Double.MAX_VALUE);
headerLabel.setMaxHeight(Double.MAX_VALUE);
grid.add(headerLabel, 1, 0);
dialogPane.setHeader(grid);
dialogPane.setGraphic(null);
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
And this is what you will see:

Resources