6 Replies - 623 Views - Last Post: 10 October 2012 - 10:54 AM Rate Topic: -----

#1 mon_e_mons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-October 12

Can anybody tell why my gridLayout isn't working?

Posted 10 October 2012 - 10:21 AM

Hi there. I'm sure I'm doing something silly but I can't figure out why my gridLayout isn't working properly. I have it set to gridLayout(5,3,5,5) and it makes 5 rows but no matter what my column int is set to it ALWAYS has only 2 columns. I don't get why. Why is this happening?
import javax.swing    .*;
import java .awt      .*;
import java .awt.event.*;
import java .awt.font .*;
import java .awt.geom .*;

public class Frames04 extends JFrame
{
	public Frames04()
	{
		int Row, Col;
		Toolkit MyKit = Toolkit.getDefaultToolkit();
		Dimension XY  = MyKit.getScreenSize();

		Row = XY.width;
		Col = XY.height;

		setSize    (2*Row/7,   Col/2);
		setLocation(  Row/4,5*Col/16);

		MyPanel Panel01 = new MyPanel();
		Container ContentPanel01 = getContentPane();
		ContentPanel01.add(Panel01);
	}
	private class MyPanel extends JPanel
	{
		public MyPanel()
		{
			setLayout(new BorderLayout());

			PanelOne.setBackground(Color.yellow);
			PanelOne.setLayout(new GridLayout(5,3,5,5));  //The gridLayout that isn't working is here.

			PanelTwo.setLayout(new GridLayout(1,1));

			for(int i = 0; i < 9; i++)
			{
				PanelOne.add(new JButton("" + (i+1)));
			}


			Font FontTxt1 = new Font("TimesRoman",Font.BOLD, 35);
			Txt1.setFont(FontTxt1);
			PanelTwo.add(Txt1);

			add(PanelTwo,BorderLayout.NORTH );
			add(PanelOne,BorderLayout.CENTER);
		}
	}
	JTextField Txt1    = new JTextField();
	JPanel PanelOne    = new JPanel    ();
	JPanel PanelTwo    = new JPanel    ();
}


Here's the caller if you want to try it yourself.
import javax.swing.*;

public class FrameCaller
{
	public static void main(String[] args)
	{
//		Frames01 MyFrame = new Frames01();
//		Frames02 MyFrame = new Frames02();
//       	Frames03 MyFrame = new Frames03();
                Frames04 MyFrame = new Frames04();

		MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		MyFrame.setVisible(true);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Can anybody tell why my gridLayout isn't working?

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Can anybody tell why my gridLayout isn't working?

Posted 10 October 2012 - 10:32 AM

Just playing around with the code, I found out this works:

Change the gridLayout so it is constructed this way:

PanelOne.setLayout(new GridLayout(0,3,5,5));



That is, as many rows as necessary. You get three columns. Still trying to figure out why your code isn't working as is...
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can anybody tell why my gridLayout isn't working?

Posted 10 October 2012 - 10:38 AM

Because GidLayout tries to gfill the row first
try with GridLayout(3,5) you will see that it will make a GridLayoput(3,3)

use new JLabel(" ") to fill the wholes if you really want your design

			for(int i = 0; i < 9; i++)
			{
				PanelOne.add(new JButton("" + (i+1)));
			}
			for(int i = 9; i < 15; ++i)
				PanelOne.add(new JLabel(" "));


Was This Post Helpful? 1
  • +
  • -

#4 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Can anybody tell why my gridLayout isn't working?

Posted 10 October 2012 - 10:40 AM

Got it. Not sure why this makes sense as a behavior, but this is what the Oracle says:

Quote

When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number or rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.


http://docs.oracle.c...GridLayout.html

If you fill it up completely, it will work correctly.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can anybody tell why my gridLayout isn't working?

Posted 10 October 2012 - 10:41 AM

From the API


When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.



Was This Post Helpful? 1
  • +
  • -

#6 mon_e_mons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-October 12

Re: Can anybody tell why my gridLayout isn't working?

Posted 10 October 2012 - 10:45 AM

Thanks everyone. I got it. I just had to made a number of buttons that was divisible by 3 to fill out each row completely.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can anybody tell why my gridLayout isn't working?

Posted 10 October 2012 - 10:54 AM

You can also use blank JLabel if require as in

7 8 9
4 5 6
1 2 3
. 0 .


the . being new JLabel(" ")
:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1