Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2006

Tip: Looking for answers? Try searching our database.

how to refresh jComboBox in popupMenuWillBecomeVisible

Thread view: 
Trufel - 29 Aug 2006 11:54 GMT
hello,
i have jComboBox which should calculate its drop-down list size before
opening (it depends on current window size so I decide to put it into
popupMenuWillBecomeVisible method)

 void jComboParamNames_popupMenuWillBecomeVisible(PopupMenuEvent e) {
   int rowCount = 30;  // sample for testing purposes...
   myCombo.setMaximumRowCount(rowCount);
   // how to refresh combo???
 }

Desired size of drop down list will appear... after second click on the
comboBox. Is it possible to refresh it in popupMenuWillBecomeVisible
before showing drop-down list?

thanks
T.
Rui - 29 Aug 2006 20:45 GMT
Trufel a écrit :
> hello,
> i have jComboBox which should calculate its drop-down list size before
[quoted text clipped - 15 lines]
>
> From - Tue

If you intend to recalculate the size depending on the window size,
better use ComponentListener on the container of your combo.

public class ComboSizeTest extends JFrame {
    private static final String[] data = { "First", "Second", "Third",
            "Fourth", "Fifth", "Sixth" };

    JComboBox combo = null;

    public ComboSizeTest() {
        super("Test combo dropdown list resizing");
        combo = new JComboBox(data);
        this.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                System.out.println("Resized");
                combo.setMaximumRowCount(4);
            }
        });
        this.setSize(100, 50);
        this.getContentPane().add(combo, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        new ComboSizeTest().setVisible(true);
    }
}

NB: I added a setSize() method after adding the ComponentListener to
insure that combo size will be calculated before the frame is made visible.

Rui.
Trufel - 30 Aug 2006 10:38 GMT
Rui napisal(a):
> NB: I added a setSize() method after adding the ComponentListener to
> insure that combo size will be calculated before the frame is made visible.

Thanks! It works. I do it finally in this way:

   contentPane.addComponentListener(new
java.awt.event.ComponentAdapter() {
     public void componentResized(ComponentEvent e) {
       contentPane_componentResized(e);
     }
   });

 void contentPane_componentResized(ComponentEvent e) {
   System.out.println("content resized");
   int frameCurrentHeight = contentPane.getHeight();
   double comboYPosition = myCombo.getBounds().getY();
   int fontSize = (int)myCombo.getFont().getSize() + 6;
   myCombo.setFixedCellHeight(fontSize);
   int rowCount = (int)((frameCurrentHeight - comboYPosition) /
fontSize) - 5;
   myCombo.setMaximumRowCount(rowCount);
 }

thanks
T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.