Fix JComboBox Width

I have a JComboBox with some options. When I make a selection in another component, I change the contents of the JComboBox. First, I call the removeAllItems() method, and then I add in order the lines that I need now.

The problem is that by default I had some parameters, and one of them was larger, so the JComboBox got the necessary width to correctly display this option. When I change the contents of the JComboBox, this text option disappears, and now the smaller text gives the width of the JComboBox, so when I change the contents, it becomes smaller.

My first approach was to call myComboBox.setPreferredSize(myComboBox.getSize()) , and then its dimensions were fixed but not correct: it gets a little smaller in height and width. I think this is because I use Nimbus Look & Feel, and the sizes that I get from the getSize() method are the default Look% Feel defaults.

I also tried myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth())) with the same results.

How can I approach this problem?

I am adding sample code as I use 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 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]); } } } } 

Basically this is a GridBagLayout with 6 JPanel with a new GridBagLayout for each of them. I just wrote jPanel1 here to simplify things. Hope this is not very dirty.

+4
source share
3 answers

Finally, I found a simple solution:

 jComboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXX"); 

And it works perfectly for what I wanted

+12
source

In the moment prior to adding the JComboBox to jpanel1, try setting the fill attribute from GridBagConstraints to NONE (not BOTH ). This should (hopefully) prevent sockets from being resized.

 c.fill = GridBagConstraints.NONE; jPanel1.add(combo1, c); 
0
source

NetBeans does this for you with a pair of checkboxes in the context menu of the contextual context: Auto Resize → Horizontal and Vertical.

I disabled them and compared the generated NetBeans code in my version control history. There was an unexpected amount of changes that I could not relate to the size of the component, but I saw changes in the way the combo box containing the layout group was added to it. essentially

 ....addComponent(destinationFolderComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 

has been changed to

 ....addComponent(myComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE) 

JComboBox.setPreferredSize() never explicitly named, so I assume that you will get better results from telling the layout manager about preferred sizes and resizing properties rather than telling JComboBox and hoping the layout manager will read these properties behind the scenes.

(by the way, by ....addComponent() , I mean addComponent() is called in a long cascading sequence createParallelGroup() and createSequentialGroup() , starting with a new javax.swing.GroupLayout(myPanel) . I presented the short form above because I didn’t want to go into the insignificance of the automatically generated NetBeans code that I couldn’t start writing manually or explaining. Do you want all this? You cannot handle all this, here it is:

  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()) ); 

)

0
source

All Articles