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