Here is my code:
public class TabApp implements Initializable {
#FXML
Button newButton;
#FXML
TabPane tabPane = new TabPane();
public Tab newTab;
private SingleSelectionModel<Tab> selectionModel;
public int zQueryTabCount = 2;
#FXML
public void handleNewButton(ActionEvent event) {
System.out.println("new button is pressed");
newTab = new Tab();
selectionModel.select(newTab);
newTab.setId("Query " + zQueryTabCount);
newTab.setText("Query " + zQueryTabCount);
tabPane.getTabs().add(tabPane.getTabs().size(), newTab);
tabPane.getTabs().get(0).setClosable(false);
newTab.setClosable(true);
if (zQueryTabCount < 2) {
tabPane.getTabs().get(0).setClosable(false);
}
zQueryTabCount++;
}
}
I am getting the NullPointerException at the line selectionModel.select(newTab);
How can I resolve it?
Your selectionModel variable is not initialized nor attached to TabPane.
Use next call instead:
[...]
tabPane.getTabs().add(tabPane.getTabs().size(), newTab);
// should be called after tab has been added to TabPane
tabPane.getSelectionModel().select(newTab);
Related
I am trying Show a Button on TextField look like Windows 8 Metro theme in javafx.
If TextField is empty button is invisible otherwise button show.
In this stage i'm little close to success. i use this code to make it.
#FXML
private TextField tfMyName;//fx:id="tfMyName"
#FXML
private Button btnClear;//fx:id="btnClear"
#Override
public void initialize(URL url, ResourceBundle rb) {
clearTextFieldByButton(tfMyName, btnClear);
}
public void clearTextFieldByButton(TextField value, Button btn){
btn.setVisible(false);
value.setOnKeyTyped(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event) {
if ((value.textProperty().get().length() < 0) || (value.textProperty().get().equals(""))) {
btn.setVisible(false);
} else if (value.textProperty().get().length() > -1 || (!value.textProperty().get().equals(""))) {
btn.setVisible(true);
}
}
});
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
tfMyName.clear();
btn.setVisible(false);
tfMyName.requestFocus();
}
});
Using this code by default button is invisible but the button is only visible when i type more then one Characters.
But i need if anything input into the TextField to Button show.
But when i remove the condition under KeyEvent replace by
value.setOnKeyTyped(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event) {
btn.setVisible(true);
}
});
Then btn show if any character input into the TextField
You may also prefer to use JavaFX binding mechanism:
#Override
public void start( final Stage primaryStage )
{
TextField textfield = new TextField();
Button button = new Button( "my button" );
button.visibleProperty().bind( textfield.textProperty().isEmpty().not() );
final Scene scene = new Scene( new HBox( button, textfield ), 800, 600 );
primaryStage.setScene( scene );
primaryStage.show();
}
The actual problem in your code:
You have attached a listener to field when "OnKeyTyped", at this stage the newly typed text is not appended to the textfield's text value so your if-else condition will not see it. Instead, the correct way should be attaching the listener on "OnKeyReleased".
Add a listener to the textProperty() of the TextField. Check if the value is empty, hide the button else show it. It will be called whenever a character is added or removed from the textfield.
Here is a MCVE, you can just add the listener to the initialize method of the controller.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class HideButtonOnTextEntered extends Application {
#Override
public void start(Stage stage) {
TextField textField = new TextField();
Button button = new Button("Button");
button.setVisible(false);
VBox root = new VBox(20, textField, button);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 200, 200);
stage.setScene(scene);
stage.show();
textField.textProperty().addListener((ov, oldValue, newValue) -> {
if (newValue.isEmpty()) {
button.setVisible(false);
} else {
button.setVisible(true);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
I'm writing a program on the desktop that takes an array of COM port and displays it on the chart dynamically. the program runs without failures for a while, but then fails with the following exception:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:521)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$399(QuantumToolkit.java:334)
at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$40/432581434.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$144(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
the application continues to run. continue to saved data to a text file, but the window is not active any button except the close button.
if anyone has any ideas please help.
here is some code
public class FXMLController implements Initializable {
public static XYChart.Series<Number, Number> hourDataSeries;
#FXML
public Label label;
#FXML
public Label statusbar;
#FXML
public LineChart lineChart;
#FXML
public NumberAxis xAxis;
#FXML
NumberAxis yAxis;
#FXML
MenuItem close;
#FXML
MenuItem Options;
#FXML
MenuItem about;
#FXML
Button connect;
#FXML
public Label hexString;
#FXML
private void close(ActionEvent event) throws SerialPortException {
//COMConnect.serialPort.closePort();
System.exit(0);
}
#FXML
private void connect(ActionEvent event) {
OpenScene openScene = new OpenScene("/SceneCOM.fxml", "Options");
}
#FXML
private void showStatistics(ActionEvent event) {
OpenScene openScene = new OpenScene("/SceneStatistics.fxml", "Statistics");
}
#FXML
private void aboutit(ActionEvent event) {
OpenScene openScene = new OpenScene("/SceneAbout.fxml", "About");
}
#FXML
private void getconnect(ActionEvent event) {
if (COMConnect.b == true) {
COMConnect.b = false;
try {
COMConnect.serialPort.closePort();
} catch (SerialPortException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
connect.setText("Connect");
} else {
COMConnect.b = true;
COMConnect.comConnect();
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
hourDataSeries = new XYChart.Series<Number, Number>();
label.textProperty().bind(COMConnect.EventListener.valueProperty);
COMConnect.connectProperty.setValue("Connect");
connect.textProperty().bindBidirectional(COMConnect.connectProperty);
statusbar.textProperty().bind(Typewriter.status);
hexString.textProperty().bind(TypewriterHexString.hexStr);
xAxis.setLabel("Time");
xAxis.setTickUnit(1);
xAxis.setTickLabelFormatter(new NSC());
yAxis.setLabel("density," + superscript(" kg/m3"));
xAxis.setForceZeroInRange(false);
lineChart.setLegendVisible(false);
lineChart.setCursor(Cursor.CROSSHAIR);
lineChart.getData().add(hourDataSeries);
}
}
and add data to LineChart from other class
javafx.application.Platform.runLater(new Runnable() {
#Override
public void run() {
valueProperty.setValue(formatt(second) + superscript(" kg/m3"));
if (y > 10) {
hourDataSeries.getData().remove(0);
}
data = new XYChart.Data<Number, Number>(y, second);
data.setNode(new HoveredThresholdNode(0, second, ""));
hourDataSeries.getData().add(data);
System.out.println(hourDataSeries.getData().toString());
y++;
}
});
}
Thus, the solution was very simple:
lineChart.setAnimated(false);
the application works correctly for about 25 hours.
Thanks everyone!
Please how to share object User between Controllers? I have a TabPanelController. In initialization send to other controllers Data with object User. Its OK, but I cant acces to this object before stage shown.
Its possible acces to Stage in initialize method in controller, when controller is loaded before Stage? Or how handle onWindowShow in loader (When windows show, object User was filled)?
#FXML
private ActionController panelActionController;
#FXML
private StoreController panelStoreController;
#FXML
private ProfilesController panelProfilesController;
#FXML
private UsersController panelUsersController;
#FXML
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Platform.runLater(new Runnable() {
#Override
public void run() {
menuUserInfo.setText(" " + loggedInUser.getName() + " " +
loggedInUser.getForname() + " (" + loggedInUser.getLogin() + ")");
menuUserInfo.setUserData(loggedInUser);
panelActionController.setLoggedUser(loggedInUser);
panelStoreController.setLoggedUser(loggedInUser);
panelProfilesController.setLoggedUser(loggedInUser);
panelUsersController.setLoggedUser(loggedInUser);
}
});
}
In panelActionController
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println(loggedInUser); // NULL..
}
When i press any button in stage, loggedUser is not null. But i need set buttons, textfields by user role (Admin, User) during initialization.
SOLVED:
1. Inject Controllers in Main TabPanel Controller
in fxml must have a root element (e.g. GridPane) with fx:id="panelAction".
Controller must name: fx:id of GridPane + word Controller : panelActionController
#FXML
private ActionController panelActionController;
#FXML
private StoreController panelStoreController;
#FXML
private ProfilesController panelProfilesController;
#FXML
private UsersController panelUsersController;
2. I write getters for all child controllers in main TabPanelController:
public ActionController getActionController() {
return panelActionController;
}
public StoreController getStoreController() {
return panelStoreController;
}
public ProfilesController getProfilesController() {
return panelProfilesController;
}
public UsersController getUsersController() {
return panelUsersController;
}
3. And in main TabPanelStage (here i have Object loggedInUser) get all controllers
TabPanelController tabPanelController = fxmlLoader.getController();
ActionController actionController = tabPanelController.getActionController();
StoreController storeController = tabPanelController.getStoreController();
ProfilesController profilesController = tabPanelController.getProfilesController();
UsersController usersController = tabPanelController.getUsersController();
4. And then:
actionController.setComponentsByRole(loggedInUser.getRole());
I am using a progressbar in a tableview, where I'm using bind on his property so that when the value is changed, progressbar is started:
private SimpleDoubleProperty progressBar;
public Tabela(Double progressBar) {
this.progressBar = new SimpleDoubleProperty(progressBar);
}
public Double getProgressBar() {
return progressBar.get();
}
public DoubleProperty getProgressBarProperty() {
return progressBar;
}
public void setProgressBar(Double progressBar) {
this.progressBar.set(progressBar);
}
public void setProgressBar(SimpleDoubleProperty progressBar) {
this.progressBar = progressBar;
}
And in my Hbox am using progressbar as follows:
final ProgressBar progress = new ProgressBar();
progress.setMinWidth(324.0);
progress.progressProperty().bind(tabela.getProgressBarProperty());
So I'm using a so that after a certain time the value is changed, ie I will control the progressbar. The value is changed, but in my tableview nothing happens, but when I change the column position to another, the progressbar is running.
The same happens with "label", i changed for "text" and it work, but if the progressbar I have to use.
have a way to force the 'tableview' refresh?
You need to follow the JavaFX naming standard to get TableViews and other widgets to properly refresh when an observable object changes. For instance, you should rename getProgressBarProperty() to progressBarProperty().
See: How to update TableView Row using javaFx
There would be something wrong in this source?
class table
...
public Tabela(String nome, Double progressBar, String etapa) {
this.nome = nome;
this.progressBar = new SimpleDoubleProperty(progressBar);
this.etapa = new SimpleStringProperty(etapa);
}
....
Add new line.
private void preencheListaNomeTabelas() {
getLista().add(new Tabela("Test", 0.0, "Test Text"));
Add hbox in table.
columTabela.setCellValueFactory(new PropertyValueFactory<Tabela, String>("nome"));
columSituacao.setCellFactory(new Callback<TableColumn<Tabela, Double>, TableCell<Tabela, Double>>() {
public TableCell<Tabela, Double> call(TableColumn<Tabela, Double> p) {
final HBox box = new HBox();
box.setPrefHeight(25.0);
final ProgressBar progressBar = new ProgressBar(-1);
final Text text = new Text();
**text.textProperty().bind(..); //I would use here the**
BorderPane border = new BorderPane();
border.setTop(text);
border.setBottom(progressBar);
BorderPane.setAlignment(text, Pos.CENTER);
box.getChildren().add(border);
final TableCell cell = new TableCell<Tabela, Double>() {
#Override
protected void updateItem(Double t, boolean bln) {
super.updateItem(t, bln);
if (bln) {
setText(null);
setGraphic(null);
} else {
progressBar.setProgress(t);
progressBar.prefWidthProperty().bind(this.widthProperty());
setGraphic(box);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
};
cell.setAlignment(Pos.CENTER);
return cell;
}
});
columSituacao.setCellValueFactory(new PropertyValueFactory<Tabela, Double>("progress"));
columSituacao.setText("Progresso");
tableView.getItems().addAll(lista);
tableView.getSelectionModel().selectFirst();
task
Task t = new Task() {
#Override
protected Object call() throws Exception {
Thread.sleep(10000);
getLista().get(0).setEtapa("Lendo PostgreSQL");
getLista().get(0).setProgressBar(-1.0);
return null;
}
};
new Thread(t).start();
I'm trying to implement Menu with select box which sets to display or not component. I have this checkbox:
final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
DataTabs.renderTab = toolbarSubMenuNavigation.isSelected();
// call here the getter setter and send boolean flag
System.out.println("subsystem1 #1 Enabled!");
}
});
And I have this tabpane which I want to render only if I have selected the checkbox:
public static boolean renderTab;
public DataTabs()
{
}
public boolean isRenderTab()
{
return renderTab;
}
public void setRenderTab(boolean renderTab)
{
this.renderTab = renderTab;
}
// below this code
tabPane.setVisible(renderTab);
When I run the code it's not working. I also tested this:
DataTabs tabs = new DataTabs(); // instantiate first
tabs.setRenderTab(toolbarSubMenuNavigation.isSelected());
public static boolean renderTab;
TabPane tabPane = new TabPane();
public DataTabs()
{
}
public boolean isRenderTab()
{
return renderTab;
}
public void setRenderTab(boolean renderTab)
{
tabPane.setVisible(renderTab);
}
But again there is no result when I run the code and I check or uncheck the checkbox.
This is the complete source code:
http://pastebin.com/tkj4Fby1
Maybe I need to add listener or something else which I'm missing?
EDIT
Test 3
I also tested this code:
final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
DataTabs.toolbarSubMenuNavigation = toolbarSubMenuNavigation;
// call here the getter setter and send boolean flag
System.out.println("subsystem1 #1 Enabled!");
}
});
// class with tabs
public static CheckMenuItem toolbarSubMenuNavigation;
public static CheckMenuItem getToolbarSubMenuNavigation()
{
return toolbarSubMenuNavigation;
}
public static void setToolbarSubMenuNavigation(CheckMenuItem toolbarSubMenuNavigation)
{
DataTabs.toolbarSubMenuNavigation = toolbarSubMenuNavigation;
}
// below
abPane.visibleProperty().bind(toolbarSubMenuNavigation.selectedProperty());
I get NPE when I run the code.
You can easely tell to your tab to be visible when you check the box in one line
yourTab.visibleProperty().bind(yourCheckBox.selectedProperty());
And just with this line your tabpane will be visible only when it's checked