CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2004
    Posts
    5

    Question The width of the JComboBox drop-down list

    I want to have same prefferredSize() for all JComboBox i use. So I made an subclass to JComboBox and wrote setPrefferredsize( ... ). But i want to know how i can change the width of the drop-down list, that shows JComboBox items (wich is String-objects) to be wide as a widest String item. Because now, this drop-down list is wide as JComboBox.

    Please if u know the answer, write details. Beacause I am not so very good in english. If u write an example it will help very much.

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: The width of the JComboBox drop-down list

    Originally posted by nonick
    I want to have same prefferredSize() for all JComboBox i use. So I made an subclass to JComboBox and wrote setPrefferredsize( ... ). But i want to know how i can change the width of the drop-down list, that shows JComboBox items (wich is String-objects) to be wide as a widest String item. Because now, this drop-down list is wide as JComboBox.

    Please if u know the answer, write details. Beacause I am not so very good in english. If u write an example it will help very much.
    I thought setPreferredSize() was a layout manager related thing.. are you sure that that is what is affecting the popup menu?
    you have subclassed your JCBox.. have you got access to the menu?
    Someone else was recently asking about moving the popup.. see if any advice has appeared in that thread.. or maybe you would have something to contribute there, and achieve a collaborated solution..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Re: The width of the JComboBox drop-down list

    Originally posted by cjard
    I thought setPreferredSize() was a layout manager related thing.. are you sure that that is what is affecting the popup menu?
    you have subclassed your JCBox.. have you got access to the menu?
    Someone else was recently asking about moving the popup.. see if any advice has appeared in that thread.. or maybe you would have something to contribute there, and achieve a collaborated solution..
    additionally, you should consider if a combobox is the best component to use in this particular situation.. if it is being used to hold data that is wider than its display.. should it be abbreviated (the data) or swapped for another component entirely?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Apr 2004
    Posts
    5

    Re: Re: The width of the JComboBox drop-down list

    Originally posted by cjard
    I thought setPreferredSize() was a layout manager related thing.. are you sure that that is what is affecting the popup menu?
    you have subclassed your JCBox.. have you got access to the menu?
    Someone else was recently asking about moving the popup.. see if any advice has appeared in that thread.. or maybe you would have something to contribute there, and achieve a collaborated solution..
    Oh, I'm sorry! I maybe didn't explained so good! With setPrefferredSize() i only controlled the width of the JComboBox, not the drop-down list! My question is how i can set width to the drop-down list too!

  5. #5
    Join Date
    Apr 2004
    Posts
    5

    Re: Re: Re: The width of the JComboBox drop-down list

    Originally posted by cjard
    additionally, you should consider if a combobox is the best component to use in this particular situation.. if it is being used to hold data that is wider than its display.. should it be abbreviated (the data) or swapped for another component entirely?
    Ok! Thanks for the tips! But I still want to use Combobox..and i know that it is in some way possible to set width of the popup list!

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Re: Re: The width of the JComboBox drop-down list

    Originally posted by nonick
    Oh, I'm sorry! I maybe didn't explained so good! With setPrefferredSize() i only controlled the width of the JComboBox, not the drop-down list! My question is how i can set width to the drop-down list too!
    JComboBox can have a PopupMenuListener added to it with the addPopupMenuListener() method

    write your own PopupMenuListener with reference to:
    http://java.sun.com/j2se/1.4.2/docs/...uListener.html

    you can see that the listener will fire an event when the popup is about to become visible. the PopupMenuEvent (with reference to: http://java.sun.com/j2se/1.4.2/docs/...MenuEvent.html) has just one purpose; to confer the source of the event.

    so, once you use the provided PopupMenuEvent to determine the source JPopupMenu that is about to show.. you can have a look at:
    http://java.sun.com/j2se/1.4.2/docs/...PopupMenu.html

    and see that a JPopupMenu has a method "setSize"

    can you see where to go from there?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Apr 2004
    Posts
    5

    Re: Re: Re: Re: The width of the JComboBox drop-down list

    Originally posted by cjard
    JComboBox can have a PopupMenuListener added to it with the addPopupMenuListener() method

    write your own PopupMenuListener with reference to:
    http://java.sun.com/j2se/1.4.2/docs/...uListener.html

    you can see that the listener will fire an event when the popup is about to become visible. the PopupMenuEvent (with reference to: http://java.sun.com/j2se/1.4.2/docs/...MenuEvent.html) has just one purpose; to confer the source of the event.

    so, once you use the provided PopupMenuEvent to determine the source JPopupMenu that is about to show.. you can have a look at:
    http://java.sun.com/j2se/1.4.2/docs/...PopupMenu.html

    and see that a JPopupMenu has a method "setSize"

    can you see where to go from there?
    Ok! But how do i get JPopupMenu for this combobox, from the PopupMenuevent?

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Re: Re: Re: Re: The width of the JComboBox drop-down list

    Originally posted by nonick
    Ok! But how do i get JPopupMenu for this combobox, from the PopupMenuevent?
    The 'event.getSource()' method will return the Object that caused the event (in this case the JPopupMenu). Cast the return value to JPopupMenu and there you have it.

    Many casts will be a things of the past - in Java 1.5...
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Apr 2004
    Posts
    5

    Re: Re: Re: Re: Re: Re: The width of the JComboBox drop-down list

    Originally posted by dlorde
    The 'event.getSource()' method will return the Object that caused the event (in this case the JPopupMenu). Cast the return value to JPopupMenu and there you have it.

    Many casts will be a things of the past - in Java 1.5...
    ok! I'll paste the codes here:


    private class Popuplistener implements PopupMenuListener
    {

    public void popupMenuCanceled( PopupMenuEvent e )
    {

    }

    public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
    {

    }

    public void popupMenuWillBecomeVisible(PopupMenuEvent e)
    {

    }
    } // end private class

    I made a private class which is the PopupMenuListener. I have to redefine 3 methods, and as parameter i have PopupMenuEvent e . If I write e.getSource() i will just get the JComboBox as return. Because i added this popupListener to combobox (combobox.addPopupMenuListener( new Popuplistener() ); ). So I dont get PopupMenu as You wrote! I'm I doing this wrong or what?

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    I haven't tried any of this stuff, I just looked at the Java docs for PopupMenuEvent, which say:

    'PopupMenuEvent only contains the source of the event which is the JPoupMenu sending the event'.

    But it looks like in this situation the combo box is returned instead of the popup menu - very annoying... another nasty Swing gotcha. It appears that cjard's popup listener suggestion is a dead end - unless he knows how to get the popup menu from this particular popup menu event ?

    As soon as we started programming, we found out to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs...
    M. Wilkes
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    those toads! If there was enough room to Swing a cat round this API, i'd beat 'em to death with it! I'm looking for the sourcecode to j2se, see can i find out if those baskets wrote something like:

    setSource(this);

    instead of

    setSource(popup);

    when generating the event. If they did, the resolution would be to subclass JCombobox for that one particular thing.. I'll post back if i find something
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  12. #12
    Join Date
    Jun 2012
    Posts
    1

    Re: The width of the JComboBox drop-down list

    Quote Originally Posted by dlorde View Post
    I haven't tried any of this stuff, I just looked at the Java docs for PopupMenuEvent, which say:

    'PopupMenuEvent only contains the source of the event which is the JPoupMenu sending the event'.

    But it looks like in this situation the combo box is returned instead of the popup menu - very annoying... another nasty Swing gotcha. It appears that cjard's popup listener suggestion is a dead end - unless he knows how to get the popup menu from this particular popup menu event ?

    As soon as we started programming, we found out to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs...
    M. Wilkes
    source code i found that is able to access popup menu.

    package com.easynth.designer.laf.popup;

    import java.awt.Dimension;

    import javax.swing.BorderFactory;
    import javax.swing.JComboBox;
    import javax.swing.JList;
    import javax.swing.JPopupMenu;
    import javax.swing.JScrollPane;
    import javax.swing.event.PopupMenuEvent;
    import javax.swing.event.PopupMenuListener;
    import javax.swing.plaf.basic.ComboPopup;

    public class EaSynthComboBoxPopupMenuListener implements PopupMenuListener {
    // ==============================================================================
    // Members
    // ==============================================================================
    private int bgTop = 4;
    private int bgLeft = 4;
    private int bgRight = 9;
    private int bgBottom = 10;

    // ==============================================================================
    // Constructors
    // ==============================================================================
    public EaSynthComboBoxPopupMenuListener() {
    super();
    }

    // ==============================================================================
    // Methods
    // ==============================================================================
    public void popupMenuCanceled(PopupMenuEvent e) {

    }

    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {

    }

    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    final JComboBox box = (JComboBox) e.getSource();
    final Object comp = box.getUI().getAccessibleChild(box, 0);
    if (!(comp instanceof JPopupMenu)) {
    return;
    }
    final JPopupMenu popupMenu = (JPopupMenu)comp;
    popupMenu.setBorder(null);
    if (popupMenu.getComponent(0) instanceof JScrollPane) {
    final JScrollPane scrollPane = (JScrollPane)popupMenu.getComponent(0);
    scrollPane.setBorder(BorderFactory.createEmptyBorder(bgTop, bgLeft, bgBottom, bgRight));
    scrollPane.setOpaque(false);
    scrollPane.getViewport().setOpaque(false);
    if (popupMenu instanceof ComboPopup) {
    final ComboPopup popup = (ComboPopup)popupMenu;
    final JList list = popup.getList();
    list.setBorder(null);
    final Dimension size = list.getPreferredSize();
    size.width = Math.max(box.getPreferredSize().width + bgLeft + bgRight, box.getWidth());
    size.height = Math.min(scrollPane.getPreferredSize().height + bgTop + bgBottom, size.height + bgTop + bgBottom);
    scrollPane.setPreferredSize(size);
    scrollPane.setMaximumSize(size);
    }
    }
    }
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured