2017-04-22 7 views
0

javafx 및 장면 작성기에서 콤보 상자 옵션을 표시하는 데 문제가 있습니다.JavaFX 및 장면 빌더가 콤보 상자 옵션을 표시하지 않습니다.

먼저 배열을 사용하려고했는데 작동하지 않는다고 생각합니다. 나는 다음 콤보 옵션을 표시하지 않을 여전히

ComboBox combo = new ComboBox(list); 

이 같은 콤보 상자의 인스턴스를 ObservableList<String>를 사용하여 만들려고. 컨트롤러 클래스 :

package javafxapplication1; 

import javafxapplication1.JavaFXApplication1; 
import java.net.URL; 
import static java.util.Collections.list; 
import java.util.ResourceBundle; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javax.lang.model.element.Element; 

/** 
* 
* @author KJ4CC 
*/ 
public class FXMLDocumentController implements Initializable { 
    ObservableList<String> options = 
    FXCollections.observableArrayList(
     "Option 1", 
     "Option 2", 
     "Option 3" 
    ); 

    @FXML 
    private Label label; 
    @FXML 
    private TextField dateText; 
    @FXML 
    private TextField time; 
    @FXML 
    private ComboBox band; 

    public void setTimeDate(){ 
     JavaFXApplication1 javaFXApp = new JavaFXApplication1(); 
     dateText.setText(javaFXApp.getDate()); 

    } 

    public void setTime(){ 
     JavaFXApplication1 javaFXApp = new JavaFXApplication1(); 
     time.setText(javaFXApp.getTime()); 

    } 
    @Override 

    public void initialize(URL url, ResourceBundle rb) { 
    band = new ComboBox(options); 

    }  

} 

홈페이지 자바 클래스 :

public class JavaFXApplication1 extends Application { 
    private FXMLDocumentController initScene; 
    private DateFormat dtf; 
    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 

     stage.show(); 


    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 
    public String getDate(){ 
     dtf = new SimpleDateFormat("dd/MM/yy"); 
     Date dateobj = new Date(); 
     return dtf.format(dateobj); 
    } 
    public String getTime(){ 
     dtf = new SimpleDateFormat("HH:mm;ss"); 
     Date dateobj = new Date(); 
     return dtf.format(dateobj); 
    } 

} 

FXML

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

<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.ComboBox?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.Menu?> 
<?import javafx.scene.control.MenuBar?> 
<?import javafx.scene.control.MenuItem?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.BorderPane?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.text.Text?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="383.0" prefWidth="590.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController"> 
    <top> 
     <VBox prefHeight="235.0" prefWidth="543.0" BorderPane.alignment="CENTER"> 
     <children> 
      <MenuBar> 
       <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> 
      <TableView prefHeight="210.0" prefWidth="634.0"> 
       <columns> 
       <TableColumn prefWidth="75.0" text="C1" /> 
       <TableColumn prefWidth="75.0" text="C2" /> 
       </columns> 
       <VBox.margin> 
        <Insets left="2.0" right="2.0" /> 
       </VBox.margin> 
      </TableView> 
     </children> 
     </VBox> 
    </top> 
    <bottom> 
     <VBox prefHeight="160.0" prefWidth="676.0" BorderPane.alignment="CENTER"> 
     <children> 
      <HBox prefHeight="22.0" prefWidth="625.0"> 
       <children> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Date:" wrappingWidth="83.462890625"> 
        <HBox.margin> 
         <Insets left="5.0" right="20.0" /> 
        </HBox.margin> 
        </Text> 
        <Label onMouseClicked="#setTimeDate" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Time:" wrappingWidth="73.40625" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Call:" wrappingWidth="122.94921875"> 
        <HBox.margin> 
         <Insets left="5.0" /> 
        </HBox.margin> 
        </Text> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Band:" wrappingWidth="58.443359375" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Freq:" wrappingWidth="106.974609375" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Location:" /> 
       </children> 
      </HBox> 
      <HBox prefHeight="35.0" prefWidth="625.0"> 
       <children> 
        <TextField id="dateText" fx:id="dateText" prefHeight="25.0" prefWidth="94.0"> 
        <HBox.margin> 
         <Insets left="5.0" /> 
        </HBox.margin> 
        </TextField> 
        <TextField fx:id="time" layoutX="10.0" layoutY="10.0" prefHeight="25.0" prefWidth="71.0"> 
        <padding> 
         <Insets left="5.0" /> 
        </padding> 
        <HBox.margin> 
         <Insets left="10.0" right="10.0" /> 
        </HBox.margin> 
        </TextField> 
        <TextField layoutX="104.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0"> 
        <HBox.margin> 
         <Insets right="10.0" /> 
        </HBox.margin> 
        </TextField> 
        <ComboBox fx:id="band" prefHeight="25.0" prefWidth="57.0"> 
        <HBox.margin> 
         <Insets right="10.0" /> 
        </HBox.margin> 
        </ComboBox> 
        <TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0"> 
        <HBox.margin> 
         <Insets right="10.0" /> 
        </HBox.margin> 
        </TextField> 
        <TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0" /> 
       </children> 
      </HBox> 
      <HBox prefHeight="30.0" prefWidth="580.0"> 
       <children> 
        <Text fill="#14bdd7" onMouseClicked="#setTimeDate" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Date"> 
        <HBox.margin> 
         <Insets right="20.0" /> 
        </HBox.margin> 
        </Text> 
        <Text fill="#14bdd7" layoutX="10.0" layoutY="23.0" onMouseClicked="#setTime" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Time" /> 
       </children> 
      </HBox> 
     </children> 
     <BorderPane.margin> 
      <Insets left="5.0" right="5.0" /> 
     </BorderPane.margin> 
     </VBox> 
    </bottom> 
</BorderPane> 

어떤 도움을 주시면 감사하겠습니다! 나는이 장면 제작자에게 새로운 것과 javafx를 사용하고있다. 나는 그 길을 감히 느낄 뿐이다. 도와 주셔서 감사합니다!

답변

-1

좋아, 그래서 문제를 알아 냈다. 콤보 상자의 새 인스턴스를 만들어 빈 컨테이너를 만듭니다. 다음은 개정 된 작업 코드가있는 메소드입니다.

public void initialize(URL url, ResourceBundle rb) { 
     ObservableList<String> options = 
    FXCollections.observableArrayList(
     "Option 1", 
     "Option 2", 
     "Option 3" 
    ); 
     band.setItems(options); 

    }