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.


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

Results 1 to 3 of 3

Thread: Could need some help with Swing

  1. #1
    Join Date
    Mar 2011
    Thanked 1 Time in 1 Post

    Default Could need some help with Swing

    Hi all.

    Have some issues with Swing, or more specifically, how to control the layout. I want to have a window where one part, at the bottom, has a rather constant size and the rest is taken by list. Only thing is that when I add things to the list it starts pushing down everything bellow it and resizing the window. I want to make so I instead get a scroll bar, or something like that. Not that good with layout managers, to be honest, so it is a real challenge. My code at the moment looks like this:
    	 * Creates and returns a window frame
    	protected JFrame createFrame()
    		// Create the window
    		JPanel content = new JPanel( true );
    		content.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		c.gridheight = 1;
    		c.weightx = 1;
    		c.weighty = 1;
    		c.insets = new Insets(1, 1, 1,  1);
    		// Create the list
    		JList l = new JList();
    		l.setBorder(new EtchedBorder( EtchedBorder.LOWERED ));
    		l.setSize(400, 500);
    		l.setModel( mLstModel );
    		c.fill = GridBagConstraints.BOTH;
    		c.gridwidth = 3;
    		c.gridx = 0;
    		c.gridy = 0;
    		content.add(l, c);
    		// Add the information panel
    		JPanel info = new JPanel( true );
    		info.setBorder(new TitledBorder(new EtchedBorder( EtchedBorder.LOWERED ), "Info"));
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridy = 2;
    		content.add(info, c);
    		// Set the constraints that applies to the buttons
    		c.fill = GridBagConstraints.NONE;
    		c.gridwidth = 1;
    		c.weighty = 0;
    		// Add the buttons
    		c.gridy = 4;
    		JButton btn = new JButton( "Close" );
    		btn.addActionListener( mClose );
    		c.gridx = 2;
    		c.anchor = GridBagConstraints.LAST_LINE_END;
    		content.add(btn, c);
    		// Return the window
    		JFrame win = new JFrame( "View logs" );
    		win.add( content );
    		return win;
    The method will create a frame and return it. mLstModel is the model for the list and mClose is the action listener for the close button (I made it a variable because I figured it would mean less objects will be created, which may make things easier for the gc... or something, lol, I may be thinking completely wrong). Not asking for you to code for me, but any pointers and help with how to make it work the way I want it to, i.e. making so adding stuff to the list won´t push down the components bellow it and won´t start to resize the window, would be deeply appreciated.

    Tell me if you want more code, this method is the only one that actually creates the window and its components. The rest just deals with what is created, so to speak.

    Take care,
    Last edited by Kerr; March 10th, 2011 at 06:13 AM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Could need some help with Swing

  3. The Following User Says Thank You to copeg For This Useful Post:

    Kerr (March 10th, 2011)

  4. #3
    Join Date
    Mar 2011
    Thanked 1 Time in 1 Post

    Default Re: Could need some help with Swing

    Ok, thanks, will test that .


    Worked perfectly, thanks.
    Last edited by Kerr; March 10th, 2011 at 11:37 AM.

Similar Threads

  1. OSGi and Swing
    By dead_devil_66 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 8th, 2011, 02:40 PM
  2. Swing
    By waboke in forum AWT / Java Swing
    Replies: 2
    Last Post: November 3rd, 2010, 08:55 AM
    By relixus in forum AWT / Java Swing
    Replies: 3
    Last Post: October 3rd, 2010, 10:55 PM
  4. Text in Swing
    By whoismrsaxon in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:22 AM
  5. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM