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 / January 2007

Tip: Looking for answers? Try searching our database.

setting size of JComboBox

Thread view: 
Brandon McCombs - 28 Jan 2007 09:36 GMT
Has anyone ever had issues with setting the preferred size of a
JComboBox using GridBagLayout?  It seems the box's height is larger than
 the JTextField's height that I create in the same window.  I can
change the height of the JTextFields by using setPreferredSize() to
match the JComboBox so everything is uniform but that's only because I
can't decrease the JComboBox's height because it seems to ignore that
call.  Any ideas?

thanks
Brandon
Knute Johnson - 28 Jan 2007 18:01 GMT
> Has anyone ever had issues with setting the preferred size of a
> JComboBox using GridBagLayout?  It seems the box's height is larger than
[quoted text clipped - 6 lines]
> thanks
> Brandon

Set the fill element on the GridBagConstraints for the JTextField.

Signature

Knute Johnson
email s/nospam/knute/

Brandon McCombs - 28 Jan 2007 19:43 GMT
>> Has anyone ever had issues with setting the preferred size of a
>> JComboBox using GridBagLayout?  It seems the box's height is larger
[quoted text clipped - 8 lines]
>
> Set the fill element on the GridBagConstraints for the JTextField.

Won't that make the JTextField match the size of the JComboBox? I want
the JComboBox to match the size of the JTextField (JComboBox has the
larger height due to the button for the dropdown box) which doesn't work
at the moment because it ignores the setPreferredSize() call.
Knute Johnson - 28 Jan 2007 21:07 GMT
>>> Has anyone ever had issues with setting the preferred size of a
>>> JComboBox using GridBagLayout?  It seems the box's height is larger
[quoted text clipped - 13 lines]
> larger height due to the button for the dropdown box) which doesn't work
> at the moment because it ignores the setPreferredSize() call.

JComboBoxes have a preferred size that is larger than a JTextField.
GridBagLayout uses only preferred size, not minimum size.  So if you
really want to make the JComboBox smaller then you will have to do some
tricks.  Set the minimum size to 1,1 and reset the preferred size to
match the height of the JTextField.  I think I would just let my
JTextField get bigger to match the JComboBox but it does appear to work.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame {
    String[] array = {"one","two","three"};

    public test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2,2,2,2);

        JComboBox cb = new JComboBox(array);
        JTextField tf = new JTextField("test");

        System.out.println(cb.getMinimumSize());
        System.out.println(cb.getPreferredSize());
        System.out.println(tf.getPreferredSize());

        cb.setMinimumSize(new Dimension(1,1));
        cb.setPreferredSize(new Dimension(cb.getPreferredSize().width,
         tf.getPreferredSize().height));

        add(cb,c);
        add(tf,c);

        pack();
        setVisible(true);

        System.out.println(cb.getSize());
        System.out.println(cb.getPreferredSize());
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                new test();
            }
        };
        EventQueue.invokeLater(r);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Brandon McCombs - 28 Jan 2007 22:51 GMT
>>>> Has anyone ever had issues with setting the preferred size of a
>>>> JComboBox using GridBagLayout?  It seems the box's height is larger
[quoted text clipped - 45 lines]
>         cb.setPreferredSize(new Dimension(cb.getPreferredSize().width,
>          tf.getPreferredSize().height));

thanks. I got it to work by doing that. I had tried to set the size
before but couldn't get it to work. I'm not sure what the difference
between your code and my previous code.  By the way, I was able to leave
out the setMinimumSize() call.


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.