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 7 of 7

Thread: JComboBox size?

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default JComboBox size?

    I'm trying to set a maximum size in a JComboBox, but I can't do it.
    I'm willing to do it so I can put the selecter next to a JLabel, but it's going into the second line, and not following the GridLayout

    package temporizadorDeEventos;
     
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridLayout;
     
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    @SuppressWarnings("serial")
    public class Informacoes extends JFrame {
     
    	JLabel qualDia;
    	public Integer[] dias = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,29,21,22,23,24,25,26,27,28,29,30,31};
    	JComboBox<Integer> opDias;
    	public Dimension tamanhoJB = new Dimension(2, 3);
     
    	public Informacoes(){
     
    		super("Novo Temporizador");
     
    		Container c1 = getContentPane();
    		c1.setLayout(new GridLayout(15,3));
     
    		qualDia = new JLabel("Select a number");
    		opDias = new JComboBox<Integer>(dias);
    			opDias.setPrototypeDisplayValue(22);
    			opDias.setMaximumSize(tamanhoJB);
    			opDias.setSize(tamanhoJB);
     
    		c1.add(qualDia);
    		c1.add(opDias);
     
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(300, 300);
    		setVisible(true);	
    	}
     
     
    	public static void main(String[] args){
    		new Informacoes();
     
    	}
     
    }

    I tried using the methods

    opDias.setPrototypeDisplayValue(00);
    opDias.setMaximumSize(tamanhoJB);
    opDias.setSize(tamanhoJB);

    But neither of this work, the size is still big!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JComboBox size?

    You might want to experiment with FlowLayout. I don't know what else you plan to add to the interface, but setting the layout to a FlowLayout and then adding the JLabel and JComboBox will give you the effect you've described.

    As an aside, using the JFrame's content pane was a technique that became unnecessary with Java 1.4 (I think). You can accomplish the same thing now by adding directly to the JFrame.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox size?

    I got the part of the content pane! Thanks for that!!

    About the FlowLayout, could you tell me how to make a linebreak on a FlowLayout? Because I need 15 lines of information, and making a FlowLayout would put everything in a row, wouldn't it?


    @@Edit

    For the getContentPane() trick I were using, I changed it for
    this.setLayout(new FlowLayout());
    this.add(); etc...
    Was this what you meant?
    Last edited by Kerooker; May 23rd, 2014 at 07:16 PM.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JComboBox size?

    Are the 15 lines of info of the same format:

    JLabel, JComboBox
    JLabel, JComboBox
    JLabel, JComboBox

    etc. , 15 times?

    You had a third column. What was that for?

    As for your edits, Yes, that's what I had in mind, but the 'this.' is not required. It's okay to include, but not necessary.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox size?

    The third column was a mistake

    No, not all with the same format. Some are Label-Jbox ; some are Label-JTextBox .... Some are with imageicon.. That's why I needed the GridLayout

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JComboBox size?

    Then I think a GridLayout is not what you want. In a GridLayout, "each cell is exactly the same size." (From the How to Use GridLayout page.) That's great for chess/checker boards, tic-tac-toe, and other applications where the sizes of each cell don't need to accommodate different sized contents like what you've described.

    I don't know which layout is (or layouts are) best for your design. If you size the components appropriately and prevent the container from being resized, FlowLayout might be the simplest to do. If that approach does not meet your requirements, then you'll have to try a combination of containers and components. Don't restrict yourself to a single container. Maybe a container containing multiple containers will give the flexibility and control you need.

  7. #7
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: JComboBox size?

    You might want to try SpringLayout. Take a look at the screenshots at How to Use SpringLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) to see if it meets your needs.

Similar Threads

  1. Jpanel preffered size exceed the nactual screen size
    By manish.ctae@gmail.com in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2012, 01:29 AM
  2. JComboBox
    By SV25 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 20th, 2011, 08:47 AM
  3. [SOLVED] JCombobox help
    By sman36 in forum AWT / Java Swing
    Replies: 10
    Last Post: August 11th, 2010, 04:11 AM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  5. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM