我发现了相关问题,但没有使用FXML,MenuItem
没有一个我可以用来获取场景的超类。当我发现按钮实例存在同样的问题时,我使用以下方法。
(Scene) ((Node) event.getSource()).getScene();
我对如何解决这个问题的任何想法都将非常感激。 在此先感谢
这是窗口的视图
/**
* @author Jose Gonzalez
*
*/
public class Transaction extends TempletePattern{
private ImageView viewImage;
private Button button;
private TransactionController controller;
private TableView<Saving> table;
private TableColumn dateColum;
private TableColumn descriptionColum;
private TableColumn amountColum;
private TableColumn valanceColum;
/**
*
* @param controller
*/
public Transaction(TransactionController controller)
{
this.controller = controller;
}
/**
* main method all private methods in the class and set them up on the borderpane
* @return pane fully setup to be mount on the scene
*/
public BorderPane setScreen()
{
BorderPane trans = new BorderPane();
trans.setStyle("-fx-background: #FFFFFF;");
VBox topBox = new VBox ();
topBox.getChildren().addAll( setMenu(),setTop() );
trans.setTop(topBox );
trans.setBottom(processUpdate(process) );
trans.setCenter(setCenter() );
return trans;
}
/**
*
* @return vbox holding all note pertaining to the center of the borderpane
*/
private VBox setCenter()
{
VBox center = new VBox();
center.setPadding(new Insets(30, 20, 20, 20) );
table = new TableView<>();
table.setEditable(true);
dateColum = new TableColumn("Date");
dateColum.setCellValueFactory( new PropertyValueFactory<>("firstName"));
dateColum.prefWidthProperty().bind(table.widthProperty().divide(4));
dateColum.setResizable(false);
descriptionColum = new TableColumn("Description");
descriptionColum.prefWidthProperty().bind(table.widthProperty().divide(4));
descriptionColum.setResizable(false);
descriptionColum.setCellValueFactory(new PropertyValueFactory<>("lastName") );
amountColum = new TableColumn("Amount");
amountColum.prefWidthProperty().bind(table.widthProperty().divide(4));
amountColum.setResizable(false);
amountColum.setCellValueFactory( new PropertyValueFactory<>("transaction") );
valanceColum = new TableColumn("Availble Valance");
valanceColum.prefWidthProperty().bind(table.widthProperty().divide(4));
valanceColum.setResizable(false);
valanceColum.setCellValueFactory( new PropertyValueFactory<>("valance"));
table.getColumns().addAll(dateColum, descriptionColum, amountColum,valanceColum );
mockInfo();
center.getChildren().add(table);
return center;
}
/**
*
* @return the screen's menu fully set up
*/
private MenuBar setMenu()
{
MenuBar menubar = new MenuBar();
final Menu UserMenu = new Menu("User");
UserMenu.setId("user");
UserMenu.setOnAction(controller);
MenuItem itemLog = new MenuItem("log out");
itemLog.setId("logout");
itemLog.setOnAction(controller);
MenuItem itemAcount = new MenuItem("new acount");
itemAcount.setId("newAccount");
UserMenu.getItems().addAll(itemLog, itemAcount);
final Menu acctMenu = new Menu("Accounts");
MenuItem itemsavis = new MenuItem("Savings");
MenuItem itemCredit = new MenuItem("Credit");
MenuItem itemChecking = new MenuItem("Checking");
acctMenu.getItems().addAll(itemsavis, itemCredit, itemChecking);
final Menu aboutMenu = new Menu("about");
MenuItem itemHelp = new MenuItem("Help");
aboutMenu.getItems().addAll(itemHelp);
menubar.getMenus().addAll(UserMenu, acctMenu, aboutMenu);
return menubar;
}
/**
* @param receive constumer info from model through controller and set it on table.
*/
public void tableDataSetter(ObservableList<Saving> costumerInfo)
{
table.setItems(costumerInfo);
}
/**
* insert mock data to be displayed as a place holder
*/
private void mockInfo()
{
ObservableList<Saving> data =
FXCollections.observableArrayList(
new Saving("11/10/16", "Deposit", 123, "123" ),
new Saving("11/11/16", "Withdraw", 5, "123" ),
new Saving("11/12/16", "Deposit", 24, "123" ),
new Saving("11/13/16", "Withdraw", 62, "123" ),
new Saving("11/14/16", "Deposit", 134, "123" ),
new Saving("11/15/16", "Deposit", 134, "123" ),
new Saving("11/10/16", "Withdraw", 123, "123" ),
new Saving("11/11/16", "Deposit", 5, "123" ),
new Saving("11/12/16", "Withdraw", 24, "123" ),
new Saving("11/13/16", "Deposit", 62, "123" ),
new Saving("11/14/16", "Withdraw", 134, "123" ),
new Saving("11/15/16", "Deposit", 134, "123" )
);
table.setItems( data );
}
}
这是控制器。 ps我刚刚开始研究它。
public class TransactionController implements EventHandler{
//private LoginModel model ;
private Transaction view;
public TransactionController()
{
// model = new LoginModel();
view = new Transaction(this);
}
@Override
public void handle(Event event) {
if( ( event.getSource() instanceof MenuItem))
{
System.out.println( "afe " + (((Object)event.getTarget())) );
if( ( (MenuItem) (event.getSource()) ).getId().equals("logout") )
{
System.err.println("from inside logout");
/// this.goTologInt(event);
}else if( ( (MenuItem) (event.getSource()) ).getId().equals("newAccount") )
{
// this.goToNewAct(event);
}
}
}
private void goToNewAct(Event event)
{
Scene scene = (Scene) ( (Control) event.getSource()).getScene();
// Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
NewCustomerView newAcct = new NewCustomerView();
scene.setRoot( newAcct.newCustomerScreen());
}
private void goTologInt(Event event)
{
Scene scene = (Scene) ((Node)event.getSource()).getScene() ;
//Scene scene = (Scene) ((Control) event.getSource()).getScene();
// Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
LoginView newAcct = new LoginView( new LoginController());
scene.setRoot( newAcct.loginScreen());
}
}
答案 0 :(得分:0)
您可以通过菜单层次结构直到到达带有弹出窗口的菜单,然后获取此弹出窗口的所有者窗口,这是打开上下文菜单的窗口。
@Override
public void start(Stage primaryStage) {
MenuItem menuItem = new MenuItem("Something");
Menu menuInner = new Menu("menu", null, menuItem);
Menu menuOuter = new Menu("menu", null, menuInner);
MenuBar menuBar = new MenuBar(menuOuter);
StackPane root = new StackPane();
root.setAlignment(Pos.TOP_LEFT);
root.getChildren().add(menuBar);
Scene scene = new Scene(root);
menuItem.setOnAction(evt -> {
MenuItem m = (MenuItem) evt.getSource();
while (m.getParentPopup() == null) {
m = m.getParentMenu();
}
Scene s = m.getParentPopup().getOwnerWindow().getScene();
System.out.println(s == scene);
});
primaryStage.setScene(scene);
primaryStage.show();
}
一般情况下,我认为最好只是让EventHandler
MenuItem
的{{1}}提供有关场景的信息:
final Scene scene = ...
menuItem.setOnAction(evt -> {
// do something with scene
});
(有效)final
局部变量的替代方法是使用字段...