ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • javaFX - 입력 컨트롤
    고급 JAVA/FX 2020. 5. 22. 19:30

    TextField - 한 줄 입력

     

    TextArea - 다중 행 입력

     

    PasswordField - 패스워드 입력

     

    ComboBox - 제한된 항목 입력

     

    DatePicker - 날자 선택

     

    ColorPicker - 색상 선택

     

    HTMLEditor - HTML 입력

     

    Label - 입력 컨트롤의 제목을 표시할 때

     

     

    FXML로 입력 컨트롤 선언 방법

    <Label prefWidth="폭" prefHeight="높이" text="제목" />
    
    <TextField prefWidth="폭" prefHeight="높이" promptText="힌트문자열" />
    
    <PasswordFieldprefWidth="폭" prefHeight="높이" promptText="힌트문자열" />
    
    <ComboBox prefWidth="폭" prefHeight="높이" promptText="힌트문자열" />
    
       <items>
    
          <FXCollections fx:factory="observableArrayList">
    
              <String fx:value="공개" />
    
              <String fx:value="비공개" />
    
         </FxCollections>
    
      </items>
    
    </ComboBox>
    
    
    
    <DatePicker prefWidth="폭" prefHeight="높이" promptText="힌트문자열" />
    <TextArea prefWidth="폭" prefHeight="높이" promptText="힌트문자열" />
    
    
    
    promptText="힌트문자열" : 컨트롤이 포커스를 얻으면 사라짐.
    
    

     

    예제

    예제)
    
    FXML
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    
    
    <?import javafx.scene.text.*?>
    
    <?import javafx.scene.shape.*?>
    
    <?import javafx.scene.web.*?>
    
    <?import javafx.geometry.*?>
    
    <?import javafx.scene.image.*?>
    
    <?import java.lang.*?>
    
    <?import javafx.scene.control.*?>
    
    <?import javafx.scene.layout.*?>
    
    <?import javafx.collections.*?>
    
    
    
    <AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="thisisjava.RootController"
    
    prefHeight="380" prefWidth="485" >
    
       <children>
    
          <Label layoutX="22.0" layoutY="36.0" text="제목" />
    
          <TextField fx:id="txtTitle" layoutX="84.0" layoutY="32.0" prefHeight="23.0" prefWidth="375.0" />
    
          <Label layoutX="22.0" layoutY="69.0" text="비밀번호" />
    
          <PasswordField fx:id="txtPassword" layoutX="86.0" layoutY="65.0" prefHeight="23.0" prefWidth="132.0" />
    
          <Label layoutX="22.0" layoutY="104.0" text="공개" />
    
    <ComboBox fx:id="comboPublic" layoutX="86.0" layoutY="100.0" prefHeight="23.0" prefWidth="132.0" promptText="선택" >
    
    <items>
    
    <FXCollections fx:factory="observableArrayList">
    
    <String fx:value="공개"/>
    
    <String fx:value="비공개"/>
    
    </FXCollections>
    
    </items>
    
    </ComboBox>
    
          <Label layoutX="240.0" layoutY="104.0" text="게시종료" />
    
          <DatePicker fx:id="dateExit" layoutX="296.0" layoutY="100.0" promptText="날짜 선택"/>
    
          <Label layoutX="22.0" layoutY="135.0" text="내용" />
    
          <TextArea fx:id="txtContent" layoutX="22.0" layoutY="154.0" prefHeight="132.0" prefWidth="440.0"/>
    
          <Separator layoutX="13.0" layoutY="320" prefHeight="0.0" prefWidth="457.0" />
    
          <Button fx:id="btnReg" layoutX="189.0" layoutY="340" text="등록" onAction="#handleBtnRegAction"/>
    
          <Button fx:id="btnCancel" layoutX="252.0" layoutY="340" text="취소" onAction="#handleBtnCancelAction"/>
    
       </children>
    
     
    
    </AnchorPane>
    
    
    자바1)
    
    package thisisjava;
    
    
    
    import javafx.application.Application;
    
    import javafx.fxml.FXMLLoader;
    
    import javafx.scene.Parent;
    
    import javafx.scene.Scene;
    
    import javafx.stage.Stage;
    
    
    
    public class AppMain extends Application {
    
    @Override
    
    public void start(Stage primaryStage) throws Exception {
    
    Parent root = (Parent)FXMLLoader.load(getClass().getResource("root.fxml"));
    
    Scene scene = new Scene(root);
    
    
    primaryStage.setTitle("Input Control");
    
    primaryStage.setScene(scene);
    
    primaryStage.show();
    
    }
    
    
    public static void main(String[] args) {
    
    launch(args);
    
    }
    
     
    
    }
    
    
    자바2)
    
    package thisisjava;
    
    
    
    import java.net.URL;
    
    import java.time.LocalDate;
    
    import java.util.ResourceBundle;
    
    
    
    import javafx.application.Platform;
    
    import javafx.event.ActionEvent;
    
    import javafx.fxml.FXML;
    
    import javafx.fxml.Initializable;
    
    import javafx.scene.control.ComboBox;
    
    import javafx.scene.control.DatePicker;
    
    import javafx.scene.control.PasswordField;
    
    import javafx.scene.control.TextArea;
    
    import javafx.scene.control.TextField;
    
    
    
    public class RootController implements Initializable {
    
    @FXML private TextField txtTitle;
    
    @FXML private PasswordField txtPassword;
    
    @FXML private ComboBox<String> comboPublic;
    
    @FXML private DatePicker dateExit;
    
    @FXML private TextArea txtContent;
    
    
    @Override
    
    public void initialize(URL location, ResourceBundle resources) {
    
    }
    
    
    public void handleBtnRegAction(ActionEvent e) {
    
    String title = txtTitle.getText();
    
    System.out.println("title: " + title);
    
    
    String password = txtPassword.getText();
    
    System.out.println("password: " + password);
    
    
    String strPublic = comboPublic.getValue();
    
    System.out.println("public: " + strPublic);
    
    
    LocalDate localDate = dateExit.getValue();
    
    if(localDate != null) {
    
    System.out.println("dateExit: " + localDate.toString());
    
    }
    
    
    String content = txtContent.getText();
    
    System.out.println("content: " + content);
    
    }
    
    
    public void handleBtnCancelAction(ActionEvent e) {
    
    Platform.exit();
    
    }
    
     
    
    }

    '고급 JAVA > FX' 카테고리의 다른 글

    JavaFX - 차트 컨트롤  (0) 2020.05.23
    javaFX - view 컨트롤 (ImageView, ListView, TableView)  (0) 2020.05.22
    javaFX 컨트롤 - 버튼 컨트롤  (0) 2020.05.22
    javaFX 속성 바인딩  (0) 2020.05.22
    javaFX 이벤트 핸들러  (0) 2020.05.22

    댓글

Designed by Tistory.