如果我在可见时添加项目,则不会调整JComboBox弹出窗口的大小

我有一个JComboBox弹出窗口的问题。 我的JComboBox有一个自动完成实现,如谷歌搜索框。

所以,问题是如果我在弹出窗口可见时添加或删除项目它没有resize,我需要关闭并重新打开它。 但是这个火爆的弹出成为隐形和弹出的结果,因此我无法将这个事件用于我真正的porpouse。

有一种方法可以根据其中包含的项目数量“刷新”弹出窗口大小,而不关闭并重新打开它?

谢谢。

在包含combobox的面板上调用revalidate()。 这将导致组件根据其首选尺寸再次布置。

这与在可见GUI上添加/删除组件的概念相同。

编辑:

只是重读你的问题。 我不确定你是否可以在弹出窗口时动态调整弹出窗口,但你可以查看combobox弹出窗口。 它向您展示了如何覆盖弹出窗口的首选宽度。 当即将显示弹出菜单时执行此代码。 但您可以使用这些概念来访问弹出窗口并动态更改宽度。

编辑2:

这是一个显示基本概念的示例。 弹出窗口将每2秒调整一次宽度。 但是,我不知道这是否有助于解决您的问题,因为如果您动态添加/删除弹出窗口中的项目,则每次更改弹出窗口时都需要重新创建弹出窗口。 这可能会导致弹出窗口隐藏/显示,这意味着无论如何你都需要有一点点闪烁。

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

这是另一个在添加项目时更改宽度的示例:

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