JavaFX:如何设置默认选择/打开的TiteledPane

栏目: Java · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/33194517/javafx-how-to-set-a-titeledpane-selected-opened-by-default
我正在使用 javajava

FX开发一个项目,我需要知道是否有办法让Accordion中的第一个TiteledPane默认打开.

我的意思是在加载舞台时,会选择第一个TiteledPane并显示其内容而无需任何用户干预.我尝试了所有建议的方法,但没有人看起来像我想要的.正如我所看到的还没有在官方文档中描述或在本网站或其他地方给出的一样.

此外:

这是我的方法:(代码java和FXML)

@FXML
private Accordion accord_pane;
@FXML
private TitledPane pane_ara,pane_sci,pane_soc,pane_art,pane_spo,pane_fra,pane_ang;

...

pane_ara.setText("TitledPane A");
pane_sci.setText("TitledPane B");

accord_pane.setExpandedPane(pane_ara);
<Accordion fx:id="accord_pane" layoutX="20.0" layoutY="100.0" prefHeight="400.0" prefWidth="680.0">
                  <panes>
                    <TitledPane fx:id="pane_ara" animated="false">
                         <content>
                            <Pane prefHeight="200.0" prefWidth="200.0">
                               <children>
                                  <ImageView fx:id="img_unit_ara" fitHeight="250.0" fitWidth="650.0" layoutX="15.0" layoutY="15.0" pickOnBounds="true" preserveRatio="true">
                                     <image>
                                        <Image url="@temp_5.png" />
                                     </image>
                                  </ImageView>
                               </children>
                            </Pane>
                         </content>
                    </TitledPane>
                      <TitledPane fx:id="pane_sci" animated="false">
                         <content>
                            <Pane prefHeight="200.0" prefWidth="200.0">
                               <children>
                                  <ImageView fx:id="img_unit_sci" fitHeight="190.0" fitWidth="650.0" layoutX="15.0" layoutY="15.0" pickOnBounds="true" preserveRatio="true">
                                     <image>
                                        <Image url="@temp_3.png" />
                                     </image>
                                  </ImageView>
                               </children>
                            </Pane>
                         </content>
                      </TitledPane>
                     </panes>
                   </Accordion>

这是例外:

javafx.fxml.LoadException: 
/C:/Users/Malek%20Boubakri/Google%20Drive/workspace/Sirat/bin/vue/Enseignant.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at vue.Enseignant.start(Enseignant.java:18)
at vue.Index.stageCtrlHandler(Index.java:85)
at controleur.MenuCtrl.click_btn(MenuCtrl.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
at javafx.scene.Scene$ClickGenerator.access$8100(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$165/775736639.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at controleur.EnsCtrl.initialize(EnsCtrl.java:301)
    ... 54 more

这是我依赖的文档中的文本.

public final void setExpandedPane(TitledPane value)

The expanded TitledPane that is currently visible. While it is

technically possible to set the expanded pane to a value that is not

in getPanes(), doing so will be treated by the skin as if expandedPane

is null. If a pane is set as the expanded pane, and is subsequently

removed from getPanes(), then expanded pane will be set to null, if

possible. (This will not be possible if you have manually bound the

expanded pane to some value, for example).

如果有任何不清楚的地方请评论.任何建议都会有所帮助.谢谢

关于代码困难,强烈建议每次处理时刷新项目,然后从javaFX场景构建器保存FXML文件,因为eclipse不会自动执行此操作.

如果您仍然遇到一些代码问题,则会验证此示例:

@FXML
private Accordion accord;
@FXML
private TitledPane pane1,pane2;

//codes in controller.intialize()
...
    pane1.setText("TitledPane A");
    pane2.setText("TitledPane B");
    accord.setExpandedPane(pane1);

关于你在评论中提到的云问题,因为我知道将工作区放在谷歌驱动器文件夹中是没有害处的.如果你每次完成代码的一部分都做备份会更好,因为有时驱动器崩溃当他尝试加载另一个程序已经使用过的文件时.

翻译自:https://stackoverflow.com/questions/33194517/javafx-how-to-set-a-titeledpane-selected-opened-by-default


以上所述就是小编给大家介绍的《JavaFX:如何设置默认选择/打开的TiteledPane》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

100个可操作的网络赚钱方法

100个可操作的网络赚钱方法

陶秋丰 / 云南科技 / 2009-12 / 29.80元

《100个可操作的网络赚钱方法》专为有志于网上创业的读者量身打造,作者是“实战型”的网赚高手,在17岁时就通过互联网创业“年人10万”,如今结合自身的亲身实战经验,与大家分享可以实实在在盈利的100个网络赚钱方法和技巧。内容包括:网站创建与推广、竞价广告、联盟赚钱、网站SEO优化、域名投资、广告投放盈利、威客、博客、淘客赚钱等多个方面。 本手册中作者结合自身的网络赚钱经历,通过具体的、可操作......一起来看看 《100个可操作的网络赚钱方法》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具