Who can resist a compilable example? - JComboBox

V

VisionSet

Sorry to blatently sensationalise my post, for once I'm in a hurry.

Here is a short compilable example that illustrates my problem.

To sumarise, I require the combobox to change its preferredsize as the
source for BasicComboBoxUI indicates it should when fireContentsChanged() is
called.
If the preferredsize changes then hopefully the JCB has half a chance of
responding visually.
I do not want to use DefaultComboBoxModel, my requirement is not simple row
addition like the example, the whole model may change at each update.
If anyone has any idea how to modify the below to fill this requirement,
please let me in on the secret!

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class ComboTest extends JFrame {

JComboBox combo = new JComboBox();
ComboModel model = new ComboModel();

public ComboTest() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

combo.setModel(model);

JPanel panel = new JPanel(new BorderLayout());

Box box = Box.createHorizontalBox();
combo.setMaximumSize(combo.getPreferredSize());
combo.setMinimumSize(combo.getPreferredSize());
box.add(combo);

panel.add(box, BorderLayout.NORTH);

JButton button = new JButton("Refresh");

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.changeModel();
System.out.println(combo.getPreferredSize());
}
});

panel.add(button, BorderLayout.EAST);

setContentPane(panel);

setBounds(50,50,400,100);

setVisible(true);
}

public static void main(String[] args) {
new ComboTest();
}

class ComboModel
extends AbstractListModel implements ComboBoxModel {

List list = new ArrayList();
String last = "X";
Object selected;// = last;

public Object getSelectedItem() {
return selected;
}

public void setSelectedItem(Object item) {
if((selected != null && ! selected.equals(item)) ||
(selected == null && item != null)) {
selected = item;
fireContentsChanged(this, -1, -1);
}
}

public int getSize() {
return list.size();
}

public Object getElementAt(int index) {
return list.get(index);
}

void changeModel() {
list.add(last);
last = last + "X";
fireContentsChanged(this, 0, list.size());
}
}
}
 
C

Christian Kaufhold

Followup-To set.

In comp.lang.java.gui VisionSet said:
To sumarise, I require the combobox to change its preferredsize as the
source for BasicComboBoxUI indicates it should when fireContentsChanged() is
called.

The source of BasicComboBoxUI does not indicate it.

BasicComboBoxUI.ListDataHandler.contentsChanged only set
"isMinimumSizeDirty", but not "isDisplaySizeDirty", therefore
the cached preferred size is used. This is a clear error.
The same problem occurs with the "font" property.


This fix seems to work, at some minor loss of efficiency.



class JComboBox2
extends JComboBox
{
private static ListCellRenderer fixRenderer
= new DefaultListCellRenderer();

public void contentsChanged(ListDataEvent e)
{
super.contentsChanged(e);

fixPreferredSize();
}

public void setFont(Font f)
{
super.setFont(f);

fixPreferredSize();
}

private void fixPreferredSize()
{
ListCellRenderer renderer = getRenderer();

setRenderer(fixRenderer);
setRenderer(renderer);
}
}


Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top