I have one question regarding Javafx controller.
Lets say, I have multiple fxml files that are bind together in a main app. Then I have separate controllers for every fxml files. Lets see the following structure
com.par.app
- MainApp.java -> This is the main Application
- FirstController.java
- SecondController.java
com.par.app.view
- First.fxml
- Second.fxml
com.par.app.model
- MyModel -> This has some getter and setter methods.
Now as per above structure, I have a checkbox in First.fxml and a label in Second.fxml.
My Question : How can i set the label text in Second.FXML by checking and unchecking the checkbox in First.FXML , I have tried like this:
// In FirstController.Java
1) Initialize the SecondController
2) Get checkbox from FXMl as , priate CheckBox box1;
3) On initialize(....) method, I have set the event handler, as box1.setOnAction(enableHandle)
4) Finally the event Handler as,
EventHandler<ActionEvent> enableHandle = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if (box1.isSelected()) {
secondController.setLabelText("PoP");
} else {
secondController.setText("Gone Wrong");
}
}
};
Similarly, On my second controller I have declared FXML control as,
#FXML
private Label lblTes;
// the method
public void setLabelText(String a)
{
this.lblTes.settest(a);
}
-> The above wont work as it returns Unknown Source.
The next way I tried is by using the MyModel , and using getter and setter methods, But unsuccessful.
I'm sorry my question is really long. I have tried but not succeeded.
What can I do to solve this?
Thanks in advance.
// my model looks like
public class MyModel {
private String btnname;
public String getBtnname() {
return btnname;
}
public void setBtnname(String btnname) {
this.btnname = btnname;
}
}
When you check the check box then in the controller of the FirstView (where you implement an event handler for the check box click) change the label text in your model.
Your model should be bound to your views therefore the label text in your SecondView should be updated.
If you did not bind the model to your views you may use an Observer pattern.
1.Change your model and extend java.util.Observable
public class MyModel extends Observable {
private String btnname;
public String getBtnname() {
return btnname;
}
public void setBtnname(String btnname) {
this.btnname = btnname;
pingObservers()
}
private void pingObservers() {
setChanged();
notifyObservers();
}
}
Register your SecondController as an Observer of the model. When you set the model to the controller add a line similar to this:
model.addObserver(this);
SecondController must implement java.util.Observer.update(...)
void update(Observable o, Object o1) {
// Set the label text with model value
}
In you event handler in the FirstController when you call the setBtnname() method on your model the update() method in the SecondController will be called. There up to you to add the code to change your label text. Since the label is in the view controlled by SecondController you just need to inject a reference of the label in the controller with #FXML annotation.
Related
I wish to find a method to get the Text node of a TextArea without using the CSS properties. The Text node of JavaFX exposes the method setUnderline(boolean)
through which I can set the underline property of a TextArea; the TextArea, instead, doesn't expose the same method. In addition, the TextArea.getText() method returns a String instead of a Text object.
So, I solved the problem as follow:
In the code,
// Fields..
private final PseudoClass pseudoClass = PseudoClass.getPseudoClass("underlined");
private final SimpleBooleanProperty underlinedProperty = new SimpleBooleanProperty(false);
private final TextArea textArea = new TextArea();
[...]
// In a method (ex. in the constructor)..
{
textArea.setId("textArea");
underlinedProperty.addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
Node node = textArea.getScene().lookup("#textArea .text");
node.pseudoClassStateChanged(pseudoClass, newValue);
}
});
}
[...]
// The class exposes the getter method for the underlinedProperty
public SimpleBooleanProperty getUnderlinedProperty() {
return underlinedProperty;
}
Now, I've create a CSS sheet with this code:
#textArea .text {
/* some styles */
-fx-underline: false;
}
#textArea .text:underlined {
-fx-underline: true;
}
Finally, the above class is called in some other classes:
{
'handleOfClassInPoint1'.getUnderlineProperty().set(true); // or false
}
The problem is the lookup() method: this method returns a not null value only if all the fx nodes were created, i.e. only after some graphical result.
I wish to find a procedure to set the underline property of a TextArea without using CSS (ex. a toggle button manages the underline property: the text of a TextArea is underlined if the toggle is selected).
Anyone can help me?
Thank you so much!
Based on your description in the question comments, I would recommend the following.
Create a custom TextArea that looks something like this:
public class CustomTextArea extends TextArea {
private static final PseudoClass UNDERLINED = PseudoClass.getPseudoClass("underlined");
private final BooleanProperty underlined = new SimpleBooleanProperty(this, "underlined") {
#Override
protected void invalidated() {
pseudoClassStateChanged(UNDERLINED, get()); // update PseudoClass state to match
// the current value of the property
}
};
// property access methods
public final void setUnderlined(boolean underlined) {
this.underlined.set(underlined);
}
public final boolean isUnderlined() {
return underlined.get();
}
public final BooleanProperty underlinedProperty() {
return underlined;
}
// constructor
public CustomTextArea(String text, boolean underlined) {
super(text);
setUnderlined(underlined);
getStyleClass().add("custom-text-area"); // to allow specific CSS styling
}
}
Then in your CSS you would do:
.custom-text-area .text {
-fx-underline: false;
}
.custom-text-area:underlined .text {
-fx-underline: true;
}
The CSS is set on any Text node that is a descendant of aCustomTextArea.
The first CSS rule (the one without ":underlined") may not even be necessary since the default of -fx-underline for a Text node is false.
Then to query whether or not the text is underlined is a simple matter of calling area.isUnderlined() where area is an instance of CustomTextArea.
To maintain the correct visual state you could bind the underlined property of the CustomTextArea bidirectionally to the selected property of a ToggleButton. When one changes the other will reflect that change.
If you want to style only a specific CustomTextArea then you can still give it an ID and reference it in the CSS with #ID.
I have two controller class Fxml1.java and Fxml2.java. In Fxml1.java I hava label Label l1, and in Fxml2.java I have text field t1. I have some text in text field t1 and I want to set this text to Label l1. i want to do some thing like below. Below is a just dummy code for understanding consider both class have separate fxml files. Here i receive NullPointerException.
//class Fxml1
public class Fxml1{
public label l1;
l1.setText("hello");
}
//class Fxml2
public class Fxml2{
public TextField t1;
public Button b1;
public Fxml1 ob;
public void onButtonSubmit(ActionEvent event){
ob = new Fxml1();
ob.l1.setText(t1.getText());
}
public void initialize(URL url, ResourceBundle rb){
t1.setText("This is textfield text");
}
}
In your Fxml2 controller you are creating a new, completely separate instance of Fxml1 that is not linked to your fxml graph file, so it cannot update your required label since it has no access to it or any knowledge that it exists.
You should handle this through the use of events. A basic tutorial is can be found on Vogella's site.
Inject the event broker into both controllers:
#Inject IEventBroker broker;
In your Fxml2 controller you should post this string to the event broker when the text is updated:
public void onButtonSubmit(ActionEvent event) {
broker.post("SomeSharedConstant", t1.getText());
}
In your Fxml1 controller, you should listen for this event and update your label when it is fired.
#Inject #Optional
public void getEvent(#UIEventTopic("SomeSharedConstant") String text) {
// text1 is a SWT Text field
l1.setText(text);
}
I am building a preference page in Eclipse by extending the FieldEditorPreferencePage class. this page contains 2 fields : 1 BooleanFieldEditor (checkbox) and 1 FileFieldEditor. I would like to disable/enable the file field following the checkbox value.
I went up to something like this (some obvious code is not displayed):
public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
public static final String PREF_KEY_1 = "checkBoxPref";
public static final String PREF_KEY_2 = "filePref";
private FileFieldEditor pathField;
private BooleanFieldEditor yesOrNoField;
private Composite pathFieldParent;
#Override
protected void createFieldEditors() {
this.yesOrNoField = new BooleanFieldEditor(PREF_KEY_1, "Check this box!", getFieldEditorParent());
this.pathFieldParent = getFieldEditorParent();
this.pathField = new FileFieldEditor(PREF_KEY_2, "Path:", this.pathFieldParent);
addField(this.yesOrNoField);
addField(this.pathField);
boolean isChecked = getPreferenceStore().getBoolean(PREF_KEY_1);
updatePathFieldEnablement(! isChecked);
}
/**
* Updates the fields according to entered values
*/
private void updatePathFieldEnablement(boolean enabled) {
this.pathField.setEnabled(enabled, this.pathFieldParent);
}
#SuppressWarnings("boxing")
#Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty().equals(FieldEditor.VALUE) && event.getSource() == this.yesOrNoField) {
updatePathFieldEnablement(! (boolean) event.getNewValue());
}
super.propertyChange(event);
}
}
My question is about this second parameter in FieldEditor#setEnabled. This parameter is the parent composite of the FieldEditor's controls ("Used to create the controls if required" says the javadoc) . At first, I set the value with the return of getFieldEditorParent but then I got an exception "Different parent". So I ended storing it (cf. this.pathFieldParent) and give it back to setEnabled and it works (or it seems to work).
But I am not sure I am doing well, especially because I had to create a member in my class that means nothing to it (and I would have to create many of them if I had many fields to enable/disable).
Do you think I am doing well or is there a better way to provide this parent ? And could you explain to me why *setEnabled" needs it ?
Thanks.
You are using the default FLAT layout for the preference page. When this layout is used each call to getFieldEditorParent generates a new Composite so you have to make just one call and remember the correct parent. Using the GRID layout getFieldEditorParent always returns the same parent. This is the actual code:
protected Composite getFieldEditorParent() {
if (style == FLAT) {
// Create a new parent for each field editor
Composite parent = new Composite(fieldEditorParent, SWT.NULL);
parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
return parent;
}
// Just return the parent
return fieldEditorParent;
}
setEnabled does sometimes create a new Label control so it needs to know the correct parent Composite.
I'm having a difficults to add rows to a table that located in different class.
Following are the classes structure:
The dashed arrow is the desired relation that I dont manage to have
in the AddPanel class I have a fileds and Addbutton.
when clicking the addButton I first creating an instance of Product (class located in Logic Package). Then I need to add row to the table (Using the TableModel.AddRow method).
Following are the GUI Looks (the focused tab is AddPannel):
I tried different approches but non of them were successed.
My last attempt was to create in the Table class the following method:
public void AddRow(Product p) {
tbmStock.addRow(new Object[] { p.getExternalId(), p.getName(),
p.getAmount(), p.getPriceForMe(), p.getPriceForCustomer() });
}
In addition, in the AddPanel class I tried to add the following method:
private void AddButtonAction() {
btnAddProduct.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
Product p = new Product(txtName.getText(), txtExternalID
.getText(), Integer.parseInt(txtAmount.getText()),
Double.parseDouble(txtPriceForMe.getText()),
Double.parseDouble(txtPriceForCustomer.getText()),
Integer.parseInt(txtFromYear.getText()), Integer
.parseInt(txtToYear.getText()), Integer
.parseInt(txtSupplier.getText()), Integer
.parseInt(txtCarType.getText()));
AddRow(p); //This call doesn't compiles
}
catch (Exception e1){
JOptionPane.showMessageDialog(null,"Error");
}
}
});
}
Any suggestions ? (actually I'm not sure even that my structure is good :S )
Provide a custom event and event listener. If you create a new product fire the event and notify all listeners. The MainPanel (assuming that's where you create the instance of AddPanel), should register a listener. In this listener you then can update the table as the MainPanel has access to the table.
This is also known as the Mediator pattern.
Some pointers:
Create an event class, e.g. ProductCreatedEvent extends EventObject. Pass the Product as an argument to the constructor and make it accessible with a getter.
Create an event listener class: interface ProductCreatedEventListener extends EventListener. Provide a method such as productCreated(ProductCreatedEvent productCreatedEvent).
In the AddPanel add something like:
private final List<ProductCreatedEventListener> productCreatedEventListeners = new ArrayList<>();
...
public void addProductCreatedEventListener(ProductCreatedEventListener productCreatedEventListener){
productCreatedEventListeners.add(productCreatedEventListener);
}
public void removeProductCreatedEventListener(ProductCreatedEventListener productCreatedEventListener){
productCreatedEventListeners.remove(productCreatedEventListener);
}
private void fireProductCreatedEvent(ProductCreatedEvent event){
for (ProductCreatedEventListener productCreatedEventListener : productCreatedEventListeners){
productCreatedEventListener.productCreated(event);
}
}
Replace:
AddRow(p); //This isn't working
with
fireProductCreatedEvent(new ProductCreatedEvent(AddPanel.this, p));
I am building a simple app and I am implementing it in a simple MVC pattern where the controller adds event handlers to the view. Here's a sample controller code attaching a handler to the UI.
Basically, the code adds an event handler when the UI's save button is clicked. The UI contains the name and id number entry. What I wanted to happen is to pass the name and id number into the actionPerformed function.
ui.onAddStudent(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
System.out.print("test");
}
});
And the receiving function in the UI (in another file) is the following.
public void onAddStudent(ActionListener handler){
//something missing here
addStudent.addActionListener(handler);
}
I am not really into Java because it's not my forte. I actually do JavaScript. Now, a similar handler In JavaScript, one can use the call() or apply() method to call the handler and pass in additional parameters. If the above code was in JS, it would be like
//in the controller
ui.onAddStudent(function(event,id,name){
//I can use id and name
});
//in the UI
ui.onAddStudent = function(handler){
//store into a cache
//add handler to the button
}
//when student is added (button clicked)
handler.call(this,event,id,name);
How do I do the same thing in Java?
You have two choices:
let it as it is, and have the controller get the ID and name from the GUI (and that is the easiest and simplest solution, IMHO)
use your own Event and Listener types, containing this information. For example:
public class StudentAddedEvent {
private long ID;
private String name;
...
}
public interface StudentAddedListener {
void studentAdded(StudentAddedEvent event);
}
The UI would register an ActionListener on the button, and this action listener would do:
#Override
public void actionPerformed(ActionEvent e) {
long id = getIdInGui();
String name = getNameInGui();
StudentAddedEvent event = new StudentAddedEvent(id, name);
for (StudentAddedListener listener : studentAddedListeners) {
listener.studentAdded(event);
}
}
You can define your own Actions too, and set those to the buttons (constructor argument or setAction) and other components.
Extend AbstractAction for that.