Weird Behavior: Sizing Problem with JComboBox in a JTable

Hi

I'm using JComboBox as a cell in a JTable. But the problem is when the length of the popup menu in the combobox exceeds that of the frame and the selection starts behaving erratically. I suspect this might be due to a reported bug # 4372285.

So now I'm using a stepped combobox as suggested in one the posting in this forum:

http://forum.java.sun.com/thread.jsp?forum=57&thread=121540 and I'm able to specify the width of the popupmenu of the combo as I want.

But I can't make this width hard coded, I mean it doesn't make sense to make the width of the popup less than the cell width of its container cell, nor do I want to exceed its length. I'm OK with the data truncation in the popup but I would like to make the width of the popup exactly the same as the cell width.

Is there a way I can get get this width to specify it for teh pop-up width? Any other way to do this?

Pls halp asap.

Thanks

Abhi

I'm attaching the code of stepped combo-box in case you don't wanna visit the link provioded:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.plaf.metal.*;

import javax.swing.plaf.basic.*;

import javax.swing.border.*;

public class SteppedComboBoxExample extends JFrame {

public SteppedComboBoxExample() {

super("SteppedComboBox Example");

String[] str = {

"A",

"abcdefghijklmnopqrstuvwxyz",

"ABCDEFGHIJKLMNOPQRSTUVWXYZ",

"0123456789"

};

SteppedComboBox combo = new SteppedComboBox(str);

Dimension d = combo.getPreferredSize();

combo.setPreferredSize(new Dimension(50, d.height));

combo.setPopupWidth(d.width);

getContentPane().setLayout(new FlowLayout());

getContentPane().add(combo);

}

class SteppedComboBoxUI extends MetalComboBoxUI {

protected ComboPopup createPopup() {

BasicComboPopup popup = new BasicComboPopup( comboBox ) {

public void show() {

Dimension popupSize = ((SteppedComboBox)

comboBox).getPopupSize();

popupSize.setSize( popupSize.width,

getPopupHeightForRowCount( comboBox.getMaximumRowCount

() ) );

Rectangle popupBounds = computePopupBounds( 0,

comboBox.getBounds().height, popupSize.width,

popupSize.height);

scroller.setMaximumSize( popupBounds.getSize() );

scroller.setPreferredSize( popupBounds.getSize() );

scroller.setMinimumSize( popupBounds.getSize() );

list.invalidate();

int selectedIndex = comboBox.getSelectedIndex();

if ( selectedIndex == -1 ) {

list.clearSelection();

} else {

list.setSelectedIndex( selectedIndex );

}

list.ensureIndexIsVisible( list.getSelectedIndex() );

setLightWeightPopupEnabled(

comboBox.isLightWeightPopupEnabled() );

show( comboBox, popupBounds.x, popupBounds.y );

}

};

popup.getAccessibleContext().setAccessibleParent(comboBox);

return popup;

}

}

public class SteppedComboBox extends JComboBox {

protected int popupWidth;

public SteppedComboBox(ComboBoxModel aModel) {

super(aModel);

setUI(new SteppedComboBoxUI());

popupWidth = 0;

}

public SteppedComboBox(final Object[] items) {

super(items);

setUI(new SteppedComboBoxUI());

popupWidth = 0;

}

public SteppedComboBox(Vector items) {

super(items);

setUI(new SteppedComboBoxUI());

popupWidth = 0;

}

public void setPopupWidth(int width) {

popupWidth = width;

}

public Dimension getPopupSize() {

Dimension size = getSize();

if (popupWidth < 1) popupWidth = size.width;

return new Dimension(popupWidth, size.height);

}

}

public static void main (String args[]) {

SteppedComboBoxExample f = new SteppedComboBoxExample();

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

f.setSize (300, 100);

f.show();

}

}

[4179 byte] By [abhi_pathak] at [2007-9-26 16:48:17]
# 1

Add a method to your custom combo box / l&f that allows you to set the minimum size for the created popup menu.

Now, rather than using the default cell editor with a combo box, create your own cell editor. When the editor is called (see the TableCellEditor interface) get the column width from the JTable and use that to set the min width of your combo box prior to returning it in the interface method.

gweedo at 2007-7-2 20:44:12 > top of Java-index,Archived Forums,Swing...
# 2
Thanks for posting the stepped combo box code.It was really useful to me.Zeke
zekke at 2007-7-2 20:44:12 > top of Java-index,Archived Forums,Swing...