Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to programmatically set combobox width?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to programmatically set combobox width?

    Hi,

    Newbie in Java testing database access.
    Succeeded to load driver, make a connection and reading tablenames.
    Want to load retrieved tablenames into a jcombobox and afterwards adjust
    combobox width to show even the table with longest name.
    Tried this code:

      private void getTables(Connection conn) throws SQLException
      {
        DatabaseMetaData dbmd = conn.getMetaData();
     
        FontMetrics fm = cbxTableNames.getFontMetrics(cbxTableNames.getFont());
     
        int maxw, w;
        maxw = 0;
        String[] types = {"TABLE"};
        ResultSet tables = dbmd.getTables(null, null, null, types);
        while (tables.next()) 
        {
          String tablename = tables.getString("TABLE_NAME");
          w = fm.stringWidth(tablename);
          if (maxw < w)
          {  maxw = w;
          }
          cbxTableNames.addItem(tablename);
        }
        cbxTableNames.setVisible(true);
        lblTableNames.setVisible(true);
        cbxTableNames.setSize(new Dimension(maxw, 20));
      }

    Last statement:cbxTableNames.setSize(new Dimension(maxw, 20)); doesn't change the combobox width
    but adding a separate jButton and in its' ActionPerformed event executing:
    cbxTableNames.setSize(new Dimension(300, 20)) changes the with.

    Why and how can I achieve desired effect?

    Regards
    stab


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to programmatically set combobox width?

    Try setPreferredSize(Dimension preferredSize) method instead.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. JTable is not fit to the screen width.
    By rajakumarjk in forum AWT / Java Swing
    Replies: 2
    Last Post: October 12th, 2010, 10:13 AM
  2. Programmatically Creating Jars
    By aussiemcgr in forum Java Theory & Questions
    Replies: 8
    Last Post: October 6th, 2010, 11:51 AM
  3. JButton Width & Height
    By Howdy_McGee in forum Java Theory & Questions
    Replies: 4
    Last Post: May 22nd, 2010, 07:12 AM
  4. [SOLVED] frame closing programmatically
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: May 10th, 2010, 03:43 AM
  5. Replies: 3
    Last Post: April 14th, 2010, 07:33 PM