Invoke revalidate() on the panel containing the combobox. This will cause the components to be layed out again based on their preferred sizes.
Invoke revalidate() on the panel containing the combobox. This will cause the components to be layed out again based on their preferred sizes. This is the same concept as adding/removing a component on a visible GUI.
Edit: Just reread your question. I'm not sure if you can dynamically resize the popup when it is open but you can check out Combo Box Popup. It shows you how to override the preferred width of the popup.
This code is executed when the popup menu is about to be shown. But you may be able to use the concepts to access the popup and change the width dynamically. Edit 2: Here is an example that shows the basic concept.
The popup will adjust its width every 2 seconds. However, I don't know if this will help with your problem because if you are dynamicallyl adding/removing items from the popup, then you will need to recreate the popup every time the popup is changed. This will probably result in the popup hiding/showing which means you will need to live with a little flicker anyway.
Import java.awt. *; import java.awt.event. *; import javax.swing.
*; import javax.swing.plaf.basic. *; public class ComboBoxExample extends JPanel implements ActionListener { private JComboBox comboBox; public ComboBoxExample() { String petStrings = { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" }; comboBox = new JComboBox( petStrings ); add( comboBox, BorderLayout. PAGE_START ); Timer timer = new javax.swing.
Timer(2000, this); timer.start(); } public void actionPerformed(ActionEvent e) { comboBox.showPopup(); Object child = comboBox. GetAccessibleContext(). GetAccessibleChild(0); BasicComboPopup popup = (BasicComboPopup)child; JList list = popup.getList(); Container c = SwingUtilities.
GetAncestorOfClass(JScrollPane. Class, list); JScrollPane scrollPane = (JScrollPane)c; Dimension size = scrollPane.getSize(); if (size. Width > 20) size.
Width -= 5; scrollPane. SetPreferredSize(size); scrollPane. SetMaximumSize(size); Dimension popupSize = popup.getSize(); popupSize.
Width = size. Width; Component parent = popup.getParent(); parent. SetSize(popupSize); parent.validate(); parent.repaint(); Window mainFrame = SwingUtilities.
WindowForComponent(comboBox); Window popupWindow = SwingUtilities. WindowForComponent(popup); // For heavy weight popups you need to pack the window if (popupWindow! = mainFrame) popupWindow.pack(); } private static void createAndShowGUI() { JFrame frame = new JFrame( "ComboBoxExample" ); frame.
SetDefaultCloseOperation( JFrame. EXIT_ON_CLOSE ); JComponent newContentPane = new ComboBoxExample(); newContentPane. SetOpaque( true ); frame.
SetContentPane( newContentPane ); frame.pack(); frame. SetVisible(true); } public static void main(String args) { javax.swing.SwingUtilities. InvokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }.
I try to revalidate combobox and popup but nothing seem to work. I try also to resize the HEIGHT of my popup by setting preferredSize, but the new height is set after the popup is closed... – blow Feb 20 at 16:30 The example in your link provide a popup listener and work during the popupBecomeVisible events, I try to resize my popup WITHOUT make popup invisible and visible. – blow Feb 20 at 16:35 I mentioned in my answer that the code works when the popup is about to be displayed.
I was suggesting you maight be able to use the concepts presented there, since it gives you access to the components that make up the popup menu. That is get the popup, and get the scrollpane and resize the scrollpane and revalidate the popup. I don't know if it will work, but that is all I can suggest.
– camickr Feb 20 at 16:42 Ah ok, no it doesn't work, thank anyway, +1 vote for you. – blow Feb 20 at 16:58 @blow, see example. – camickr Feb 20 at 17:19.
Here is another example that changes the width as items are added: import java.awt. *; import java.awt.event. *; import javax.swing.
*; import javax.swing.plaf.basic. *; public class ComboBoxExample2 extends JPanel implements ActionListener { private JComboBox comboBox; public ComboBoxExample2() { String petStrings = { "A" }; comboBox = new JComboBox( petStrings ); comboBox. SetPrototypeDisplayValue("A1111111111"); add( comboBox, BorderLayout.
PAGE_START ); Timer timer = new javax.swing. Timer(2000, this); timer.start(); } public void actionPerformed(ActionEvent e) { String text = comboBox. GetItemAt( comboBox.getItemCount() - 1 ).toString(); comboBox.
AddItem( text + "1"); comboBox.showPopup(); Object child = comboBox. GetAccessibleContext(). GetAccessibleChild(0); BasicComboPopup popup = (BasicComboPopup)child; JList list = popup.getList(); Dimension preferred = list.
GetPreferredSize(); preferred. Width += 20; // allow for scrollbar int rowHeight = preferred. Height / comboBox.getItemCount(); int maxHeight = comboBox.
GetMaximumRowCount() * rowHeight; preferred. Height = Math. Min(preferred.
Height, maxHeight); Container c = SwingUtilities. GetAncestorOfClass(JScrollPane. Class, list); JScrollPane scrollPane = (JScrollPane)c; scrollPane.
SetPreferredSize(preferred); scrollPane. SetMaximumSize(preferred); Dimension popupSize = popup.getSize(); popupSize. Width = preferred.
Width; popupSize. Height = preferred. Height + 2; Component parent = popup.getParent(); parent.
SetSize(popupSize); parent.validate(); parent.repaint(); Window mainFrame = SwingUtilities. WindowForComponent(comboBox); Window popupWindow = SwingUtilities. WindowForComponent(popup); // For heavy weight popups you need to pack the window if (popupWindow!
= mainFrame) popupWindow.pack(); } private static void createAndShowGUI() { JFrame frame = new JFrame( "ComboBoxExample2" ); frame. SetDefaultCloseOperation( JFrame. EXIT_ON_CLOSE ); JComponent newContentPane = new ComboBoxExample2(); newContentPane.
SetOpaque( true ); frame. SetContentPane( newContentPane ); frame.pack(); frame. SetVisible(true); } public static void main(String args) { javax.swing.SwingUtilities.
InvokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.