Trufel a écrit :
> hello,
> i have jComboBox which should calculate its drop-down list size before
[quoted text clipped - 15 lines]
>
> From - Tue
If you intend to recalculate the size depending on the window size,
better use ComponentListener on the container of your combo.
public class ComboSizeTest extends JFrame {
private static final String[] data = { "First", "Second", "Third",
"Fourth", "Fifth", "Sixth" };
JComboBox combo = null;
public ComboSizeTest() {
super("Test combo dropdown list resizing");
combo = new JComboBox(data);
this.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
System.out.println("Resized");
combo.setMaximumRowCount(4);
}
});
this.setSize(100, 50);
this.getContentPane().add(combo, BorderLayout.CENTER);
}
public static void main(String[] args) {
new ComboSizeTest().setVisible(true);
}
}
NB: I added a setSize() method after adding the ComponentListener to
insure that combo size will be calculated before the frame is made visible.
Rui.
Trufel - 30 Aug 2006 10:38 GMT
Rui napisal(a):
> NB: I added a setSize() method after adding the ComponentListener to
> insure that combo size will be calculated before the frame is made visible.
Thanks! It works. I do it finally in this way:
contentPane.addComponentListener(new
java.awt.event.ComponentAdapter() {
public void componentResized(ComponentEvent e) {
contentPane_componentResized(e);
}
});
void contentPane_componentResized(ComponentEvent e) {
System.out.println("content resized");
int frameCurrentHeight = contentPane.getHeight();
double comboYPosition = myCombo.getBounds().getY();
int fontSize = (int)myCombo.getFont().getSize() + 6;
myCombo.setFixedCellHeight(fontSize);
int rowCount = (int)((frameCurrentHeight - comboYPosition) /
fontSize) - 5;
myCombo.setMaximumRowCount(rowCount);
}
thanks
T.