Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default renewing the content of a combo box

    I have a combo box at my toolbar and want to swap its content:
    I get the items in but the combo box does not appear well

    Here it gets installed:

    Java Code:
    //the font list combobox
                String[] s = {"Set Shortlist"};
                combo1 = new JComboBox(s);
    
                Font f = combo1.getFont();
                f = new Font(f.getFamily(), f.getStyle(), 12);
                combo1.setFont(f);
                JScrollPane sp = new JScrollPane(combo1);
                fontPanel1.add(sp);
                toolBar.add(fontPanel1);


    and here I'd like to change its content:


    Java Code:
    final ArrayList shortFontNameList = fontMenu.getShortFontNameList();
                        Object fontl[] = shortFontNameList.toArray();
                        toolbar.getComboRef().removeAll();
                        toolbar.getComboRef().setModel(new DefaultComboBoxModel(fontl));
                        
                        // next works equally bad
                        for (int i =0; i < fontl.length; i++) {
                              System.out.println("item: " + fontl[i]);
                             toolbar.getComboRef().addItem(fontl[i]);
                        }
    What is going wrong?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: renewing the content of a combo box

    Quote Originally Posted by willemjav View Post
    I have a combo box at my toolbar and want to swap its content:
    I get the items in but the combo box does not appear well



    Java Code:
                       [B] // next works equally bad[/B]
    What does that mean? Do you get exceptions? What happens? What do you expect?
    If you don't provide an SSCCE or a detailed description, we can only guess or ignore your question.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default Re: renewing the content of a combo box

    No the data is there, but I can not scroll it.
    No errors or exceptions.
    It is like the size is not ok?
    It does not pop down even!

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default Re: renewing the content of a combo box

    I think the concept is wrong.
    I need to remove the complete combo box and install a new one
    wrapped in a scroller?
    My reference is the JMenu.
    I removed a complete menu and built a new one (adding items)
    and put it at the please of the old menu.
    But combo boxes act differently!
    Last edited by willemjav; 01-31-2014 at 12:25 PM.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default Re: renewing the content of a combo box

    I have the combo box all most running.
    The problem I have occurs when invoking a font renderer.
    The renderer is added at line 15
    and the problem occurs at line 50
    Here is the error code:

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.Box$Filler cannot be cast to javax.swing.JScrollPane
    at willemwriter.WriterToolBar$ComboRenderer.getFontCo mbo(WriterToolBar.java:344)
    at willemwriter.WriterToolBar$ComboRenderer.getListCe llRendererComponent(WriterToolBar.java:362)
    at javax.swing.plaf.basic.BasicComboBoxUI.getDisplayS ize(BasicComboBoxUI.java:1334)
    at com.apple.laf.AquaComboBoxUI.getMinimumSize(AquaCo mboBoxUI.java:499)
    at javax.swing.JComponent.getMinimumSize(JComponent.j ava:1722)
    at java.awt.FlowLayout.minimumLayoutSize(FlowLayout.j ava:448)

    Next the method which changes the content of the combo
    (the renderer I copied from the net)

    Java Code:
    // adding the short font list as a combo box
            public void instalShortFontList()  {
                fontMenu.addPropertyChangeListener("SHORTLIST",
                                                new PropertyChangeListener() {
                    public void propertyChange(PropertyChangeEvent event) {
                        final ArrayList shortFontNameList =
                                            fontMenu.getShortFontNameList();
                        Object fontl[] = shortFontNameList.toArray();
                        combo1.removeAllItems();
                        for (int i =0; i < fontl.length; i++) {
                             combo1.addItem(fontl[i]);
                        }
    
                        combo1.setSelectedItem(0);
                        combo1.setRenderer(new ComboRenderer(combo1));
                        combo1.addItemListener(new ItemListener() {
                            @Override
                            public void itemStateChanged(ItemEvent e) {
                                if (e.getStateChange() == ItemEvent.SELECTED) {
                                    final String fontName =
                                        combo1.getSelectedItem().toString();
                                     combo1.setFont(new Font(fontName, Font.PLAIN, 16));
                                }
                            }
                        });
                        combo1.setSelectedItem(0);
                        combo1.getEditor().selectAll();
                    }
                });
            }
            // rendering the font family for the combo
            private class ComboRenderer extends BasicComboBoxRenderer {
    
                private JComboBox combobox;
                final DefaultListCellRenderer defaultRenderer =
                                                    new DefaultListCellRenderer();
                private int row;
    
                private ComboRenderer(JComboBox fontcombbox) {
                    combobox = fontcombbox;
                }
    
                private void getFontCombo() {
                    if (combobox.getItemCount() > 0) {
                        final Object comp =
                                combobox.getUI().getAccessibleChild(combobox, 0);
                        if ((comp instanceof JPopupMenu)) {
                            final JList list = new JList(combobox.getModel());
                            final JPopupMenu popup = (JPopupMenu) comp;
                            final JScrollPane scrollPane = (JScrollPane)
                                                            popup.getComponent(0);
                            final JViewport viewport = scrollPane.getViewport();
                            final Rectangle rect = popup.getVisibleRect();
                            final Point pt = viewport.getViewPosition();
                            row = list.locationToIndex(pt);
                        }
                    }
                }
    
                @Override
                public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected,
                                                        boolean cellHasFocus) {
    
                    super.getListCellRendererComponent(list, value,
                                            index, isSelected, cellHasFocus);
                    if (list.getModel().getSize() > 0) {
                        getFontCombo();
                    }
                    final JLabel renderer = (JLabel)
                        defaultRenderer.getListCellRendererComponent(list, value,
                                                    row, isSelected, cellHasFocus);
                    final Object fntObj = value;
                    final String fontFamilyName = (String) fntObj;
                    setFont(new Font(fontFamilyName, Font.PLAIN, 14));
                    return this;
                }
         }

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default Re: renewing the content of a combo box

    I finally use thus renderer which I understand and works just fine:

    Java Code:
     private class ComboRenderer extends BasicComboBoxRenderer {
                
                @Override
                public Component getListCellRendererComponent(JList list,
                        Object value, int index, boolean isSelected,
                                                    boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index,
                                                    isSelected, cellHasFocus);
                    final Object fntObj = value;
                    final String fontFamilyName = (String) fntObj;
                    setFont(new Font(fontFamilyName, Font.PLAIN, 16));
                    return this;
                }
            }

Similar Threads

  1. Replies: 1
    Last Post: 12-15-2013, 05:19 PM
  2. Renewing JBoss SSL Certificate
    By aparun222 in forum Advanced Java
    Replies: 0
    Last Post: 05-02-2012, 11:51 AM
  3. Combo with arbitrary content
    By BadaBingNow in forum SWT / JFace
    Replies: 0
    Last Post: 03-08-2012, 04:09 PM
  4. dynamically populate the city combo box based on the values of state combo +ajax+jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-29-2010, 10:00 AM
  5. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •