将Tab添加到TabPane JavaFX

时间:2016-09-20 20:04:22

标签: java javafx tabs fxml tabpanel

我正在尝试使用fxml添加新标签。

我已经在tabPane中添加了标签。

@Configuration
@EnableJpaRepositories(basePackageClasses = {
    TestTableRepository.class
})
public class InMemoryDataConfig {

@Bean
public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .addDefaultScripts()
            .build();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[]{"org.test.domain.entity"});

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(buildHibernateProperties());

    return em;
}

protected Properties buildHibernateProperties() {
    Properties hibernateProperties = new Properties();

    hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    hibernateProperties.setProperty("hibernate.show_sql", "true");
    hibernateProperties.setProperty("hibernate.use_sql_comments", "true");
    hibernateProperties.setProperty("hibernate.format_sql", "true");

    return hibernateProperties;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
}

@Bean
public TestService testService() {
    return new TestService();
}

但我无法用

加载FXML
int numTabs = tabPane.getTabs().size();
Tab tab = new Tab("Tab " + (numTabs + 1));
tabPane.getTabs().add(tab);

错误:tabPane.getTabs().addAll((Tab)FXMLLoader.load(this.getClass().getResource("tab.fxml")));`

enter image description here

tab.fxml:

javafx.fxml.LoadException:

修改:我已尝试删除de <TabPane fx:id="tabPane" layoutX="14.0" layoutY="105.0" prefHeight="200.0" prefWidth="292.0" tabClosingPolicy="UNAVAILABLE"> <tabs> <Tab text="Untitled Tab"> <content> <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Button layoutX="118.0" layoutY="74.0" mnemonicParsing="false" text="Button" /> </children> </AnchorPane> </content> </Tab> </tabs> </TabPane> 代码,但无效。

<TabPane>

FXML主文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

  <tabs>
    <Tab text="Untitled Tab">
      <content>
        <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
          <children>
            <Button layoutX="118.0" layoutY="74.0" mnemonicParsing="false" text="Button" />
          </children>
        </AnchorPane>
      </content>
    </Tab>
  </tabs>

堆栈跟踪:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="319.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="testpane2.FXMLDocumentController">
  <children>
    <Button fx:id="button" layoutX="126.0" layoutY="14.0" onAction="#handleButtonAction" text="Click Me!" />
    <Label fx:id="label" layoutX="126.0" layoutY="44.0" minHeight="16.0" minWidth="69.0" />
    <TabPane fx:id="tabPane" layoutX="14.0" layoutY="105.0" prefHeight="200.0" prefWidth="292.0" tabClosingPolicy="UNAVAILABLE" />
  </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

尝试使用:

<Tab text="Untitled Tab">
  <content>
    <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
      <children>
        <Button layoutX="118.0" layoutY="74.0" mnemonicParsing="false" text="Button" />
      </children>
    </AnchorPane>
  </content>
</Tab>