J'ai un problème avec JComboBoxes et longues chaînes. Le résultat est que le JComboBox est trop long pour le JFrame afin qu'il n'affiche pas la boîte entière. Ici, j'ai un petit code qui montre mon problème:JComboBox trop long pour JFrame
JFrame frame = new JFrame();
frame.setSize(500, 500);
JPanel panel = new JPanel();
DesignGridLayout layout = new DesignGridLayout(panel);
Vector<ArrayList<String>> content = new Vector<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
content.add(list);
ComboBoxFullMenu<ArrayList<String>> combobox = new ComboBoxFullMenu<ArrayList<String>>(content);
combobox.setRenderer(new DefaultListCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
ArrayList<String> array = (ArrayList<String>) value;
if (!array.isEmpty())
{
String text = "";
for (String info : array)
{
text += info + "; ";
}
setText(text);
}
return this;
}
});
AutoCompleteDecorator.decorate(combobox, new ObjectToStringConverter()
{
@Override
public String getPreferredStringForItem(Object object)
{
if (object != null)
{
if (object instanceof ArrayList)
{
ArrayList<String> list = (ArrayList<String>) object;
String text = "";
for (String info : list)
{
text += info + "; ";
}
return text;
}
else
{
return object.toString();
}
}
return null;
}
});
layout.row().grid().add(combobox);
frame.add(panel);
frame.setVisible(true);
Et voici la ComboBoxFullMenu de classe:
public class ComboBoxFullMenu<E> extends JComboBox<E>
{
/**
*
*/
private static final long serialVersionUID = 1L;
public ComboBoxFullMenu(Vector<E> items)
{
super(items);
addActionListener(this);
}
public ComboBoxFullMenu()
{
super();
addActionListener(this);
}
public ComboBoxFullMenu (DefaultComboBoxModel<E> defaultComboBoxModel)
{
super(defaultComboBoxModel);
addActionListener(this);
}
/**
* Small hack to get pop up menu size bigger enough to show items even though
* the combo box size could be smaller
* */
private boolean layingOut = false;
@Override
public void doLayout()
{
try
{
layingOut = true;
super.doLayout();
}
finally
{
layingOut = false;
}
}
@Override
public Dimension getSize()
{
Dimension dim = super.getSize();
if (!layingOut)
{
dim.width = Math.max(dim.width, getPreferredSize().width);
}
return dim;
}
}
J'ai aussi essayé d'utiliser DefaultListCellRenderer et ObjectTroStringConverter pour afficher l'élément sélectionné d'une manière plus courte, mais j'ai besoin de voir toutes les informations dans le menu déroulant et je pense que le JComboBox calcule sa largeur sur le plus long article? J'espère que vous comprenez mon problème sinon faites le moi savoir.
Merci pour votre réponse mais j'utilise déjà WideComboBox. C'est le même code que vous pouvez voir dans ma classe ComboBoxFullMenu. J'ai un JTable où cela fonctionne et je ne comprends pas pourquoi il ne fonctionne pas dans cet exemple simple. – mavok
Essayez de définir la taille préférée de la zone de liste déroulante – Jayfray
'combobox.setPreferredSize (new Dimension (20, 20));' ne change rien – mavok