我有两个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
按钮时,我希望主窗口中的标签signin
更新为来自登录窗口中txtDisplay名称的文本。有人帮助如何操作.soon plz
答案 0 :(得分:0)
有很多方法可以做到这一点,在你的情况下,Login会创建另一个阶段,所以一个简单的方法就是创建一个新的FXML加载器(变量名:myLoader),如果你想传递用户的用户名,构造函数参数可以使用myLoader.setControllerFactory并作为return:
return clazz == MyController.class ? new MyController(userName) : null;
MyController是您想要读取用户名
的Controller的名称如果你想使用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(我更喜欢到处都是..)
答案 1 :(得分:0)
我不确定我是否完全理解两个控制器之间的关系,哪些是与控制器对应的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();
}
}