0

I have a combobox that is put in a 20x20 field, so only the button is shown.

When the user clicks the button, it drops down a popupmenu that is 150 wide (using Mark McLaren's WiderDropDownCombo solution).

However, the button is on the rightmost side of the panel, that contains 2 other fields (both JTextFields). When I open the dropdown menu, it starts from the top-left corner by default and goes out of bounds for the area. I need to change that so the combobox would appear beneath all fields.

I've tried messing with CellRenderers and orientation, but It doesn't seem to work. Anyone know something about the solution?!

My code for the part that creates the field is following :

    HistoryProcessor processor = new HistoryProcessor(field.getName().toLowerCase());
    amount = new JTextField( amountString );
    currency = new JTextField( currencyString );
    amount.setMinimumSize( new Dimension(94, 20) );
    amount.setPreferredSize( amount.getMinimumSize() );
    amount.setMaximumSize( amount.getMinimumSize() );
    currency.setMinimumSize( new Dimension(30, 20) );
    currency.setPreferredSize( currency.getMinimumSize() );
    currency.setMaximumSize( currency.getMinimumSize() );
    popupButton = processor.populateHistoryBox();
    popupButton.setWide(true);
    popupButton.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    popupButton.setMinimumSize(new Dimension(20, 20));
    popupButton.setPreferredSize(popupButton.getMinimumSize());
    popupButton.setMaximumSize(popupButton.getMinimumSize());
    panel.add(amount, BorderLayout.WEST);
    panel.add(currency, BorderLayout.CENTER);
    panel.add(popupButton, BorderLayout.EAST);
    popupButton.addPopupMenuListener(new MoneyHistoryListener(this));

where amount is the first field, currency the second and popupButton is the combobox. it is automatically filled by HistoryProcessor. Here's an image to my problem : https://i.stack.imgur.com/X3ZLk.jpg

enter image description here

Thanks in advance!

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • from this code and description is (only) clear that Mark McLaren's WiderDropDownCombo has bug, or popupButton.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); is proper (look like as) output from applyComponentOrientation, but everything is about guessing – mKorbel Jul 23 '13 at 10:57
  • I don't think that the DropDownCombo is bugged. http://pastebin.com/rpGcxH2w the only change I've made myself is that it's not text-dependant, but you can set a static size. – Rainer Keerdo Jul 23 '13 at 11:11
  • everything is about guessing, I'm never wrote your code, used WiderDropDownCombo – mKorbel Jul 23 '13 at 11:12
  • and JComboBox has own accelator for PreferredSize (your code talking about BoxLayout in API for WiderDropDownCombo, but screenshot talking about JList with PrototypeDispalyValue), to [setPrototypeDisplayValue](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#setPrototypeDisplayValue%28E%29) instead of, then JList in basiccomboboxpopup will know proper size on then screen (widht) – mKorbel Jul 23 '13 at 11:22

2 Answers2

2

I would simply create your own button. This way you can control the location (and size) of the popup yourself

Take a look at Make JPopupMenu Display with a Certain Bottom Left Coordinate for an example of how you can control the location of a JPopupMenu

You will need to construct your own JList and deal with the hiding the popup when the selection is changed, but it wouldn't to much work to put it altogether into a single component

Updated

You may also want to take a look at Prevent Popup Menu Dismissal

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Quick question about this - does this allow completely dynamic placements? I never know the location of my panels, as they are generated automatically based on information from a database/xml file, which may be changed. – Rainer Keerdo Jul 23 '13 at 11:09
  • You can do that. The example I've sited uses a parent component (that would be the button) to determine it's location on the screen and aligns itself accordingly. You will need to do some calculations to determine the best location for the popup, these are demonstrated [here](http://stackoverflow.com/questions/14670516/how-do-i-get-a-popupmenu-to-show-up-when-i-left-click-on-a-trayicon-in-java/14681503#14681503), [here](http://stackoverflow.com/questions/14431467/how-do-i-determine-the-position-of-the-system-tray-on-the-screen/14431536#14431536) and – MadProgrammer Jul 23 '13 at 11:12
  • [here](http://stackoverflow.com/questions/12133273/how-to-spring-jframe-from-system-tray-in-java/12133445#12133445). While these use the `SystemTray`, the concept is the same. It calculates the screen bounds, the location of the component is relative to and the best location it be displayed so that it appears on the screen – MadProgrammer Jul 23 '13 at 11:12
  • 1
    @mad rob created own popup factory for JComboBox (size, aling, ontop, bellow, both sides), too hard to scroll in JPopup, mouse or key selection to hide popup, but +1 (if I forgot about JPopup) – mKorbel Jul 23 '13 at 11:15
  • @mKorbel Ah, good point. There's a `clientProperty` that can be used to prevent this...can't for the life of me remember it... – MadProgrammer Jul 23 '13 at 11:15
  • @mKorbel [Prevent Popup menu dismissal](http://explodingpixels.wordpress.com/2008/11/10/prevent-popup-menu-dismissal/) – MadProgrammer Jul 23 '13 at 11:19
  • see robs site scroll in popup or something similair???, fixed > Java_1.4, then you have to wrote own code instead of using AWT/Swing popup (valid for JComboBox popup too) – mKorbel Jul 23 '13 at 11:25
  • Thanks! I'll just try to make a new button that displays the menu, although the requirements stated quite clearly that it has to be a combobox. – Rainer Keerdo Jul 23 '13 at 11:36
0

Take a look at the different Layout Managers in java. With using several JPanels you are able to combine different Layout Managers to fit your needs. I hope this helps you.

Sam
  • 1,301
  • 1
  • 17
  • 26