Aritra Paul Aritra Paul - 3 years ago 209
Java Question

console output & gui textfield in javafx?

I am a new programmer in javafx. I am building a desktop application named as fuzzy calculator. whenever I run this program it opens a user interface but when click on the button(Max-Min) in the interface I found my output in the console instead of textfield. How can I get my desired output in the textfield I have created??? I have already wasted too many hours searching the solution but none of them is working or clear to me.

I am attaching my code below for your suggestion..

N.B: as my application is in developing mood all button is not working currently, only Max-min button is active.

Main.java

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Fuzzy Calculator");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}


public static void main(String[] args) {
launch(args);
}
}


Controller.java

package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import java.util.Arrays;
import java.util.Scanner;
public class Controller {
@FXML
private Button add;

@FXML
private Button maxmin;

@FXML
private Button maxavg;

@FXML
private Button sub;

@FXML
private TextField display;

@FXML
private Button maxprod;

@FXML
private Button minmax;

@FXML
void maxmin(ActionEvent event) {

System.out.println("Array size:");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
System.out.println("1st input ");
int a[][]=new int [n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=input.nextInt();
}
}
System.out.println("2nd input ");
int b[][]=new int [n][n];

for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=input.nextInt();
}
}
int [] c=new int [n];
int [] k=new int [n];
int t=0;
for(int i=0;i<n;i++)
{
t=0;
for(int j=0;j<n;j++ )
{

if(a[i][j]<b[j][i])
{
k[t]=a[i][j];
t++;
}
else
{
k[t]=b[j][i];
t++;
}
}
Arrays.sort(k);
c[i]=k[n-1];
}
System.out.println("the output is :");
for(int i=0;i<n;i++)
{
System.out.print(" "+c[i]);
}

}
}


sample.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<SplitPane dividerPositions="0.5" layoutX="102.0" layoutY="51.0" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
<items>
<AnchorPane maxHeight="100.0" prefHeight="80.0">
<children>
<TextField fx:id="display" layoutX="150.0" layoutY="42.0" prefHeight="100.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font size="15.0" />
</font>
</TextField>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="230.0" prefWidth="598.0">
<children>
<GridPane layoutY="6.0" prefHeight="267.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="maxmin" alignment="CENTER" contentDisplay="CENTER" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#maxmin" prefHeight="134.0" prefWidth="199.0" text="Max-Min">
<font>
<Font size="28.0" />
</font>
</Button>
<Button fx:id="maxprod" alignment="CENTER" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Max_Product" GridPane.columnIndex="1">
<font>
<Font size="28.0" />
</font>
</Button>
<Button fx:id="maxavg" alignment="CENTER" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Max_Average" GridPane.columnIndex="2">
<font>
<Font size="27.0" />
</font>
</Button>
<Button fx:id="add" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Addition" GridPane.rowIndex="1">
<font>
<Font size="28.0" />
</font>
</Button>
<Button fx:id="sub" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Subtraction" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="28.0" />
</font>
</Button>
<Button fx:id="minmax" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Min-Max" GridPane.columnIndex="2" GridPane.rowIndex="1">
<font>
<Font name="System Bold" size="28.0" />
</font>
</Button>
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>

Answer Source

All seems normal with your code, it does what you wrote

System.out.println("the output is :");

will print into the console, because this is the standard output

You said you want to print into the textfield so just write it :

display.setText(display.getText()+"new text, with its value : + 65);

setText() is to decide which text will appear into

getText() is to get back the text already into, it allows to do concatenation

Because if you use only setText(); you'll have something like this :

display.setText("log n°1 ...");

log n°1

display.setText("log n°2 ...");

log n°2

And you want to add the text and not replace it so :

display.setText("log n°1 ...\n");

log n°1

display.setText(display.getText() + "log n°2 ...");

log n°1

log n°2


But this is updating the UI from a different thread. The UI can only be updated from the UI thread. To achieve this, you've to do like this

Platform.runLater(() -> {
         display.setText(display.getText() + "log n°2 ...");
});

It tells the UI Thread to do the task ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download