2012-07-14 22 views
19

में तालिका दृश्य का पूरा आकार नहीं लेगा, मैं दो कॉलम के साथ एक टेबल बनाने की कोशिश कर रहा हूं। मैं netbeans 7.2 में शामिल दृश्य निर्माता का उपयोग कर रहा हूं। मैंने उन सभी उदाहरणों में देखा है जो आपको करने की ज़रूरत है, तालिका तालिका को तालिका में खींचें और यह पूर्ण आकार लेगा, यह मेरे मामले में सच नहीं है। यह दृश्य निर्माता द्वारा उत्पन्न fxml फ़ाइल है। बस स्पष्ट होने के लिए मैं जावा से तालिका गुणों को नहीं बदल रहा हूं।तालिका कॉलम javaFX

fxml: जब एक FXML दस्तावेज़ के साथ लागू

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

<?import java.lang.*?> 
<?import java.net.*?> 
<?import java.util.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.image.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 

<AnchorPane id="AnchorPane" prefHeight="758.0" prefWidth="956.0" styleClass="screen" xmlns:fx="http://javafx.com/fxml" fx:controller="bgu.dcr.az.cpu.ui.expb.ExperimentBuilderScreen"> 
    <children> 
    <GridPane id="gridPane1" prefHeight="517.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <children> 
     <Label id="label1" alignment="CENTER" contentDisplay="CENTER" prefHeight="38.0" prefWidth="9999.0" styleClass="caption" text="Create New Experiment" textFill="#990000" GridPane.columnIndex="0" GridPane.rowIndex="0"> 
      <font> 
      <Font name="Consolas Bold" size="20.0" /> 
      </font> 
     </Label> 
     <HBox id="hBox1" alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="5.0" GridPane.columnIndex="0" GridPane.rowIndex="1"> 
      <children> 
      <Label id="label2" graphicTextGap="0.0" styleClass="field-label" text="Experiment Name"> 
       <font> 
       <Font name="Consolas" size="15.0" /> 
       </font> 
       <HBox.margin> 
       <Insets top="3.0" /> 
       </HBox.margin> 
      </Label> 
      <TextField id="textField1" prefWidth="200.0" HBox.hgrow="ALWAYS" /> 
      <Button id="button2" fx:id="saveButton" styleClass="dialog-button" text="Save" /> 
      <Button id="button3" styleClass="dialog-button" text="Dismiss" /> 
      </children> 
      <padding> 
      <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x3" /> 
      </padding> 
     </HBox> 
     <VBox id="VBox" alignment="CENTER" spacing="5.0" GridPane.columnIndex="0" GridPane.rowIndex="2"> 
      <children> 
      <Label id="label3" prefWidth="9999.0" styleClass="field-label" text="Experiment Tests"> 
       <VBox.margin> 
       <Insets left="5.0" top="5.0" /> 
       </VBox.margin> 
      </Label> 
      <HBox id="HBox" alignment="CENTER" spacing="5.0"> 
       <children> 
       <Button id="button1" prefHeight="999.0" styleClass="add-button" text="New"> 
        <HBox.margin> 
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x3" /> 
        </HBox.margin> 
       </Button> 
       <ListView id="listView1" fx:id="tests" orientation="HORIZONTAL" prefHeight="999.0" prefWidth="200.0" HBox.hgrow="ALWAYS"> 
        <HBox.margin> 
        <Insets bottom="4.0" right="5.0" top="4.0" /> 
        </HBox.margin> 
       </ListView> 
       </children> 
      </HBox> 
      </children> 
     </VBox> 
     <VBox id="vBox1" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="0" GridPane.rowIndex="3"> 
      <children> 
      <GridPane id="gridPane2" VBox.vgrow="ALWAYS"> 
       <children> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.rowSpan="4"> 
        <children> 
        <Label id="label4" text="Algorithms"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_algo.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" fx:id="newAlgorithmButton" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" /> 
        </children> 
        <GridPane.margin> 
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" fx:id="x2" /> 
        </GridPane.margin> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="1" GridPane.margin="$x2" GridPane.rowIndex="1"> 
        <children> 
        <Label id="label4" text="Problem Generator"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_pgen.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="2" GridPane.margin="$x2" GridPane.rowIndex="1"> 
        <children> 
        <Label id="label4" prefWidth="9999.0" text="Correctness tester"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_ctest.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" maxWidth="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="1" GridPane.margin="$x2" GridPane.rowIndex="2"> 
        <children> 
        <Label id="label4" prefWidth="130.0" text="Message delayer"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_mdel.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" maxWidth="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="2" GridPane.margin="$x2" GridPane.rowIndex="2"> 
        <children> 
        <Label id="label4" prefWidth="130.0" text="Limiter"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_limiter.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" maxWidth="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="3" GridPane.margin="$x2" GridPane.rowIndex="0" GridPane.rowSpan="4"> 
        <children> 
        <Label id="label4" prefWidth="9999.0" text="Statistic collectors"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_scol.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" /> 
        </children> 
       </VBox> 
       <HBox id="hBox1" alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="5.0" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="0"> 
        <children> 
        <Label id="label2" graphicTextGap="0.0" styleClass="field-label" text="Name"> 
         <HBox.margin> 
         <Insets top="7.0" /> 
         </HBox.margin> 
        </Label> 
        <TextField id="textField1" maxHeight="28.0" minHeight="28.0" prefHeight="28.0" prefWidth="200.0" text="Empty Test" HBox.hgrow="ALWAYS" /> 
        </children> 
        <padding> 
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x3" /> 
        </padding> 
       </HBox> 
       <VBox id="vBox3" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="3" /> 
       </children> 
       <columnConstraints> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       <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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
       </rowConstraints> 
      </GridPane> 
      </children> 
     </VBox> 
     <VBox id="vBox4" alignment="CENTER" styleClass="with-dashed-border" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.rowIndex="4" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS"> 
      <children> 
      <TableView id="tableView1" prefHeight="54.0" prefWidth="837.0" VBox.vgrow="ALWAYS"> 
       <columns> 
       <TableColumn prefWidth="75.0" text="Column X" /> 
       <TableColumn prefWidth="75.0" text="Column X" /> 
       </columns> 
      </TableView> 
      </children> 
      <GridPane.margin> 
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" fx:id="x2" /> 
      </GridPane.margin> 
     </VBox> 
     </children> 
     <columnConstraints> 
     <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
     </columnConstraints> 
     <rowConstraints> 
     <RowConstraints maxHeight="38.0" minHeight="38.0" prefHeight="38.0" vgrow="NEVER" /> 
     <RowConstraints maxHeight="32.0" minHeight="32.0" prefHeight="32.0" vgrow="NEVER" /> 
     <RowConstraints maxHeight="100.0" minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" /> 
     <RowConstraints vgrow="SOMETIMES" /> 
     <RowConstraints maxHeight="100.0" prefHeight="100.0" valignment="CENTER" vgrow="ALWAYS" /> 
     </rowConstraints> 
    </GridPane> 
    </children> 
    <stylesheets> 
    <URL value="@_style.css" /> 
    <URL value="@../_style.css" /> 
    </stylesheets> 
</AnchorPane> 

उत्तर

41

अद्यतन

JavaFX 2.2+ विज्ञप्ति में नई सुविधाएँ इस जवाब अप्रचलित है।

एक FXML आधारित समाधान जो FXML के माध्यम से कॉलम आकार को सेट करने की अनुमति देने के लिए नई FXML सुविधाओं का उपयोग करता है: FXML set TableView column resize policy प्रदान किया जाता है।

<TableView fx:id="tableView" layoutX="110.0" layoutY="78.0" prefHeight="200.0" prefWidth="396.0"> 
    <columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/></columnResizePolicy> 
    <columns> 
     <TableColumn prefWidth="75.0" text="Column X" /> 
     <TableColumn prefWidth="75.0" text="Column X" /> 
    </columns> 
</TableView> 

आप FXML का उपयोग नहीं कर रहे हैं, तो tableview के आकार नीति अभी भी कोड से सेट किया जा सकता के रूप में इस सवाल का जवाब में परिभाषित:

tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 

अपने FXML में, आप के पसंदीदा चौड़ाई को परिभाषित किया है तालिका 837 तक, और प्रत्येक कॉलम की पसंदीदा चौड़ाई 75 तक है। डिफ़ॉल्ट रूप से, जावाएफएक्स कॉलम आकार बदलने वाली नीति कॉलम को उनकी पसंदीदा चौड़ाई पर रखेगी। जब तक आप या तो नई कॉलम आकार बदलने वाली नीति सेट नहीं करते हैं या अपनी तालिका चौड़ाई को जोड़ने के लिए अपने सभी कॉलम की पसंदीदा चौड़ाई के योग को सेट नहीं करते हैं, तब तक आप अपनी संपूर्ण तालिका की चौड़ाई भरने वाले टेबल कॉलम नहीं प्राप्त करेंगे।

जावाएफएक्स modify the default column resizing policy पर एक तंत्र प्रदान करता है। कॉलम आकार बदलने की नीति को CONSTRAINED_RESIZE_POLICY पर सेट करके, प्रत्येक कॉलम की चौड़ाई का योग तालिकादृश्य की चौड़ाई को भरने के लिए बाध्य किया जाएगा - भले ही तालिकादृश्य का आकार बदल दिया गया हो या व्यक्तिगत कॉलम का आकार बदल दिया गया हो। यह संभवतः व्यवहार की इच्छा है।

जबकि FXML से तालिका में CONSTRAINED_RESIZE_POLICY सेट करने का कोई तरीका हो सकता है, मुझे एक के बारे में पता नहीं है (हालांकि fxml में मेरे कौशल प्राथमिक हैं - इसलिए शायद कोई और अधिक जानकारी शुद्ध fxml का उपयोग कर कॉन्फ़िगरेशन को पूरा कर सकता है)।

आप क्या कर सकते हैं अपनी fxml फ़ाइल के लिए नियंत्रक को परिभाषित करना है और फिर नियंत्रक में आकार बदलें नीति को कॉन्फ़िगर करें। निम्नलिखित कोड एक पूर्ण उदाहरण प्रदर्शित करता है (जावाएफएक्स 2.2 बी 17 पूर्वावलोकन के खिलाफ लिखा गया है)।

FXML फ़ाइल:

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

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

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="tableview.TableViewController"> 
    <children> 
    <TableView fx:id="tableView" layoutX="110.0" layoutY="78.0" prefHeight="200.0" prefWidth="396.0"> 
     <columns> 
     <TableColumn prefWidth="75.0" text="Column X" /> 
     <TableColumn prefWidth="75.0" text="Column X" /> 
     </columns> 
    </TableView> 
    </children> 
</AnchorPane> 

नियंत्रक वर्ग:

package tableview; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TableView; 

/* controller class for the tableview fxml definition */ 
public class TableViewController implements Initializable { 
    @FXML // fx:id="tableView" 
    private TableView<?> tableView; // Value injected by FXMLLoader 

    @Override // This method is called by the FXMLLoader when initialization is complete 
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) { 
    assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file 'tableview.fxml'."; 

    // initialize your logic here: all @FXML variables will have been injected 
    tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 
    } 
} 

आवेदन वर्ग:

package tableview; 

import java.io.IOException; 
import java.net.URL; 
import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.Stage; 

/** Main application class for tableview fxml demo application */ 
public class TableViewApplication extends Application { 
    public static void main(String[] args) { launch(args); } 
    @Override public void start(Stage stage) throws IOException { 
    AnchorPane layout = FXMLLoader.load(
     new URL(TableViewApplication.class.getResource("tableview.fxml").toExternalForm()) 
    ); 
    stage.setScene(new Scene(layout)); 
    stage.show(); 
    } 
} 
+0

आपको बहुत धन्यवाद @ jewelsea आप पहले और एकमात्र जगह थे जहां मुझे कॉलम के पुन: आकार को नियंत्रित करने का तरीका मिला। अगर मैं fxml से तालिका को नियंत्रित करने के लिए एक तरफ आ गया हूं तो मैं इसे यहां पोस्ट करूंगा। – raven

+0

नियंत्रक में यूआई कॉन्फ़िगरेशन जोड़ने के लिए मुझे अजीब लग रहा है। आप इस लिंक में दिखाए गए .fxml फ़ाइल में एक XML टैग जोड़कर ऐसा कर सकते हैं: http://stackoverflow.com/questions/14095708/fxml-set-tableview-column-resize-policy – gabuzo

+0

लिंक gabuzo के लिए धन्यवाद , मैंने नई जावाएफएक्स 2.2 कार्यक्षमता को प्रतिबिंबित करने के उत्तर के द्वारा अद्यतन किया है जो अब आपको FXML से तालिका की कॉलम आकार बदलने नीति को परिभाषित करने की अनुमति देता है। – jewelsea

3

यह नहीं वर्तमान में एक tableView FXML से पर एक CONSTRAINED_RESIZE_POLICY स्थापित करने के लिए संभव है:

https://forums.oracle.com/forums/thread.jspa?threadID=2337733

+2

2.2 रिलीज, अब FXML से 'tableView' पर 'CONSTRAINED_RESIZE_POLICY' सेट करना संभव है (वाक्यविन्यास के लिए मेरा अद्यतन उत्तर देखें)। – jewelsea

2

मामले में आप JavaFX GUI बिल्डर "SceneBuilder 2.0" का उपयोग करें - का उपयोग tableview संपत्ति ColumnResizePolicy "विवश-आकार" मूल्य।