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 / July 2005

Tip: Looking for answers? Try searching our database.

automatic resize of a JComboBox, based on its content

Thread view: 
Nomak - 20 Jul 2005 10:29 GMT
Hello,

i would like to resize a JComboBox in order to fit its content. The way i thought to use is to get the component of all the item and use getPreferredSize():

   public static void redimensionnementAuto(JComboBox combo) {
       int width = 1, height = 1;
       
       JList jList = new JList();
       for (int i = 0; i < combo.getItemCount(); i++) {
           Object o = combo.getItemAt(i);
           
           ListCellRenderer lcr = combo.getRenderer();
           Component c = lcr.getListCellRendererComponent(jList, o, i,
                   combo.getSelectedIndex() == i, combo.hasFocus());
           
           Dimension dim = c.getPreferredSize();
           
           width = Math.max(dim.width, width);
           height = Math.max(dim.height, height);
       }
       
       // bordure
       width += 20;
       height += 5;
       
       combo.setSize(width, height);
   }

passing null as the JList cause a NPE at javax.swing.DefaultListCellRenderer.getListCellRendererComponent(DefaultListCellRenderer.java:80)

i'm not really happy with this way of doing. Is there any better way to achieve the same result?
Michael Rauscher - 22 Jul 2005 09:51 GMT
Nomak schrieb:
> Hello,
>
> i would like to resize a JComboBox in order to fit its content. The way i thought to use is to get the component of all the item and use getPreferredSize():

Use LayoutManagers.

E.g. the following code should produce a JComboBox that is large enough
to display "A long item"...

// JComboBoxTest.java

import javax.swing.*;

public class JComboBoxTest {
    public static final void main( String args[] ) {
        JComboBox comboBox = new JComboBox(
                new String[]{"Short", "A long item"} );
        JFrame frame = new JFrame("...");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( comboBox,
                                    java.awt.BorderLayout.NORTH );
        frame.pack();
        frame.setVisible(true);
    }
}

Bye
Michael


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.