Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Java Programming 24-Hour Trainer by Yakov Fain
This is the forum to discuss the Wrox book Java Programming 24-Hour Trainer by Yakov Fain; ISBN: 978-0-470-88964-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Java Programming 24-Hour Trainer by Yakov Fain section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 15th, 2012, 08:55 PM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 8 Try It Error

Hello all. I am not able to compile the try it section. I used the same calculator and replaced BorderLayout with GridbagLayout. I keep getting an exception. Anyone know how to get passed this error?

Code:
public class CalculatorTryIt {

	// Declare all calculator's components.
	JPanel windowContent;
	JTextField displayField;
	JButton button0;
	JButton button1;
	JButton button2;
	JButton button3;
	JButton button4;
	JButton button5;
	JButton button6;
	JButton button7;
	JButton button8;
	JButton button9;
	JButton buttonPoint;
	JButton buttonEqual;
	//JPanel p1;
	
	// Constructor creates the components
	// and adds the to the frame using combination of
	// BorderLayout and GridLayout
	
	CalculatorTryIt() {
		windowContent = new JPanel();
		
		// Set the layout manager for this panel
		GridBagLayout b1 = new GridBagLayout();
		windowContent.setLayout(b1);
		
		// Create the display field and place it in the
		// North area of the window
		displayField = new JTextField(30);
		windowContent.add(displayField, GridBagConstraints.NORTH);
		
		// Create buttons using constructor of the 
		// class JButton that takes the label of the
		// button as a parameter
		button0 = new JButton("0");
		button1 = new JButton("1");
		button2 = new JButton("2");
		button3 = new JButton("3");
		button4 = new JButton("4");
		button5 = new JButton("5");
		button6 = new JButton("6");
		button7 = new JButton("7");
		button8 = new JButton("8");
		button9 = new JButton("9");
		buttonPoint = new JButton(".");
		buttonEqual = new JButton("=");
		
		// Add window controls to the panel p1
		windowContent.add(button1, GridBagConstraints.FIRST_LINE_START);
		windowContent.add(button2, GridBagConstraints.PAGE_START);
		windowContent.add(button3, GridBagConstraints.FIRST_LINE_END);
		windowContent.add(button4, GridBagConstraints.LINE_START);
		windowContent.add(button5, GridBagConstraints.CENTER);
		windowContent.add(button6, GridBagConstraints.LINE_END);
		windowContent.add(button7, GridBagConstraints.LAST_LINE_START);
		windowContent.add(button8, GridBagConstraints.PAGE_END);
		windowContent.add(button9, GridBagConstraints.LAST_LINE_END);
		windowContent.add(button0, GridBagConstraints.SOUTHWEST);
		windowContent.add(buttonPoint, GridBagConstraints.SOUTH);
		windowContent.add(buttonEqual, GridBagConstraints.SOUTHEAST);
		
		// Create the frame and set its content pane
		JFrame frame = new JFrame("Calculator");
		frame.setContentPane(windowContent);
		
		// Set the size of the window big enough to accomodate all controls
		frame.pack();
		
		// Display the window
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CalculatorTryIt calc = new CalculatorTryIt();
	}

}
ERROR:
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at com.calculator.lesson8.CalculatorTryIt.<init>(Calc ulatorTryIt.java:46)
at com.calculator.lesson8.CalculatorTryIt.main(Calcul atorTryIt.java:94)
Reply With Quote
  #2 (permalink)  
Old February 16th, 2012, 07:53 AM
Authorized User
Points: 30, Level: 1
Points: 30, Level: 1 Points: 30, Level: 1 Points: 30, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2011
Location: uk
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Lesson 8 Try It Error

Hi Luthios

the first thing that is causing the error is this line
Code:
windowContent.add(displayField, GridBagConstraints.NORTH);
when your public static void Main is calling the CalculatorTryIt class it's giving you the illegal componant position because of the above code.

I think you need to create and instance of the GridBagConstraints and set the x and y coordinates in the grid, height and width of the cell, the fill space in the cell, horizontal and vertical space, and the position of the component in the cell.
Code:
	//Create an instance of the GridBagContstraints
	//You'll have to repeat these lines for each component
	//that you'd like to add to the grid cell
	GridBagConstraints constr = new GridBagConstraints();
	
	// x coordinate in grid
	constr.gridx = 0;
	//y coordinate in grid
	constr.gridy = 0;
	
	//this cell has the same height as other cells
	constr.gridheight = 1;
	
	//this cell is as wide as 6 other cells
	constr.gridwidth = 6;
	
	//fill all space in the cell
	constr.fill = constr.BOTH;
	
	//proportion of horizontal space taken by this component
	constr.weightx = 1.0;
	
	// proportion of vertical
	constr.weighty = 1.0;
	
	//position of component within cell
	constr.anchor = constr.NORTH;
Then add to window
Code:
	displayField = new JTextField();
	b1.setConstraints(displayField, constr);
	windowContent.add(displayField);
you need to repeat these steps for each component you want to add

Code:
//Create an instance of the GridBagContstraints
	GridBagConstraints conbt0 = new GridBagConstraints();
	
	// coordinates in grid
	conbt0.gridx = 0;
	conbt0.gridy = 1;
	
	//this cell has the same height and Width as other cells
	conbt0.gridheight = 1;
	conbt0.gridwidth = 1;
	
	//fill all space in the cell
	conbt0.fill = conbt0.BOTH;
	
	//proportion of horizontal and vertical space taken by this component
	conbt0.weightx = 1.0;
	conbt0.weighty = 1.0;
	
	//position of component within cell
	conbt0.anchor = conbt0.WEST;
	
	button1 = new JButton("1");
	button2 = new JButton("2");
	button3 = new JButton("3");
	
	p1 = new JPanel();
	
	p1.add(button1);
	p1.add(button2);
	p1.add(button3);
	
	windowContent.add(p1, conbt0);
notice the gridy coordinate contbt0.gridy =1;

Think this is correct, it worked for me anyway.
Reply With Quote
  #3 (permalink)  
Old February 18th, 2012, 02:19 PM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wolverine, thanks! This allowed me to complete the lesson8 try it. The GridBagLayout is still a little awkward for me but at least now I grasp the concept.
Reply With Quote
  #4 (permalink)  
Old June 7th, 2012, 07:57 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson8 Try It Solution?

I tried to download Lesson8 Try it Solution in regard to using the GridBagLayout for a Calculator from the web site but it was not there. Which seems to be the same for all lessons , the Try It solutions are not given in the Download section for each Lesson, even though in the book it states that they are.

Has anybody else been able to download it, or possibly have it. As I cannot seem to get my program to work without getting exception errors.

Just that in Lesson10 Try It Exercise, it ask to copy Try It section of Lesson 8
Reply With Quote
  #5 (permalink)  
Old June 11th, 2012, 07:04 AM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Size of buttons

Hello,
Can anyone help me with lesson8 try it. What I can`t figure out is how to make buttons the same size. For example I want backspcae,CE and C to be the same size but whatever I try Backspace is always bigger. Here is my code.
EDIT: sorry for the comments in code, please try to ignore them :D

package com.PracticalJava.Lesson8;

import javax.swing.*;

import java.awt.*;



public class AdvancedCalculator {
JPanel windowContent;
JTextField displayField;
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton backspace;
JButton CE;
JButton C;
JButton MC;
JButton MR;
JButton MS;
JButton Mplus;
JButton plusMinus;
JButton plus;
JButton backSlash;
JButton multiply;
JButton minus;
JButton sqrt;
JButton percent;
JButton oneSlashX;
JButton point;
JButton equal;
JPanel p1;
JPanel p2;
JPanel p3;
JPanel p4;
JPanel p5;


AdvancedCalculator()
{
windowContent = new JPanel ();

GridBagLayout g = new GridBagLayout();
windowContent.setLayout(g);

//POLJE ZA UPIS BROJEVA
GridBagConstraints upisBrojeva = new GridBagConstraints();

upisBrojeva .gridx=0; //položaj u matrici koordinate 0,0
upisBrojeva .gridy=0;

upisBrojeva.gridheight=1; //visina čelije
upisBrojeva.gridwidth=6; //sirina čelije, široka je kao 6 čelija

upisBrojeva.fill=upisBrojeva.BOTH;//popunjava svih 6 čelija za upis brojeva

upisBrojeva.weightx=1.0;
upisBrojeva.weighty=1.0;

upisBrojeva.anchor=upisBrojeva.CENTER;

displayField = new JTextField ();
g.setConstraints(displayField, upisBrojeva);
windowContent.add(displayField);

//TIPKE U DRUGOM REDU
GridBagConstraints red1backspace = new GridBagConstraints ();

red1backspace .gridx=0;
red1backspace .gridy=1;

red1backspace.gridheight=1;
red1backspace.gridwidth=2;


red1backspace.weightx=1.0;
red1backspace.weighty=1.5;

red1backspace.anchor=red1backspace.FIRST_LINE_END;

backspace=new JButton("Backspace");
CE=new JButton ("CE");
C=new JButton ("C");

p1=new JPanel ();

p1.add(backspace);
p1.add(CE);
p1.add(C);

g.setConstraints(p1, red1backspace);
windowContent.add(p1);



//red2
GridBagConstraints red2 = new GridBagConstraints ();

red2 .gridx=0;
red2 .gridy=2;

red2 .gridheight=1;
red2 .gridwidth=1;

red2.fill=red2.BOTH;

red2 .weightx=1.0;
red2 .weighty=1.0;

red2.anchor=red2.WEST;

MC = new JButton ("MC");
button7 = new JButton("7");
button8 = new JButton ("8");
button9 = new JButton ("9");
backSlash = new JButton ("/");
sqrt = new JButton ("sqrt");

p2=new JPanel ();

p2.add(MC);
p2.add(button7);
p2.add(button8);
p2.add(button9);
p2.add(backSlash);
p2.add(sqrt);

windowContent.add(p2,red2);

GridBagConstraints red3 = new GridBagConstraints ();

red3 .gridx=0;
red2 .gridy=3;

red3 .gridwidth=1;
red3 .gridheight=1;

red3.fill=red3.BOTH;

red3.weightx=1.0;
red3.weighty=1.0;

red3.anchor=red3.CENTER;

MR = new JButton ("MR");
button4 = new JButton ("4");
button5 = new JButton ("5");
button6 = new JButton ("6");
multiply = new JButton ("*");
percent = new JButton ("%");

p3 = new JPanel ();

p3.add(MR);
p3.add(button4);
p3.add(button5);
p3.add(button6);
p3.add(multiply);
p3.add(percent);

windowContent.add(p3,red3);

GridBagConstraints red4 = new GridBagConstraints ();

red4 .gridx=0;
red4 .gridy=4;

red4. gridheight=1;
red4 .gridwidth=1;

red4.fill=red4.BOTH;

red4 .weightx=1.0;
red4 .weighty=1.0;

red4.anchor=red4.CENTER;

MS = new JButton ("MS");
button1 = new JButton ("1");
button2 = new JButton ("2");
button3 = new JButton ("3");
minus = new JButton ("-");
oneSlashX = new JButton ("1/x");

p4 = new JPanel ();

p4.add(MS);
p4.add(button1);
p4.add(button2);
p4.add(button3);
p4.add(minus);
p4.add(oneSlashX);

windowContent.add(p4,red4);

GridBagConstraints red5 = new GridBagConstraints ();

red5 .gridx=0;
red5 .gridy=5;

red5 .gridheight=1;
red5 .gridwidth=1;

red5.fill=red5.BOTH;

red5 .weightx=1.0;
red5 .weighty=1.0;

red5.anchor=red5.CENTER;

Mplus = new JButton ("M+");
button0 = new JButton ("0");
plusMinus = new JButton ("+/-");
plus = new JButton ("+");
equal = new JButton ("=");
point = new JButton (".");

p5 = new JPanel ();

p5.add(Mplus);
p5.add(button0);
p5.add(plusMinus);
p5.add(point);
p5.add(plus);
p5.add(equal);

windowContent.add(p5,red5);


JFrame frame =new JFrame ("Advanced Calculator");
frame.setContentPane(windowContent);

frame.pack();

frame.setVisible(true);


}

public static void main(String[] args) {
new AdvancedCalculator ();
}

}

Last edited by ikocijan; June 11th, 2012 at 07:19 AM..
Reply With Quote
  #6 (permalink)  
Old June 11th, 2012, 09:54 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Default

Hello,
How about adding a GridLayout to the panel containing the Backspace, CE and C buttons?
Quote:
p1.setLayout(new GridLayout(1,3));
Reply With Quote
The Following User Says Thank You to Cristina For This Useful Post:
ikocijan (June 11th, 2012)
  #7 (permalink)  
Old June 11th, 2012, 11:52 AM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Thanks

Thank you! That really helped me .
I have only two more questions . First, although my calculator looks pretty decent now it is nowhere near the calculator on picture 8-8 in the book. What I would like to know is how to add space between buttons and how to create empty box that is on the left next to backspace button.
And second, what doesthis line do
Code:
 frame.pack();
. I tried and it wouldn`t create the frame without it but what does it really do :D
Oh,and sorry about the code in my first post, now I figured out how to post code so that it`s easier to read :D

EDIT:found out what I should do to get some space between buttons:D

Last edited by ikocijan; June 11th, 2012 at 12:13 PM..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 8 - Try It Femi Sonuga BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 4 February 12th, 2012 08:44 AM
Lesson 13 Try it error alawode BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 4 January 27th, 2012 08:32 PM
Lesson 9 MTMike BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 3 November 3rd, 2011 01:51 PM
Lesson 8 sergeySE BOOK: Java Programming 24-Hour Trainer by Yakov Fain 1 June 29th, 2011 09:49 AM
Lesson 6 Try It zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 2 June 6th, 2011 10:07 PM



All times are GMT -4. The time now is 08:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.