我正在尝试使用OTN上的线程Horizontal scrollbar for JComboBox across multiple look and feel使用引用在JComboBox
中添加水平滚动条,但它在我的情况下不起作用。
如何正确添加水平滚动条到JComboBox
?
代码 -
public class TestJComboBoxWithScrollBar {
TestJComboBoxWithScrollBar() {
JDialog jDialog = new JDialog();
jDialog.setTitle("Test JComboBox With ScrollBar");
JPanel jPanel_Sort = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints bagConstraints = new GridBagConstraints();
jPanel_Sort.setLayout(gbl);
bagConstraints.gridwidth = 1;
bagConstraints.gridheight = 1;
bagConstraints.fill = GridBagConstraints.NONE;
bagConstraints.anchor = GridBagConstraints.WEST;
bagConstraints.weightx = 0;
bagConstraints.weighty = 0;
bagConstraints.insets = new Insets(5, 5, 5, 5);
bagConstraints.gridx = 0;
bagConstraints.gridy = 0;
SampleJComboBoxWithScrollBar cmbHeaders = new SampleJComboBoxWithScrollBar();
cmbHeaders.addItem("aaaaaaaaaaaaa");
cmbHeaders.addItem("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
cmbHeaders.setPreferredSize(new Dimension(190, 50));
cmbHeaders.setMinimumSize(new Dimension(190, 50));
cmbHeaders.setMaximumSize(new Dimension(190, 50));
jPanel_Sort.add(cmbHeaders, bagConstraints);
JCheckBox chkOrderBy = new JCheckBox("Asc");
bagConstraints.gridx = 1;
bagConstraints.gridy = 0;
jPanel_Sort.add(chkOrderBy, bagConstraints);
jPanel_Sort.setPreferredSize(new Dimension(220, 70));
jPanel_Sort.setMinimumSize(new Dimension(220, 70));
jPanel_Sort.setMaximumSize(new Dimension(220, 70));
jDialog.add(jPanel_Sort, BorderLayout.CENTER);
jDialog.setPreferredSize(new Dimension(300, 100));
jDialog.pack();
jDialog.setResizable(false);
jDialog.setModal(true);
jDialog.setVisible(true);
}
public static void main(String[] argu) {
new TestJComboBoxWithScrollBar();
}
class SampleJComboBoxWithScrollBar extends JComboBox {
SampleJComboBoxWithScrollBar() {
super();
this.addPopupMenuListener(this.getPopupMenuListener());
this.adjustScrollBar();
}
private void adjustPopupWidth() {
if (getItemCount() == 0) {
return;
}
Object comp = getUI().getAccessibleChild(this, 0);
if (!(comp instanceof JPopupMenu)) {
return;
}
JPopupMenu popup = (JPopupMenu) comp;
JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
Object value = getItemAt(0);
Component rendererComp = getRenderer().getListCellRendererComponent(new JList(), value, 0, false, false);
if (rendererComp instanceof JXTable) {
scrollPane.setColumnHeaderView(((JTable) rendererComp).getTableHeader());
}
Dimension prefSize = rendererComp.getPreferredSize();
Dimension size = scrollPane.getPreferredSize();
size.width = Math.max(size.width, prefSize.width);
scrollPane.setPreferredSize(size);
scrollPane.setMaximumSize(size);
scrollPane.revalidate();
}
private void adjustScrollBar() {
if (getItemCount() == 0) {
return;
}
Object comp = getUI().getAccessibleChild(this, 0);
if (!(comp instanceof JPopupMenu)) {
return;
}
JPopupMenu popup = (JPopupMenu) comp;
JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
scrollPane.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
private PopupMenuListener getPopupMenuListener() {
return new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
adjustPopupWidth();
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
};
}
}
}
答案 0 :(得分:3)
如果您的意思是@Kleopatra
发布的代码,那么adjustScrollBar()
PopupMenuListener
private PopupMenuListener getPopupMenuListener() {
return new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
//adjustPopupWidth();
adjustScrollBar();
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
};
}