您的位置:首页 > 编程语言 > Java开发

利用JavaFx开发RIA桌面应用-ComboBox组合编辑框水平增长

2016-10-25 11:20 841 查看
转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/52920032

1.常规使用

在初次使用ComboBox组合编辑框时,会遇到一个棘手问题,就是不能让控件自适应长度,以以下为例:

Text serverName = new Text(Constant.SERVER_NAME);
ComboBox serverNameInput = myStyleComboBox.getComboBox(1,1);
serverNameInput.setEditable(true);
grid.add(serverName,1,0,2,1);
grid.add(serverNameInput,3,0);
grid.setHgrow(serverNameInput,Priority.ALWAYS);


布局效果图:



其中服务器名称、用户名、数据库3行的输入框没有与其他行对齐,当前使用的是ComboBox默认设置,其中我也使用了grid.setHgrow()方法,让其水平增长,但依旧没有效果。

2.实现方式

接下来讲讲ComboBox组合编辑框水平增长的实现方式,其实很简单,给ComboBox设置如下方法:

serverNameInput.setMaxWidth(Double.MAX_VALUE);


控件有自带长度设置方法,但不是自适应,那是固定设置。

修改后的代码片段如下:

Text serverName = new Text(Constant.SERVER_NAME);
ComboBox serverNameInput = myStyleComboBox.getComboBox(1,1);
serverNameInput.setEditable(true);
serverNameInput.setMaxWidth(Double.MAX_VALUE);
grid.add(serverName,1,0,2,1);
grid.add(serverNameInput,3,0);
grid.setHgrow(serverNameInput,Priority.ALWAYS);

Text userName = new Text(Constant.USER_NAME);
ComboBox userNameInput = myStyleComboBox.getComboBox(1,2);
userNameInput.setEditable(true);
userNameInput.setMaxWidth(Double.MAX_VALUE);
grid.add(userName,1,1,2,1);
grid.add(userNameInput,3,1);

Text passWord = new Text(Constant.PASS_WORD);
PasswordField passWordInput = new PasswordField();
grid.add(passWord,1,2,2,1);
grid.add(passWordInput,3,2);

Text databaseName = new Text(Constant.DATABASE);
ComboBox databaseNameInput = new ComboBox();
databaseNameInput.setEditable(true);
databaseNameInput.setMaxWidth(Double.MAX_VALUE);
grid.add(databaseName,1,3,2,1);
grid.add(databaseNameInput,3,3);


实现水平增长的效果图:



3.布局源码

public ScrollPane getInitialLayout(){
// 初始化button管理类
MyStyleButton myStyleButton = MyStyleButton.getInstance();
MyStyleComboBox myStyleComboBox = MyStyleComboBox.getInstance();

// 创建布局
ScrollPane scrollPaneLayout = new ScrollPane();
BorderPane borderPaneLayout = new BorderPane();

// 创建center布局
VBox vBoxCenter = new VBox();
// 创建bottom布局
VBox vBoxBottom = new VBox();

// -------------------------------Center布局区域---------------------------------------
// 数据库配置框
GridPane grid = new GridPane();
grid.setVgap(10);
grid.setHgap(10);
grid.setPadding(new Insets(50,0,0,0));
//grid.setGridLinesVisible(true);
grid.setAlignment(Pos.CENTER);

Text serverName = new Text(Constant.SERVER_NAME); ComboBox serverNameInput = myStyleComboBox.getComboBox(1,1); serverNameInput.setEditable(true); serverNameInput.setMaxWidth(Double.MAX_VALUE); grid.add(serverName,1,0,2,1); grid.add(serverNameInput,3,0); grid.setHgrow(serverNameInput,Priority.ALWAYS); Text userName = new Text(Constant.USER_NAME); ComboBox userNameInput = myStyleComboBox.getComboBox(1,2); userNameInput.setEditable(true); userNameInput.setMaxWidth(Double.MAX_VALUE); grid.add(userName,1,1,2,1); grid.add(userNameInput,3,1); Text passWord = new Text(Constant.PASS_WORD); PasswordField passWordInput = new PasswordField(); grid.add(passWord,1,2,2,1); grid.add(passWordInput,3,2); Text databaseName = new Text(Constant.DATABASE); ComboBox databaseNameInput = new ComboBox(); databaseNameInput.setEditable(true); databaseNameInput.setMaxWidth(Double.MAX_VALUE); grid.add(databaseName,1,3,2,1); grid.add(databaseNameInput,3,3);

Text databaseConfig = new Text(Constant.DATABASE_CONFIG);
TextField databaseConfigInput = new TextField();
Button buttonOfDataPath = myStyleButton.getShadowButton(Constant.BROWSE, ImageUtil.getImageView("image/upload_file.png"));
buttonOfDataPath.getStyleClass().add("button-data");
grid.add(databaseConfig,1,4,2,1);
grid.add(databaseConfigInput,3,4);
grid.add(buttonOfDataPath,4,4);

Text searchType = new Text(Constant.SEARCH_TYPE);
ToggleGroup toggleGroup = new ToggleGroup();
RadioButton radioButtonSingle = new RadioButton();
RadioButton radioButtonBatch = new RadioButton();
toggleGroup.getToggles().addAll(radioButtonSingle,radioButtonBatch);
radioButtonSingle.setText(Constant.SINGLE_SEARCH);
radioButtonBatch.setText(Constant.BATCH_SEARCH);
grid.add(searchType,1,5,2,1);
grid.add(radioButtonSingle,3,5);
grid.add(radioButtonBatch,3,6);

CheckBox remenberPassWord = new CheckBox();
Text remenberPassWordTitle = new Text(Constant.REMENBER_PASS_WORD);
grid.add(remenberPassWord,1,7);
grid.add(remenberPassWordTitle,2,7);

// 确认取消-按钮
HBox hBoxOfBotton = new HBox();
Button buttonOfConfirm = myStyleButton.getShadowButton(Constant.EXECUTE,ImageUtil.getImageView("image/ok.png"));
Button buttonOfCancel = myStyleButton.getShadowButton(Constant.CANCEL,ImageUtil.getImageView("image/close.png"));
buttonOfConfirm.getStyleClass().add("button-data");
buttonOfCancel.getStyleClass().add("button-data");
hBoxOfBotton.getChildren().addAll(buttonOfConfirm,buttonOfCancel);
hBoxOfBotton.getStyleClass().add("hbox-button-center-catalog");

vBoxCenter.getChildren().addAll(grid,hBoxOfBotton);
vBoxCenter.getStyleClass().add("vbox-center-catalog");

// -------------------------------Bottom布局区域---------------------------------------
TextArea console = new TextArea();
console.setEditable(false);
vBoxBottom.getChildren().add(console);
vBoxBottom.getStyleClass().add("vbox-bottom-catalog");

borderPaneLayout.setLeft(new Rectangle(200,200,Color.WHITESMOKE));
borderPaneLayout.setRight(new Rectangle(200,200,Color.WHITESMOKE));
borderPaneLayout.setCenter(vBoxCenter);
borderPaneLayout.setBottom(vBoxBottom);

// 设置监听
radioButtonSingle.selectedProperty().addListener(new MyRadioButtonListener(radioButtonSingle,serverNameInput,userNameInput,passWordInput,databaseNameInput,databaseConfigInput,buttonOfDataPath));
radioButtonBatch.selectedProperty().addListener(new MyRadioButtonListener(radioButtonBatch,serverNameInput,userNameInput,passWordInput,databaseNameInput,databaseConfigInput,buttonOfDataPath));

scrollPaneLayout.setContent(borderPaneLayout);
scrollPaneLayout.setFitToHeight(true);
scrollPaneLayout.setFitToWidth(true);

return scrollPaneLayout;
}


4.参考资料

http://stackoverflow.com/questions/29489880/javafx-how-to-make-combobox-hgrow

相关文章:

《 利用JavaFx开发RIA桌面应用-TableView操作》

《 利用JavaFx开发RIA桌面应用-Clipboard剪贴板操作》

《利用JavaFx开发RIA桌面应用-TextField替换PasswordField做密码框》

《利用JavaFx开发RIA桌面应用-半透明界面设计》

《利用JavaFx开发RIA桌面应用-加载等待界面设计》

《利用JavaFx开发RIA桌面应用-文件拖拽》

《利用JavaFx开发RIA桌面应用-改变stage的标题栏的图标》

《利用JavaFx开发RIA桌面应用-事件监听》

《利用JavaFx开发RIA桌面应用-ComboBox组合编辑框水平增长》

《利用JavaFx开发RIA桌面应用-布局说明》

《利用JavaFx开发RIA桌面应用-在线资料》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: