java 修复 JComboBox 宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7818155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fix JComboBox width
提问by Roman Rdgz
I have a JComboBox with some options. When I make a selection at another component, I'm changing JComboBox contents. First I call the removeAllItems()
method, and then I add one by one the Strings I need now.
我有一个带有一些选项的 JComboBox。当我在另一个组件上进行选择时,我正在更改 JComboBox 的内容。首先我调用removeAllItems()
方法,然后我将我现在需要的字符串一个一个添加。
The problem is, by default I had some options, and one of them was the larger text, so JComboBox got the width it needed to show that option correctly. When I change the JComboBox contents, that text option is gone, and now a smaller text is giving the width to the JComboBox, so when I change contents it gets smaller.
问题是,默认情况下我有一些选项,其中之一是较大的文本,因此 JComboBox 获得了正确显示该选项所需的宽度。当我更改 JComboBox 内容时,该文本选项消失了,现在较小的文本为 JComboBox 提供了宽度,因此当我更改内容时,它会变小。
My first approach was calling myComboBox.setPreferredSize(myComboBox.getSize())
and then its dimensions are fixed, but not correctly: it gets a bit smaller in height and width. I think it is because I'm using Nimbus Look&Feel, and the dimensions I'm getting from the getSize()
method are the ones given by the Java default Look%Feel.
我的第一种方法是调用myComboBox.setPreferredSize(myComboBox.getSize())
,然后它的尺寸是固定的,但不正确:它的高度和宽度变小了一点。我认为这是因为我使用的是 Nimbus Look&Feel,并且我从该getSize()
方法中获得的尺寸是Java 默认的 Look%Feel 给出的尺寸。
I also tried myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth()))
with same results.
我也尝试myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth()))
过相同的结果。
How can I approach this problem?
我该如何解决这个问题?
I add a code example of how I'm using the Layout:
我添加了一个关于如何使用布局的代码示例:
private String[] comboEventDOutputStrings = { "Run", "Stop", "Pause", "Conditioning time", "Deposition time", "Equilibration time", "Measurement time"};
private String[] comboEventDInputStrings = { "Run", "Stop", "Pause"};
// The first String array is the default set of values. It's obvious that the JComboBox will get smaller
// when I change to the second array of contents
//...
JPanel pane = new JPanel(new GridBagLayout());
JPanel jPanelExterno = new JPanel(new GridBagLayout());
GridBagConstraints cExterna = new GridBagConstraints();
GridBagConstraints c = new GridBagConstraints();
Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
jPanelExterno.setBorder(loweredetched);
jPanelExterno.setName("");
cExterna.fill = GridBagConstraints.BOTH;
cExterna.anchor = GridBagConstraints.NORTH;
cExterna.gridx = 0;
cExterna.gridy = 0;
cExterna.insets = new Insets(10,10,5,5);
JPanel jPanel1 = new JPanel(new GridBagLayout());
jPanel1.setBorder(loweredetched);
jPanel1.setName("PIO 1");
jCheckBox1 = new JCheckBox("Enable");
jCheckBox1.setSelected(false);
jCheckBox1.setName("1");
jCheckBox1.addActionListener(new PIOCheckListener());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,5,10,10);
jPanel1.add(jCheckBox1, c);
c.gridy++;
c.insets = new Insets(5,10,5,5);
JLabel label1 = new JLabel("IO Type");
jPanel1.add(label1, c);
c.gridx++;
c.insets = new Insets(5,5,5,10);
combo1 = new JComboBox(comboIOTypeStrings);
combo1.setEnabled(false);
combo1.setSelectedIndex(0);
combo1.setName("1");
combo1.addActionListener (new PIOComboListener());
jPanel1.add(combo1, c);
c.gridy++;
c.insets = new Insets(5,10,5,5);
c.gridx=0;
JLabel label2 = new JLabel("Active level");
jPanel1.add(label2, c);
c.gridx++;
c.insets = new Insets(5,5,5,10);
combo2 = new JComboBox(comboActiveLevelStrings);
combo2.setEnabled(false);
combo2.setSelectedIndex(0);
jPanel1.add(combo2, c);
c.gridy++;
c.insets = new Insets(5,10,5,5);
c.gridx=0;
JLabel label3 = new JLabel("Event");
jPanel1.add(label3, c);
c.gridx++;
c.insets = new Insets(5,5,10,10);
combo3 = new JComboBox(comboEventDOutputStrings);
combo3.setEnabled(false);
combo3.setSelectedIndex(0);
jPanel1.add(combo3, c);
c.gridy++;
c.insets = new Insets(5,10,5,5);
c.gridx=0;
JLabel label4 = new JLabel("Node");
jPanel1.add(label4, c);
c.gridx++;
c.insets = new Insets(5,5,10,10);
combo4 = new JComboBox(comboNodeStrings);
combo4.setEnabled(false);
combo4.setSelectedIndex(0);
jPanel1.add(combo4, c);
jPanelExterno.add(jPanel1, cExterna);
pioDialog.add(pane);
pioDialog.pack();
pioDialog.setLocationRelativeTo(null);
pioDialog.setVisible(true);
//...
}
class PIOComboListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent a) {
JComboBox cb = (JComboBox)a.getSource();
JComboBox target1 = null;
JComboBox target2 = null;
JComboBox target3 = null;
switch(Integer.parseInt(cb.getName())){
case 1:
target1 = combo2;
target2 = combo3;
target3 = combo4;
break;
default:
Register.debug("PIODialog error: No target for comboBoxes");
break;
}
if(cb.getSelectedIndex()==2){ //Analog input
target1.setEnabled(false);
target2.setEnabled(false);
target3.setEnabled(false);
}
else{
target1.setEnabled(true);
target2.setEnabled(true);
target3.setEnabled(true);
target2.removeAllItems();
if(cb.getSelectedIndex()==0){
for(int i=0; i<comboEventDOutputStrings.length; i++)
target2.addItem(comboEventDOutputStrings[i]);
} else {
for(int i=0; i<comboEventDInputStrings.length; i++)
target2.addItem(comboEventDInputStrings[i]);
}
}
}
}
It's basically a GridBagLayout with 6 JPanel with a new GridBagLayout for each one of them. I just wrote jPanel1 here to simplify things. I hope it is not very messy.
它基本上是一个带有 6 个 JPanel 的 GridBagLayout,其中每个都有一个新的 GridBagLayout。我只是在这里写了 jPanel1 来简化事情。我希望它不是很乱。
回答by Roman Rdgz
Finally I found an easy solution:
最后我找到了一个简单的解决方案:
jComboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXX");
And works perfect for what I wanted
并且非常适合我想要的
回答by PMorganCA
NetBeans does this for you with a couple of checkboxes in the component's right-click context menu: Auto Resizing -> Horizontal and Vertical.
NetBeans 通过组件右键单击上下文菜单中的几个复选框为您完成此操作:自动调整大小 -> 水平和垂直。
I turned them off and compared the NetBeans generated code in my revision control history. There were a surprising number of changes that I could not relate to the size of the component but I did see changes in how the combo box was added to it's containing layout group. essentially,
我关闭了它们并在我的修订控制历史记录中比较了 NetBeans 生成的代码。有数量惊人的更改与组件的大小无关,但我确实看到组合框添加到其包含布局组的方式发生了变化。本质上,
....addComponent(destinationFolderComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
was changed to
改为
....addComponent(myComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)
JComboBox.setPreferredSize()
is never explicitly called, so I assume you get better results from telling the layout manager the preferred size and re-size properties, rather than telling the JComboBox and hoping the layout manager reads those properties behind the scenes.
JComboBox.setPreferredSize()
从来没有被明确调用,所以我假设你通过告诉布局管理器首选大小和重新调整大小属性会得到更好的结果,而不是告诉 JComboBox 并希望布局管理器在幕后读取这些属性。
(by the way, by ....addComponent()
, I mean addComponent()
is called in a long, cascaded sequence of createParallelGroup()
and createSequentialGroup()
, starting with a new javax.swing.GroupLayout(myPanel)
. I presented the short form above because I didn't want to get into the nitty gritty of auto-generated NetBeans code, which I could not begin to write manually or explain. You want the whole thing? You can't handle the whole thing! here it is:
(顺便说一句,by ....addComponent()
,我的意思addComponent()
是在一个长的、级联的createParallelGroup()
and序列中调用,以 acreateSequentialGroup()
开头new javax.swing.GroupLayout(myPanel)
。我提供了上面的简短形式,因为我不想深入了解自动生成的 NetBeans 代码的本质,其中我无法开始手动编写或解释。你想要整个事情吗?你不能处理整个事情!这里是:
javax.swing.GroupLayout taskPanelLayout = new javax.swing.GroupLayout(taskPanel);
taskPanel.setLayout(taskPanelLayout);
taskPanelLayout.setHorizontalGroup(
taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(taskPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, taskPanelLayout.createSequentialGroup()
.addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(taskPanelLayout.createSequentialGroup()
.addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(tagModifierLabel, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
.addGap(18, 18, 18)
.addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(destinationFolderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tagModifierTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(18, 18, 18)
.addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(importFilesButton)
.addComponent(selectFilesButton)
.addComponent(clearFilesListButton)
.addComponent(commitChangesButton)))
.addGroup(taskPanelLayout.createSequentialGroup()
.addComponent(importFilesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
)
)
回答by Adamski
At the point prior to adding the JComboBox
to jpanel1, try setting the fill
attribute of the GridBagConstraints
to NONE
(rather than BOTH
). This should (hopefully) prevent the combo-box from resizing.
在将 the 添加JComboBox
到 jpanel1 之前,尝试设置to (而不是)的fill
属性。这应该(希望)防止组合框调整大小。GridBagConstraints
NONE
BOTH
c.fill = GridBagConstraints.NONE;
jPanel1.add(combo1, c);