Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Who can resist a compilable example? - JComboBox

Reply
Thread Tools

Who can resist a compilable example? - JComboBox

 
 
VisionSet
Guest
Posts: n/a
 
      09-01-2004
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


 
Reply With Quote
 
 
 
 
Christian Kaufhold
Guest
Posts: n/a
 
      09-01-2004

Followup-To set.

In comp.lang.java.gui VisionSet <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      09-02-2004
Me!


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-02-2004
On Thu, 02 Sep 2004 05:14:52 GMT, Mike Schilling wrote:

> Me!


...Are you working your way up to, 'I! Me! Mine!'?

You're almost there, just two words to go.
Come on, ..you can do it.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MSDOS-Based Compilable Code for RSA / Other Public-Key Scheme Crypto Loko C Programming 3 10-16-2005 01:18 PM
Should this substitution be compilable? valentin tihomirov VHDL 12 11-30-2004 03:44 PM
Any small program in c++ compilable with gnu gpp compiler Robert Bralic C++ 4 07-19-2004 10:13 PM
Ho's can't resist me Mr Nifty Digital Photography 5 03-03-2004 03:18 PM
off-topic, but I can't resist the flamebait: best minicomputer? John Benson Python 0 01-15-2004 10:43 PM



Advertisments