-
April 28th, 2004, 10:07 AM
#1
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.
-
April 28th, 2004, 10:21 AM
#2
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..
-
April 28th, 2004, 10:22 AM
#3
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?
-
April 28th, 2004, 10:24 AM
#4
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!
-
April 28th, 2004, 10:26 AM
#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!
-
April 28th, 2004, 02:29 PM
#6
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?
-
May 3rd, 2004, 01:49 AM
#7
Re: Re: Re: Re: The width of the JComboBox drop-down list
Ok! But how do i get JPopupMenu for this combobox, from the PopupMenuevent?
-
May 3rd, 2004, 01:14 PM
#8
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.
-
May 3rd, 2004, 02:36 PM
#9
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?
-
May 4th, 2004, 05:15 AM
#10
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.
-
May 4th, 2004, 11:22 AM
#11
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
-
June 8th, 2012, 04:20 AM
#12
Re: The width of the JComboBox drop-down list
Originally Posted by dlorde
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|