我有两个window.Their窗口登录和主fxml各自的控制器如下所示:
public class MainwindowController extends Stage implements Initializable {
@FXML private Button Send;
@FXML private TextField txtBcast;
@FXML private ListView listviewUsers;
@FXML Label lblDisplayName;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> chat =FXCollections.observableArrayList ("default");
listviewUsers.setItems(chat);
}
public void setLblName(String msg){
lblDisplayName.setText(msg);
}
@FXML public void ActionSend(ActionEvent e){
send();
txtBcast.setText("");
}
private void send() {
if (txtBcast.getText().isEmpty())
return;
// chatManager.sendPublicMsg(format,txtBcast.getText());
}
/**
*
* @param e
* @throws Exception
*/
@FXML public void ActionUserSelected( MouseEvent e) throws Exception{
// String lineRest = e.getActionCommand();
if(e.getClickCount()==2)
{
if(!listviewUsers.getSelectionModel().isEmpty())
{
String str=(String)listviewUsers.getSelectionModel().getSelectedItem();
Parent main= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Usertab.fxml"));
Scene scene = new Scene(main);
Stage stage = new Stage();
stage.setTitle(str);
stage.setScene(scene);
stage.show();
}
else { JOptionPane.showMessageDialog(null, "Oops! it seems you are trying to click the list view"); }
}
//Stage pstage = (Stage)listUsers.getScene().getWindow();
//pstage.close();
}
}和
public class LoginwindowController extends Stage implements Initializable {
@FXML private LoginwindowController loginwindowController;
@FXML private MainwindowController mainwindowController;
@FXML private Button btnSignIn;
@FXML private TextField txtDisplayName;
@FXML private ToggleGroup Gender;
@FXML private ComboBox comboStatus;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> items =FXCollections.observableArrayList ("Online","Offline");
comboStatus.setItems(items);
writeToTextField();
}
public void writeToTextField() {
String username = System.getProperty("user.name");
txtDisplayName.setText(""+ username);
}
@FXML protected void ActionSignIn(ActionEvent event) throws Exception {
mainwindowController.setLblName(txtDisplayName.getText());
InetAddress addr = InetAddress.getLocalHost();
if(addr.isLoopbackAddress())
{
Dialogs.create().message("Oops! It seems you are not connected to any network..\n :(").showError();
}
else{
start(txtDisplayName.getText());// start chat manager
Parent root= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Mainwindow.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("LetsChat-Welcome "+ txtDisplayName.getText());
// Context.getInstance().setDisplayName(txtDisplayName.getText());
stage.setScene(scene);
stage.getIcons().add(new Image("/letschat/images/logo.png"));
Stage pstage = (Stage)btnSignIn.getScene().getWindow();
stage.show();
pstage.close();
}
}
private void start(String name) {
try {
ChatManager ic = new ChatManager(name);
ic.start();
} catch (Exception ex) {
Dialogs.create().message( "Could not start the chat session\nCheck that there no other instances running :(").showError();
}
}
}我希望在主窗口中的标签lblDisplayName更新为文本从登录窗口中的txtDisplay名称当用户单击signin button.can有人帮助如何做so..soon plz
发布于 2015-03-12 20:57:42
有多种方法可以做到这一点,在你的例子中,登录将创建另一个阶段,所以一个简单的方法是创建一个新的FXML加载器(变量名: myLoader),如果你想将用户的用户名作为构造函数参数传递,你可以使用myLoader.setControllerFactory和as return:
return clazz == MyController.class ? new MyController(userName) : null;MyController是要在其中读取用户名的控制器的名称
如果你想使用set方法,使用getController你可以获得控制器实例并调用set方法(例如,myController.setUsername());
创建自定义FXML的步骤
FXMLLoader myLoader = new FXMLLoader(<if you use relative paths, here you should pass the position);记住调用load(),因为URI重载是静态的。(即使用getResourceAsStream)。
如果您的应用程序又大又复杂,您可以使用EventBus (我在任何地方都喜欢使用它。)
发布于 2015-03-12 21:15:24
我不确定我是否完全理解这两个控制器之间的关系,以及哪些是与控制器相对应的FXML文件,但看起来LoginWindowController加载了MainWindow.fxml,我猜测MainWindowController是MainWindow.fxml的控制器。
在这种情况下,您可以直接执行以下操作
@FXML protected void ActionSignIn(ActionEvent event) throws Exception {
InetAddress addr = InetAddress.getLocalHost();
if(addr.isLoopbackAddress())
{
Dialogs.create().message("Oops! It seems you are not connected to any network..\n :(").showError();
}
else{
start(txtDisplayName.getText());// start chat manager
FXMLLoader loader = new FXMLLoader(getClass().getResource("/letschat/fxwindows/Mainwindow.fxml"));
Parent root= loader.load();
MainWindowController mainWindowController = loader.getController();
mainWindowController.setLblName(txtDisplayName.getText());
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("LetsChat-Welcome "+ txtDisplayName.getText());
// Context.getInstance().setDisplayName(txtDisplayName.getText());
stage.setScene(scene);
stage.getIcons().add(new Image("/letschat/images/logo.png"));
Stage pstage = (Stage)btnSignIn.getScene().getWindow();
stage.show();
pstage.close();
}
} https://stackoverflow.com/questions/29010067
复制相似问题