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

    Default rendering actions in a combo box

    When I add an array of action-objects (the class extends AbstractAction)
    to a combo box, I do receive the action performed events, great!
    But the combo box items show the long string of the action object, of course!

    How to render that string in a way that only the Action.NAME appears?

    I was experimenting a little, but could not make it to work:

    Java Code:
    private class ComboRenderer2 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;
                    Action sa = null;
                    sa = (Action) fntObj;
                    String s = (String) sa.getValue(Action.NAME);
                    // ??
                    return this;
                }
            }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: rendering actions in a combo box

    What is the component type of the renderer? Does it have a setText method? If not you can extend your own Component, like a JLabel and implement the ListCellRenderer. Then just assign the string to the JLabel.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: rendering actions in a combo box

    That is interesting jim I just added: this.setText(s);
    and the thing works!

    Java Code:
    private class ComboRenderer2 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;
                    Action sa = null;
                    sa = (Action) fntObj;
                    String s = (String) sa.getValue(Action.NAME);
                    this.setText(s);
                    return this;
                }
            }
    So what is going on here:

    ComboRenderer2 extends BasicComboBoxRenderer and it calls the list renderer (super.)
    of the BasicComboBoxRenderer!
    That one, on its turn, extends indeed JLabel which has a setText method, wow that is powerful stuff!

    Thanks Jim there was just that single code line missing!

    Any other observations?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: rendering actions in a combo box

    Quote Originally Posted by willemjav View Post
    Any other observations?
    Yep, you could also change the ComboBoxModel (see its API documentation); that way the Renderer can do what it was meant for: render the data instead of manipulating the data model.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: rendering actions in a combo box

    Great, thanks, I have actually the whole basic GUI setup (File, edit and font menu plus the two toolbars)
    managed by actions (several classes that extends AbstractAction* and one that implements ActionListener)!

    *most are Anonymous (AbstractAction) inner classes

    A real good one of myself (permit me being a little arrogant here) I think is,
    the method where I "filter out the font family actions form different sources):

    Java Code:
    class MenuActionListener implements ActionListener{
            public void actionPerformed(ActionEvent evt) {
                Object menusource = evt.getSource();
                String cmd = evt.getActionCommand();
                
                // here setup the font short list which appears on the menu and
                // and the second tool bar
                if (rbSetup.isSelected()){          
                    makeShortFontlist(evt);
                } else { // here: readout the font family selection
                    if (evt.getSource().getClass().getName().equals("javax.swing.JComboBox"))  {
                        JComboBox source = (JComboBox) evt.getSource();
                        if (source.getName() != null)
                        currentFont = source.getName();
                    } else {
                        currentFont = cmd;
                    }
                    changeSupport.firePropertyChange("FONTCHANGE",false,true); // this might be different here**
    
                    System.out.println("current font FAMILY (central): "
                                                + currentFont);
                } 
            }
         }
    ** since everything runs on actions I might not need the
    Java Code:
     public void addPropertyChangeListener(String property,
                                                    PropertyChangeListener p) {
            changeSupport2.addPropertyChangeListener(property, p);
        }
    I think I found the error of "changeSupport2" and I might change it back
    into "changeSupport"

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

    Default Re: rendering actions in a combo box

    I am pushing these actions around.....
    Is there a more "compact way" in dealing with these references?

    Java Code:
    public void setOpenAction(AbstractAction action) {
                openAction = action;
            }
    
            public void setNewAction(AbstractAction action) {
                newAction = action;
            }
    
            public void setSaveAction(AbstractAction action) {
                saveAction = action;
            }
    
    
            public void setColorAction(ColorAction[] action) { //working
                colorActions = action;
            }
    
            public void setStyleAction(StyleAction[] action) { //working
                styleActions = action;
            }
    
            public void setSizeAction (SizeAction[] action) {
                sizeActions = action;
            }

    Java Code:
     toolbar = new WriterToolBar(this, fileMenu, fontMenu );
    
            toolbar.setColorAction(fontMenu.getColorAction());
            toolbar.setStyleAction(fontMenu.getStyleAction());
            toolbar.setSizeAction(fontMenu.getSizeAction());
            toolbar.setOpenAction(fileMenu.getOpenAction());
            toolbar.setSaveAction(fileMenu.getSaveAction());

    and even more

    Java Code:
    public ColorAction[] getColorAction()  { 
             return fontMenu2.getColorAction();
         }
    
          public StyleAction[] getStyleAction() { 
             return fontMenu2.getStyleAction();
         }
    
         public SizeAction[] getSizeAction() {
             return fontMenu2.getSizeAction();
         }
    Last edited by willemjav; 02-02-2014 at 12:26 PM.

Similar Threads

  1. Replies: 1
    Last Post: 12-15-2013, 05:19 PM
  2. Why actions in url?
    By SomeoneHere in forum Struts
    Replies: 5
    Last Post: 01-10-2012, 05:07 AM
  3. 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
  4. How to Use Actions
    By AJArmstron@aol.com in forum New To Java
    Replies: 4
    Last Post: 04-18-2010, 09:03 PM
  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
  •