13 Replies - 495 Views - Last Post: 09 October 2013 - 08:40 PM Rate Topic: -----

#1 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Still not understanding Layout Managers?

Posted 06 October 2013 - 10:33 PM

Hi everyone, i'm trying to do a simple calculator however, the GUI isn't coming out the way i expected (Using whatever knowledge i know)! I would like some assistant as to what i should do or what i'm doing wrong. Now i know we have some very generous individuals out there but please try not to give me the point blank answer right away. I'm trying to understand and fully comprehend this as best as possible. I had an individual on another thread i created give me pointers but once i began trying to implement it, well lets say it wasn't coming out as easy as he made it sound (Not your fault my friend :D/>). Anyways this is what i'm trying to do:

-I'm trying to create a calculator GUI, So By all means only pay attention to the Constructor i have which will consist of the GUI itself. I want to surround the whole frame under a BorderLayout, Then i want to set each individual JPanel to a specific point in the Border Layout (CENTER, SOUTH, NORTH, WEST & EAST)! I want to set the JTextArea (JPANEL) on the Center and the Buttons which consist of the values on the SOUTH (It could also be NORTH and CENTER rather than CENTER and SOUTH)! I then want to surround the JButton in a GridLayout so the buttons stay neat and align with each other.

This is the code i have so far:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class jCalculator extends JFrame implements ActionListener{

	public static void main(String[] args) {
		jCalculator CALCULATOR = new jCalculator();
		CALCULATOR.setVisible(true);
	}

	public jCalculator(){
		this.setTitle("jCalculator");
		this.setSize(300,400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setLayout(new BorderLayout());
		
		JPanel CENTER_PANEL = new JPanel();
		
		JTextArea TEXT_AREA = new JTextArea("Write Here!");
		TEXT_AREA.setSize(200, 300);
		CENTER_PANEL.add(TEXT_AREA, BorderLayout.CENTER);
		this.add(CENTER_PANEL);
		
		
		JPanel SOUTH_PANEL = new JPanel();
		SOUTH_PANEL.setLayout(new GridLayout());
		for(int i = 0; i<8;++i){
			SOUTH_PANEL.add(new JButton("Button"), BorderLayout.SOUTH);
		}
		
	}
	public void actionPerformed(ActionEvent ae) {
		//ae = Action Event (Abbreviation)
		//if(ae.getSource() == ){}
	}

}



P.S: I wanted to implement a FOR LOOP so that the buttons will print themselves out which will write "Button""Button""Button",etc........

If by any way there is some confusion as to what i''m asking, please don't hesitate on asking. I want to help you, Help me!

Thank You
-Jonathan

This post has been edited by AndreeU17: 06 October 2013 - 10:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Still not understanding Layout Managers?

#2 ChrisNt  Icon User is online

  • cute bug

Reputation: 212
  • View blog
  • Posts: 647
  • Joined: 31-July 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:01 PM

I want to surround the whole frame under a BorderLayout
Default layout of your JFrame is BorderLayout so no need for that

Then i want to set each individual JPanel to a specific point in the Border Layout
That would be done when you add your panel to the JFrame
add(panel,BorderLayout.THE POSITION YOU WANT);



I then want to surround the JButton in a GridLayout so the buttons stay neat and align with each other.
Which is the problem you have to achieve this? you have to specify the rows and the columns so they allign as you want...


I wanted to implement a FOR LOOP so that the buttons will print themselves out which will write..

Create an array with JButton type and in your for loop create an assign JButtons to array positions.

This post has been edited by ChrisNt: 06 October 2013 - 11:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:10 PM

View PostChrisNt, on 07 October 2013 - 12:01 AM, said:

I want to surround the whole frame under a BorderLayout
Default layout of your JFrame is BorderLayout so no need for that

Then i want to set each individual JPanel to a specific point in the Border Layout
That would be done when you add your panel to the JFrame
add(panel,BorderLayout.THE POSITION YOU WANT);



I then want to surround the JButton in a GridLayout so the buttons stay neat and align with each other.
Which is the problem you have to achieve this? you have to specify the rows and the columns so they allign as you want...


I wanted to implement a FOR LOOP so that the buttons will print themselves out which will write..

Create an array with JButton type and in your for loop create an assign JButtons to array positions.


For your 1st question:

I thought that the default Layout In Java is FlowLayout? I read it on the Docs from oracle site! Or maybe i read it wrong!

For your 2nd Question:
Okay that i kinda didnt understand, i was having issue determining were to exactly put
BorderLayout.CENTER


For Your 3rd and 4th question:
Okay yes i was a few examples and recommended to use arrays and for the for loop, should i first create a JButton Object and declare it inside the for loop or should i already declare it outside?
		JButton[] BUTTON_ARRAYS = null; //null was inserted because apperantly eclipse though it should lol
		for(int i = 0; i<8;++i){
			BUTTON_ARRAYS[i] = new JButton("Buttons" + i);
		}

Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt  Icon User is online

  • cute bug

Reputation: 212
  • View blog
  • Posts: 647
  • Joined: 31-July 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:18 PM

No you maybe read that the default layout of JPanel is FlowLayout.

You just have to create the array outisde your for loop using the keyword new and set the size .. = new JButton[size];

This post has been edited by ChrisNt: 06 October 2013 - 11:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:20 PM

Quote

I thought that the default Layout In Java is FlowLayout? I read it on the Docs from oracle site! Or maybe i read it wrong!


Content panes use BorderLayout for default. Otherwise yes, you would be correct. You can always use
setLayout(new BorderLayout());


Quote

For your 2nd Question:
Okay that i kinda didnt understand, i was having issue determining were to exactly put
BorderLayout.CENTER


When you are adding your panel is where you put the location for it, as ChrisNt said. i.e.

add(panel, BorderLayout.CENTER);


Quote

For Your 3rd and 4th question:
Okay yes i was a few examples and recommended to use arrays and for the for loop, should i first create a JButton Object and declare it inside the for loop or should i already declare it outside?


JButton[] button = new JButton[numOfButtons];
for(int i = 0; i < button.length; i++)
    button[i] = new JButton("" + (i + 1));



is a little more of a proper way to do so. it should not be initialized at null.

edited a few typos in the code itself.

This post has been edited by schutzzz: 06 October 2013 - 11:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:23 PM

View PostChrisNt, on 07 October 2013 - 12:18 AM, said:

No you maybe read that the default layout of JPanel is FlowLayout.

You just have to create the array outisde your for loop using the keyword new and set the size .. = new JButton[size];


Oh yeah your right, now i remember!
I'm not sure what you mean?
i thought that the GridLayout would magically give them a specific size? The GridLayout requires number of rows and columns.
Would i need to do something like this:
BUTTON_ARRAYS[] = new JButton(new GridLayout(4,4));


However, since that is going to be in a loop, i would get an error for the multiple number of GridLayouts, correct? Other than that my friend im still confused?
Do you know an example that i can look up (Only if its from the top of your head).
Was This Post Helpful? 0
  • +
  • -

#7 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:26 PM

I made a tutorial on this exact subject,

Creating a calculator using JFrame

If it's not exactly the help on the subject you need let me know further.
Was This Post Helpful? 0
  • +
  • -

#8 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:28 PM

View Postschutzzz, on 07 October 2013 - 12:20 AM, said:

Quote

I thought that the default Layout In Java is FlowLayout? I read it on the Docs from oracle site! Or maybe i read it wrong!


Content panes use BorderLayout for default. Otherwise yes, you would be correct. You can always use
setLayout(new BorderLayout());


Quote

For your 2nd Question:
Okay that i kinda didnt understand, i was having issue determining were to exactly put
BorderLayout.CENTER


When you are adding your panel is where you put the location for it, as ChrisNt said. i.e.

add(panel, BorderLayout.CENTER);


Quote

For Your 3rd and 4th question:
Okay yes i was a few examples and recommended to use arrays and for the for loop, should i first create a JButton Object and declare it inside the for loop or should i already declare it outside?


JButton[] button = new JButton[numOfButtons];
for(int i = 0; i < button.length; i++)
    button[i] = new JButton("" + (i + 1));



is a little more of a proper way to do so. it should not be initialized at null.

edited a few typos in the code itself.


Aww i see i understand that for loop much much better :D Okay now were would i set the GridLayout? I want the JButton in a gridlayout which will be all inside the BorderLayout.
should i set up a JPanel to be the GridLayout?
this.add(panel, BorderLayout.SOUTH); //Here i'm adding the panel positioned in the South on the JFrame
JPanel panel = new JPanel(); //Creating a Panel
panel.setLayout(new GridLayout(4,4)); //Setting the panel to be GridLayout with 4 rows & 4 Columns
panel.add(button); //Here adding the button array to the GridLayout Panel 



Would that be correct?
Was This Post Helpful? 0
  • +
  • -

#9 ChrisNt  Icon User is online

  • cute bug

Reputation: 212
  • View blog
  • Posts: 647
  • Joined: 31-July 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:29 PM

You got me wrong.

1. You create an array with a specific size for your JButtons.
2. In your for loop you create the JButtons as you did.
3. In your for loop you add the JButtons to your JPanel .

This post has been edited by ChrisNt: 06 October 2013 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#10 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Still not understanding Layout Managers?

Posted 06 October 2013 - 11:32 PM

Except you also have to add your buttons to the panel in the loop. You can't just add the array.
Was This Post Helpful? 0
  • +
  • -

#11 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Re: Still not understanding Layout Managers?

Posted 07 October 2013 - 06:53 AM

View Postschutzzz, on 07 October 2013 - 12:26 AM, said:

I made a tutorial on this exact subject,

Creating a calculator using JFrame

If it's not exactly the help on the subject you need let me know further.


Hi thank you for linking me to your tutorial, i dont really understand the dimensions but other than that its great i just dont want to copy it i want to use my knowledge. i been doing java for 6 months mainly math problems, Now i need to use what i know and implement it into codes.
Was This Post Helpful? 0
  • +
  • -

#12 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Still not understanding Layout Managers?

Posted 07 October 2013 - 07:51 AM

I wasn't linking it so you could copy it. I was linking so you could learn from it.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

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

Re: Still not understanding Layout Managers?

Posted 07 October 2013 - 09:28 AM

[

public class jCalculator extends JFrame implements ActionListener{

	public static void main(String[] args) {
		jCalculator CALCULATOR = new jCalculator();
		CALCULATOR.setVisible(true);
	}


First respect Java conventions, your code is complicated to read
Variable start by a lower case
Class name start by a uppercase

	public JCalculator(){
		
		JPanel centerPanel = new JPanel();
		
		JTextArea textArea = new JTextArea("Write Here!");
		textArea.setSize(200, 300);
		centerPanel.add(textArea, BorderLayout.CENTER);
		this.add(centerPanel);


don't set the size of the component, let the Layout Manager do it
If you want to use a CENTER you need a BorderLayour

		JPanel centerPanel = new JPanel(new BorderLayout());
		
		JTextArea textArea = new JTextArea("Write Here!");
		centerPanel.add(textArea, BorderLayout.CENTER);
		this.add(centerPanel);   // <-- this add in the CENTER region of the JFrame



		
		JPanel SOUTH_PANEL = new JPanel();
		SOUTH_PANEL.setLayout(new GridLayout());
		for(int i = 0; i<8;++i){
			SOUTH_PANEL.add(new JButton("Button"), BorderLayout.SOUTH);
		}


No it is a GridLayout so you just add() to it
		
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new GridLayout());
		for(int i = 0; i<8;++i){
			southPanel.add(new JButton("Button")); // , BorderLayout.SOUTH);
		}


Was This Post Helpful? 0
  • +
  • -

#14 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Re: Still not understanding Layout Managers?

Posted 09 October 2013 - 08:40 PM

View Postpbl, on 07 October 2013 - 10:28 AM, said:

[

public class jCalculator extends JFrame implements ActionListener{

	public static void main(String[] args) {
		jCalculator CALCULATOR = new jCalculator();
		CALCULATOR.setVisible(true);
	}


First respect Java conventions, your code is complicated to read
Variable start by a lower case
Class name start by a uppercase

	public JCalculator(){
		
		JPanel centerPanel = new JPanel();
		
		JTextArea textArea = new JTextArea("Write Here!");
		textArea.setSize(200, 300);
		centerPanel.add(textArea, BorderLayout.CENTER);
		this.add(centerPanel);


don't set the size of the component, let the Layout Manager do it
If you want to use a CENTER you need a BorderLayour

		JPanel centerPanel = new JPanel(new BorderLayout());
		
		JTextArea textArea = new JTextArea("Write Here!");
		centerPanel.add(textArea, BorderLayout.CENTER);
		this.add(centerPanel);   // <-- this add in the CENTER region of the JFrame



		
		JPanel SOUTH_PANEL = new JPanel();
		SOUTH_PANEL.setLayout(new GridLayout());
		for(int i = 0; i<8;++i){
			SOUTH_PANEL.add(new JButton("Button"), BorderLayout.SOUTH);
		}


No it is a GridLayout so you just add() to it
		
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new GridLayout());
		for(int i = 0; i<8;++i){
			southPanel.add(new JButton("Button")); // , BorderLayout.SOUTH);
		}


Hi, sorry for the late reply but i been busy with school transfering, etc. Anyways thank you for the tips as i was able to fix them and got a great resulting answer :D Something that i was looking for extremely:D
Posted Image

Anyways i have a few quesitons, i dont understand why i need to set the layout for the Center panel (JTextArea)? I actually setted the BorderLayout for the JFrame and left the Center Panel alone but i made the South Panel a grid. or in other words what does this really mean:
[JPanel centerPanel = new JPanel(new BorderLayout());

Is it letting something know that it will follow a BorderLayout Layout? Im curious since here i simply added it
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new GridLayout());

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1