JavaFX:未在自定义组件上调用initialize()方法(JavaFX: initialize() method not being called on custom component)
我正在尝试使用FXML标记创建自己的自定义JavaFX组件以及扩展HBox的控制器。 无论出于何种原因,都没有调用initialize()方法(我看不到调试输出)。 这仅适用于我的自定义组件,我的所有其他控制器都按预期运行,并且始终在初始化。 我不知道出了什么问题 - 这是我的代码。
NotificationItem.fxml(标记)
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml" spacing="5"> <ImageView fx:id="image" preserveRatio="true" fitWidth="60" /> <VBox alignment="center"> <Text text="notification" /> <Label fx:id="title" /> <Label fx:id="content" /> <Label fx:id="timestamp" /> </VBox> </fx:root>
NotificationItem.java(控制器)
public class NotificationItem extends HBox { public static final String FXML_FILENAME = "NotificationItem.fxml"; @FXML private ResourceBundle resources; @FXML private ImageView image; @FXML private Label title; @FXML private Label content; @FXML private Label timestamp; private Notification notification; private AbstractModel associatedModel; public NotificationItem(Notification notification) { this.notification = notification; this.associatedModel = notification.getAssociatedModel(); FXMLHelper.loadFxml("/com/github/norbo11/topbuilders/fxml/" + FXML_FILENAME, this, this); } @FXML public void initialize() { System.out.println(notification.getType()); switch (notification.getType()) { case ASSIGNMENT_CLOSE_TO_END: break; case EMPLOYEE_ASSIGNMENT_COMPLETE: break; case NEW_ASSIGNMENT: break; case NEW_MESSAGE: Message message = (Message) associatedModel; title.setText(resources.getString("home.notifications.new_message")); content.setText(resources.getString("messages.sender") + ": " + message.getSender()); break; case NEW_QUOTE_REQUEST: break; } timestamp.setText(Util.formatDate(notification.getDate())); } }
用于加载FXML的过程(忽略返回值,在这种情况下不使用)
public static LoadedFXML loadFxml(String filename, Object root, Object controller) { Log.info("Loading FXML: " + filename); Parent loadedRoot = null; AbstractController loadedController = null; try { FXMLLoader loader = new FXMLLoader(Main.getApp().getClass().getResource(filename)); if (root != null) loader.setRoot(root); if (controller != null) loader.setController(controller); if (!filename.equals(LoginScene.getAbsoluteFxmlFilename())) { Employee user = Employee.getCurrentEmployee(); //If the user is logged in if (user != null) { Locale locale = Employee.getCurrentEmployee().getSettings().getLocale(); loader.setResources(ResourceBundle.getBundle("lang.lang", locale, ClassLoader.getSystemClassLoader())); } } if (root == null) loadedRoot = loader.load(); if (controller == null) loadedController = loader.getController(); } catch (IOException e) { e.printStackTrace(); } return new LoadedFXML(loadedRoot, loadedController); }
I am trying to create my own custom JavaFX component using FXML markup as well as a controller which extends HBox. For whatever reason, the initialize() method is simply not being called (I can see no debug output). This only occurs for my custom component, all my other controllers behave as expected and are always being initialized. I have no clue what is wrong - here is my code.
NotificationItem.fxml (markup)
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml" spacing="5"> <ImageView fx:id="image" preserveRatio="true" fitWidth="60" /> <VBox alignment="center"> <Text text="notification" /> <Label fx:id="title" /> <Label fx:id="content" /> <Label fx:id="timestamp" /> </VBox> </fx:root>
NotificationItem.java (controller)
public class NotificationItem extends HBox { public static final String FXML_FILENAME = "NotificationItem.fxml"; @FXML private ResourceBundle resources; @FXML private ImageView image; @FXML private Label title; @FXML private Label content; @FXML private Label timestamp; private Notification notification; private AbstractModel associatedModel; public NotificationItem(Notification notification) { this.notification = notification; this.associatedModel = notification.getAssociatedModel(); FXMLHelper.loadFxml("/com/github/norbo11/topbuilders/fxml/" + FXML_FILENAME, this, this); } @FXML public void initialize() { System.out.println(notification.getType()); switch (notification.getType()) { case ASSIGNMENT_CLOSE_TO_END: break; case EMPLOYEE_ASSIGNMENT_COMPLETE: break; case NEW_ASSIGNMENT: break; case NEW_MESSAGE: Message message = (Message) associatedModel; title.setText(resources.getString("home.notifications.new_message")); content.setText(resources.getString("messages.sender") + ": " + message.getSender()); break; case NEW_QUOTE_REQUEST: break; } timestamp.setText(Util.formatDate(notification.getDate())); } }
Procedure used to load my FXML (ignore the return value, that isn't used in this case)
public static LoadedFXML loadFxml(String filename, Object root, Object controller) { Log.info("Loading FXML: " + filename); Parent loadedRoot = null; AbstractController loadedController = null; try { FXMLLoader loader = new FXMLLoader(Main.getApp().getClass().getResource(filename)); if (root != null) loader.setRoot(root); if (controller != null) loader.setController(controller); if (!filename.equals(LoginScene.getAbsoluteFxmlFilename())) { Employee user = Employee.getCurrentEmployee(); //If the user is logged in if (user != null) { Locale locale = Employee.getCurrentEmployee().getSettings().getLocale(); loader.setResources(ResourceBundle.getBundle("lang.lang", locale, ClassLoader.getSystemClassLoader())); } } if (root == null) loadedRoot = loader.load(); if (controller == null) loadedController = loader.getController(); } catch (IOException e) { e.printStackTrace(); } return new LoadedFXML(loadedRoot, loadedController); }
原文:https://stackoverflow.com/questions/27806953
最满意答案
所以,我的问题是,为什么即使在删除对象后我仍然可以调用Go_XXX_Your_Self()和Identify_Your_Self()?
由于未定义的行为 。
这是它在C ++中的工作原理吗? (甚至在你删除它之后呢?)
由于未定义的行为 。 无法保证它在其他实现中的工作方式相同。 再次, 未定义的行为 。
还可以检查一下它是否存在? (我知道理论上是不可能的,但我很想知道那里有什么方法)
delete MC1; MC1 = nullptr;
通过在
delete
指针后将指针设置为nullptr
,运行时最有可能检测到您正在访问无效的,您没有权利的位置。 此外,通过努力为所有适用的指针执行此操作,您可以检查对象是否有效(如果非nullptr
则有效)。if(my_ptr) { // my_ptr is most possibly valid (though you can still go wrong) // use my_ptr }
类似地,当它们尚未初始化为某个有效地址时,您还应该将原始指针设置为
nullptr
。MyClass* some_ptr = nullptr; ...
但同样,如果您可以访问现代C ++ 11工具,最好不要使用原始指针,只需使用
std::unique_ptr
或std::shared_ptr
(取决于您所需的语义)。 在未来的C ++标准版本中,您可能还想使用建议的std::exempt_ptr
,它是一个非拥有的,仅观察指针包装器。So, my question is, why I'm still able to call Go_XXX_Your_Self() and Identify_Your_Self() even after the object was deleted?
Because of undefined behavior.
Is this how it works in C++? (is there even after you delete it?)
Because of undefined behavior. There is no guarantee that it will work the same on other implementations. Again, undefined behavior.
Also can you check to see if it's not there? (I know theoretically is not possible but I'm curious to see what methods are out there)
delete MC1; MC1 = nullptr;
By setting the pointer to
nullptr
afterdelete
ing it, the runtime is most likely to detect that you are accessing an invalid, you-have-no-right-to-use location. Also, by diligently doing this for all applicable pointers, you have the ability to check if the object is valid or not (valid if non-nullptr
).if(my_ptr) { // my_ptr is most possibly valid (though you can still go wrong) // use my_ptr }
Similarly, you should also set raw pointers to
nullptr
when they aren't yet initialized to some valid address.MyClass* some_ptr = nullptr; ...
But again, if you have access to modern C++11 facilities, it's much better not to use raw pointers at all, and just use
std::unique_ptr
orstd::shared_ptr
(depending on your required semantics). And on future C++ standard revisions, you may also want to use the proposedstd::exempt_ptr
which is a non-owning, observe-only pointer wrapper.
相关问答
更多-
成员函数或指向它们的指针不存储在对象中。 (通常通过存储在表中的指针来调用virtual函数,对象具有指向该表的指针)这将是巨大的内存浪费。 它们通常存储在代码存储器部分,并且是编译器已知的。 对象( *this )通常作为不可见参数传递,以便函数知道调用哪个对象时的操作。 所以,从外行的角度来说,你会有的 0x10001000 void A::foo .......... {code for A::foo} 和 push a; call A::foo (0x10001000) po ...
-
Javascript:如何从事件回调函数访问对象成员(Javascript: How to access object member from event callback function)[2022-09-13]
由于在这些处理程序中,上下文会发生变化,因此最容易保留对所需上下文的引用,我个人更喜欢self 。 这是一种替代格式: var comment = function(){ this.textarea = null; var self = this; $(document).ready( init ); function init() { $('.reply').click( reply_to ); self.textarea = $( ... -
为什么即使在删除对象后我也可以访问成员函数?(Why I can access member functions even after the object was deleted?)[2023-03-19]
所以,我的问题是,为什么即使在删除对象后我仍然可以调用Go_XXX_Your_Self()和Identify_Your_Self()? 由于未定义的行为 。 这是它在C ++中的工作原理吗? (甚至在你删除它之后呢?) 由于未定义的行为 。 无法保证它在其他实现中的工作方式相同。 再次, 未定义的行为 。 还可以检查一下它是否存在? (我知道理论上是不可能的,但我很想知道那里有什么方法) delete MC1; MC1 = nullptr; 通过在delete指针后将指针设置为nullptr ,运行时最有 ... -
遵循Douglas Crockfords简单的构造函数模式,我将创建一个使用对象文字而不是new的构造函数。 喜欢这个: var o = {f: function(fn) { fn.call(o); }}; function obj() { var me = {}; me.f = function () { o.f(function() { me.x = 2; }); }; return me; } var ...
-
从技术上讲,你在这里所做的事情确实需要未定义的行为。 但是,因为你的getNum方法不是virtual并且this没有任何用处,所以这次碰巧不会崩溃。 要了解原因,请想象编译器在内部重写了您的程序,如下所示: class A { }; int A_getNum(A* this) { return 1234; } int main() { A* pA = 0; std::cout << A_getNum(pA) << '\n'; } 您可以看到,即使没有实际的A对象,也永远不会取消引用空指针,因 ...
-
如果你的意思是C:g() - > C::g() , B:f() - > B::f() 除非这些成员函数是静态的,否则无法在没有对象的情 可能指向对方可能会解决您的问题? 我介绍指针是因为否则我需要写一些东西来帮助循环包含问题。 :) #include
class A::B { public: void f() { c_->g(); } std::unique_ptr c_; } class A::C { public: void g() ... -
不,没关系,如果你小心: https : //isocpp.org/wiki/faq/freestore-mgmt#delete-this No, it's okay, if you are careful: https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
-
如果要动态访问属性,请使用getattr : for member in dir(MyObject): if callable(getattr(MyObject, member)): print(member) If you want to access an attribute dynamically, use getattr: for member in dir(MyObject): if callable(getattr(MyObject, member)): ...
-
你应该在main.cpp的第51行的enter1处删除“()”字符... 否则c ++将其视为一个函数,它不会调用构造函数。 You should remove the "()" characters after enter1 at line 51 of main.cpp ... Otherwise c++ sees that as a function, it does not call the constructor.
-
朋友和静态成员函数有哪些成员访问权限?(What kind of member access do friend and static member functions have?)[2022-12-04]
请记住,虽然友元函数的原型出现在类定义中,但友元函数不是函数成员。 类的友元函数是在远离类的情况下定义的,但是友元函数可以访问非公共成员。 有些人认为“友谊”会破坏信息隐藏。 有时朋友函数用于制作测试人员类。 当对象类应该只共享变量的一个副本时,使用静态数据成员。 因此,当只有一个副本足以供所有类成员使用时,必须使用静态数据成员来保存存储。 对于静态成员函数,您可以查看以下内容: 何时使用静态成员函数? Remember that although prototypes for friend functio ...