Who can resist a compilable example? - JComboBox

Discussion in 'Java' started by VisionSet, Sep 1, 2004.

  1. VisionSet

    VisionSet Guest

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


    --
    Mike W
     
    VisionSet, Sep 1, 2004
    #1
    1. Advertising

  2. Followup-To set.

    In comp.lang.java.gui VisionSet <> wrote:

    > 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
     
    Christian Kaufhold, Sep 1, 2004
    #2
    1. Advertising

  3. Me!
     
    Mike Schilling, Sep 2, 2004
    #3
  4. Andrew Thompson, Sep 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    Should this substitution be compilable?

    valentin tihomirov, Nov 28, 2004, in forum: VHDL
    Replies:
    12
    Views:
    804
    valentin tihomirov
    Nov 30, 2004
  2. Robert Bralic
    Replies:
    4
    Views:
    3,056
  3. John Benson
    Replies:
    0
    Views:
    325
    John Benson
    Jan 15, 2004
  4. Crypto Loko
    Replies:
    3
    Views:
    315
    Crypto Loko
    Oct 16, 2005
  5. bookaa bookaa
    Replies:
    12
    Views:
    130
    Steven D'Aprano
    May 27, 2014
Loading...

Share This Page