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.
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..
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?
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!
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!
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
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.
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.
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.
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?
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.
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
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.
public class EaSynthComboBoxPopupMenuListener implements PopupMenuListener {
// ==============================================================================
// Members
// ==============================================================================
private int bgTop = 4;
private int bgLeft = 4;
private int bgRight = 9;
private int bgBottom = 10;
Bookmarks