Java expert, JavaFX noobie. Trying to setup a very simple search app. I don’t want to dive into fxxml or Scene Builder as the UI is fairly simple.
So everything works UI-wise. The issue is when I click the Search button, I have a basic EventHandler that I want to go and collect all the text values, string arrays off the screen…right now I’m just logging these values…thing is, everything is empty/null inside that btnSearch eventhandler. In the other eventhandlers, the values are there and are logged just fine.
Why is this 1 btnSearch eventhandler not able to grab certain textfield and ListView values? The other oddity, I actually output the Skip Items textfield value in the btnSearch eventhandler?? But the others come back as empty???
package org.jpo.ui;
import javafx.application.Application;
import javafx.stage.Stage;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
/**
* https://docs.oracle.com/javafx/2/ui_controls/list-view.htm
* @author n0002501
*
*/
public class UITextinator extends Application {
protected Logger log = null;
protected DefaultTheme uiTheme = new DefaultTheme();
@Override
public void start(Stage stage) throws Exception {
ConfigurationBuilder<BuiltConfiguration> log4jBuilder = this.log4JConfigure();
Configurator.initialize(log4jBuilder.build());
log = LogManager.getLogger();
log.info("Launching...");
// Labels
Text directoryLabel = uiTheme.makeLabel("Starting Directory");
Text searchLabel = uiTheme.makeLabel("Search for");
// Text fields
TextField tbxSearch = new TextField();
tbxSearch.setPrefWidth(600);
// Buttons
Button btnSearch = uiTheme.makeButton("Search", "lightgray", "black");
btnSearch.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
log.info("btnSearch.ActionvEvent [" + arg0.toString() + "]");
log.info("Search for...............................[]", tbxSearch.getText().trim());
if(tbxSearch.getText().trim().length() > 0) {
log.info("File Types...............................[]");
}
}
});
// Creating a Grid Pane
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.TOP_LEFT);
gridPane.setMinSize(500, 500);
gridPane.setPadding(new Insets(10, 10, 10, 10));
gridPane.setVgap(5);
gridPane.setHgap(5);
// Arranging all the nodes in the grid
// Row 0
gridPane.add(searchLabel, 0, 0);
gridPane.add(tbxSearch, 1, 0);
// Arranging all the nodes in the grid
// Row 1
gridPane.add(btnSearch, 0, 1);
// Setting the back ground color
gridPane.setStyle("-fx-background-color: BEIGE;");
// Creating a scene object
Scene scene = new Scene(gridPane);
stage.setTitle("Textinator");
stage.setScene(scene);
stage.show();
}
public ConfigurationBuilder<BuiltConfiguration> log4JConfigure() {
ConfigurationBuilder<BuiltConfiguration> theBuilder = ConfigurationBuilderFactory.newConfigurationBuilder();
LayoutComponentBuilder standard = theBuilder.newLayout("PatternLayout");
standard.addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable");
AppenderComponentBuilder console = theBuilder.newAppender("stdout", "Console");
console.add(standard);
theBuilder.add(console);
AppenderComponentBuilder fileLog = theBuilder.newAppender("log", "File");
fileLog.addAttribute("fileName", "target/textanator.log");
fileLog.add(standard);
theBuilder.add(fileLog);
RootLoggerComponentBuilder rootLogger = theBuilder.newRootLogger(Level.DEBUG);
rootLogger.add(theBuilder.newAppenderRef("stdout"));
theBuilder.add(rootLogger);
return theBuilder;
}
public static void main(String args[]) {
launch(args);
}
}
package org.jpo.ui;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
public class DefaultTheme {
public Button makeButton(String theText, String theBackColor, String theTextColor) {
Button theButton = new Button(theText);
theButton.setStyle("-fx-background-color: " + theBackColor + "; -fx-textfill: " + theTextColor + ";");
return theButton;
}
public Text makeLabel(String theText, String theStyle) {
Text theItem = new Text(theText);
theItem.setStyle(theStyle);
return theItem;
}
public Text makeLabel(String theText) {
return this.makeLabel(theText, "-fx-font: normal bold 15px 'serif' ");
}
}
I suspect it’s something JavaFX related that I just am missing as a noob.
- Added minimal working code
Thx in advance
Can you make this into a minimal reproducible example? Get rid of all the references to external classes and objects (
VerticalButtons
,uiTheme
etc). Either use logging from the standard API or include your log4j configuration. Reduce the amount of code so that it’s minimal while being runnable and reproducing the error. Post the complete stack trace if there is one.I actually went to the trouble of filling in all the missing pieces, and it worked just fine. The search button logged all the data entered in the UI. So the cause of the issue is not included in the question.
@James_D, Interesting. I’m on a MacOS, Java 1.8_291, using the builtin JavaFX with the JRE/JDK. Even the minimal code example I just put in does not work…Any chance I simply need to move to JDK 11 with JavaFX not included???
I think this is nothing to do with JavaFX and is just a failure to understand Log4J messages (though I may be wrong; I don’t use that much). If I change the
[]
in the log message to{}
, it logs the text in the text field just fine.OMG!!!….What am I blind!!! Added {} and it works. Thank you @James_D….It was a log4j
Show 1 more comment