у меня есть макет приложения:
https://i.stack.imgur.com/YsK5R.png
в результате написания кода я получаю это
https://i.stack.imgur.com/j2Nym.png
Вопрос: Как убрать верхнюю строку с закрывающимися вкладками и оставить только как на раскладке?
структура проекта:
-[источник]
.....|-[образец]
..............|- Main
..............|- пример.fxml
..............|- SampleController
..............|- Tab1.fxml
..............|- Tab1Controller
..............|- Tab2.fxml
..............|- Tab2Controller
КОД
класс Главный
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Stage primary;
private BorderPane rootLayout;
@Override
public void start(Stage primary) throws Exception{
this.primary = primary;
initRootLayout();
showTab();
}
private void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("sample.fxml"));
rootLayout = loader.load();
Scene scene = new Scene(rootLayout);
primary.setScene(scene);
primary.show();
}catch (IOException e){
e.printStackTrace();}
}
private void showTab() {
try {
FXMLLoader loaderTab1 = new FXMLLoader();
loaderTab1.setLocation(Main.class.getResource("Tab1.fxml"));
FXMLLoader loaderTab2 = new FXMLLoader();
loaderTab2.setLocation(Main.class.getResource("Tab2.fxml"));
TabPane tabPane = new TabPane();
Tab tab1 = new Tab();
Tab tab2 = new Tab();
tab1.setContent(loaderTab1.load());
tab2.setContent(loaderTab2.load());
tabPane.getTabs().addAll(tab1,tab2);
rootLayout.setCenter(tabPane);
} catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
образец.fxml
<?xml version = "1.0" encoding = "UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight = "-Infinity" maxWidth = "-Infinity" minHeight = "-Infinity" minWidth = "-Infinity" prefHeight = "400.0" prefWidth = "600.0" xmlns = "http://javafx.com/javafx/8.0.172-ea" xmlns:fx = "http://javafx.com/fxml/1" fx:controller = "sample.SampleController">
<top>
<MenuBar BorderPane.alignment = "CENTER">
<menus>
<Menu mnemonicParsing = "false" text = "File">
<items>
<MenuItem mnemonicParsing = "false" text = "Close" />
</items>
</Menu>
<Menu mnemonicParsing = "false" text = "Edit">
<items>
<MenuItem mnemonicParsing = "false" text = "Delete" />
</items>
</Menu>
<Menu mnemonicParsing = "false" text = "Help">
<items>
<MenuItem mnemonicParsing = "false" text = "About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
Tab1.fxml = Tab2.fxml = ... = Tab{n}.fxml
<?xml version = "1.0" encoding = "UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<TabPane maxHeight = "-Infinity" maxWidth = "-Infinity" minHeight = "-Infinity" minWidth = "-Infinity" prefHeight = "400.0" prefWidth = "600.0" tabClosingPolicy = "UNAVAILABLE" xmlns = "http://javafx.com/javafx/10.0.1" xmlns:fx = "http://javafx.com/fxml/1" fx:controller = "sample.Tab1Controller">
<tabs>
<Tab text = "Tab1">
<content>
<SplitPane dividerPositions = "0.29797979797979796" prefHeight = "160.0" prefWidth = "200.0">
<items>
<AnchorPane minHeight = "0.0" minWidth = "0.0" prefHeight = "160.0" prefWidth = "100.0">
<children>
<TableView fixedCellSize = "1.0" layoutX = "-12.0" layoutY = "27.0" prefHeight = "371.0" prefWidth = "176.0" AnchorPane.bottomAnchor = "0.0" AnchorPane.leftAnchor = "0.0" AnchorPane.rightAnchor = "0.0" AnchorPane.topAnchor = "0.0">
<columns>
<TableColumn prefWidth = "75.0" text = "C1" />
<TableColumn prefWidth = "75.0" text = "C2" />
</columns>
<columnResizePolicy>
<TableView fx:constant = "CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight = "0.0" minWidth = "0.0" prefHeight = "160.0" prefWidth = "100.0">
<children>
<Label layoutY = "14.0" text = "Label" AnchorPane.leftAnchor = "5.0" AnchorPane.topAnchor = "14.0" />
<GridPane layoutX = "98.0" layoutY = "125.0" AnchorPane.leftAnchor = "5.0" AnchorPane.rightAnchor = "5.0" AnchorPane.topAnchor = "35.0">
<columnConstraints>
<ColumnConstraints hgrow = "SOMETIMES" minWidth = "10.0" prefWidth = "100.0" />
<ColumnConstraints hgrow = "SOMETIMES" minWidth = "10.0" prefWidth = "100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight = "10.0" prefHeight = "30.0" vgrow = "SOMETIMES" />
<RowConstraints minHeight = "10.0" prefHeight = "30.0" vgrow = "SOMETIMES" />
<RowConstraints minHeight = "10.0" prefHeight = "30.0" vgrow = "SOMETIMES" />
</rowConstraints>
<children>
<Label text = "Label" />
<Label text = "Label" GridPane.rowIndex = "1" />
<Label text = "Label" GridPane.rowIndex = "2" />
<Label text = "Label" GridPane.columnIndex = "1" />
<Label text = "Label" GridPane.columnIndex = "1" GridPane.rowIndex = "1" />
<Label text = "Label" GridPane.columnIndex = "1" GridPane.rowIndex = "2" />
</children>
</GridPane>
</children></AnchorPane>
</items>
</SplitPane>
</content></Tab>
</tabs>
</TabPane>
этот вопрос дублируется от этого: https://ru.stackoverflow.com/questions/941060/%D0%A3%D0%B1%D1%80%D0%B0%D1%82%D1%8C-%D0%B2%D1%82%D0% BE%D1%80%D0%B8%D1%87%D0%BD%D1%8B%D0%B5-%D0%B2%D0%BA%D0%BB%D0%B0%D0%B4%D0%BA %D0%B8




Есть много способов сделать это в зависимости от универсальности, которую вы хотите добавить к своему приложению/макету.
Но самое главное, что вы, похоже, ошиблись, перепутав TabPane и Tab. В то время как первый является контейнером, вторые соответствуют каждой вкладке, которую вы можете иметь. Обычно у вас есть один TabPane, который содержит много Tab(ов) (даже если это не обязательно).
Одним из решений может быть изменение вашего метода showTab, выполнив это (прочитайте комментарий)
private void showTab() {
try {
// Tab 1 loader
FXMLLoader loaderTab1 = new FXMLLoader(Main.class.getResource("Tab1.fxml"));
// Tab 2 loader
FXMLLoader loaderTab2 = new FXMLLoader(Main.class.getResource("Tab2.fxml"));
TabPane tabPane = new TabPane();
// Remove the 4 following lines, Tabs are already declared in FXML
//Tab tab1 = new Tab();
//Tab tab2 = new Tab();
//tab1.setContent(loaderTab1.load());
//tab2.setContent(loaderTab2.load());
Tab tab1 = loaderTab1.load();
Tab tab2 = loaderTab2.load();
tabPane.getTabs().addAll(tab1,tab2);
rootLayout.setCenter(tabPane);
} catch (IOException e){
e.printStackTrace();
}
}
И измените свои файлы FXML, как показано ниже (теги TabPane и tabs удалены):
<Tab xmlns = "http://javafx.com/javafx/10.0.1" xmlns:fx = "http://javafx.com/fxml/1" fx:controller = "sample.Tab1Controller" text = "Tab1">
<content>
<SplitPane dividerPositions = "0.29797979797979796" prefHeight = "160.0" prefWidth = "200.0">
<items>
<AnchorPane minHeight = "0.0" minWidth = "0.0" prefHeight = "160.0" prefWidth = "100.0">
<children>
<TableView fixedCellSize = "1.0" layoutX = "-12.0" layoutY = "27.0" prefHeight = "371.0" prefWidth = "176.0" AnchorPane.bottomAnchor = "0.0" AnchorPane.leftAnchor = "0.0" AnchorPane.rightAnchor = "0.0" AnchorPane.topAnchor = "0.0">
<columns>
<TableColumn prefWidth = "75.0" text = "C1" />
<TableColumn prefWidth = "75.0" text = "C2" />
</columns>
<columnResizePolicy>
<TableView fx:constant = "CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight = "0.0" minWidth = "0.0" prefHeight = "160.0" prefWidth = "100.0">
<children>
<Label layoutY = "14.0" text = "Label" AnchorPane.leftAnchor = "5.0" AnchorPane.topAnchor = "14.0" />
<GridPane layoutX = "98.0" layoutY = "125.0" AnchorPane.leftAnchor = "5.0" AnchorPane.rightAnchor = "5.0" AnchorPane.topAnchor = "35.0">
<columnConstraints>
<ColumnConstraints hgrow = "SOMETIMES" minWidth = "10.0" prefWidth = "100.0" />
<ColumnConstraints hgrow = "SOMETIMES" minWidth = "10.0" prefWidth = "100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight = "10.0" prefHeight = "30.0" vgrow = "SOMETIMES" />
<RowConstraints minHeight = "10.0" prefHeight = "30.0" vgrow = "SOMETIMES" />
<RowConstraints minHeight = "10.0" prefHeight = "30.0" vgrow = "SOMETIMES" />
</rowConstraints>
<children>
<Label text = "Label" />
<Label text = "Label" GridPane.rowIndex = "1" />
<Label text = "Label" GridPane.rowIndex = "2" />
<Label text = "Label" GridPane.columnIndex = "1" />
<Label text = "Label" GridPane.columnIndex = "1" GridPane.rowIndex = "1" />
<Label text = "Label" GridPane.columnIndex = "1" GridPane.rowIndex = "2" />
</children>
</GridPane>
</children></AnchorPane>
</items>
</SplitPane>
</content>
</Tab>
Действительно, это еще одно доступное решение.
Спасибо! Но я тоже решил свой вопрос. Я удалил отмеченные строки (вы также отметили это) и изменил код из Tab{n}.fxml: я удалил все теги до <SplitPane> и после </SplitPane> и добавил
maxHeight = "-Infinity" maxWidth = "-Infinity" minHeight = "-Infinity" minWidth = "-Infinity" prefHeight = "400.0" prefWidth = "600.0" xmlns = "http://javafx.com/javafx/10.0.1" xmlns:fx = "http://javafx.com/fxml/1" fx:controller = "sample.Tab1Controller"в тег <SplitPane>