W klasie A mam TextArea textArea i Button przycisk, chcę zrobić obsługę przycisku w klasie B, na razie wiem jak sprawdzić czy przycisk został kliknięty, ale jak mogę pobrać tekst z textArea do Stringa? normalnie to "String tekst= textArea.getText();", ale jak dostać się z klasy B do textArea w klasie A?
Próbuję tak, ale wyskakuje mi "NullPointerException":
public class A extends Application{
B b = new B();
private final TextArea textArea = new TextArea();
private final Button przycisk = new Button("Przycisk");
@Override
public void start(Stage stage) {
textArea.setMaxHeight(100);
grid.add(textArea, 0, 1);
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(przycisk);
grid.add(hbBtn, 1, 8);
przycisk.setOnAction(b.getHandler());
}
public String getTextArea() {
String textAreaTekst= taTekstDoZaszyfrowania.getText();
return textAreaTekst;
}
}
public class B {
private A a;
private TextArea textArea;
private EventHandler<ActionEvent> EH;
public Kontroler() {
EH = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String value = ((Button) event.getSource()).getText();
if (value.equals("Przycisk")) {
System.out.println("Kliknales przycisk");
pobierzTextArea();
}
}
};
}
public EventHandler<ActionEvent> getHandler() {
return EH;
}
private String pobierzTextArea() {
String tekst= a.getTextArea();
return tekst;
}
}