Question

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 : http://i.stack.imgur.com/X3ZLk.jpg

enter image description here

Thanks in advance!

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top