转载请注明来源-作者@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,