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

Thread: what does this Java programming error mean?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default what does this Java programming error mean?

    I am coding a simple calculator in Eclipse version 4.3.1 and I get this message when I run the program:

    Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
    at java.awt.Container.checkAddToSelf(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at FunctionalCalculator.<init>(FunctionalCalculator.j ava:98)
    at FunctionalCalculator.main(FunctionalCalculator.jav a:152)


    here are the code:

    import javax.swing.*;
     
    	import java.awt.GridLayout;
     
     
    	import java.awt.BorderLayout;
     
     
     
     
    public class FunctionalCalculator{
     
    	JTextField Field = new JTextField(30);
    	JButton button0=new JButton("0");
    	JButton button1=new JButton("1");
    	JButton button2=new JButton("2");
    	JButton button3=new JButton("3");
     
    	JButton button4=new JButton("4");
    	JButton button5=new JButton("5");
    	JButton button6=new JButton("6");
    	JButton button7=new JButton("7");
    	JButton button8=new JButton("8");
    	JButton button9=new JButton("9");
    	JButton Minus=new JButton ("-");
    	JButton Add=new JButton ("+");
    	JButton Divide=new JButton ("/");
    	JButton Multiply=new JButton ("*");
     
    	JButton FullStop= new JButton(".");
    	JButton Equal= new JButton("=");
     
     
    	JPanel pl = new JPanel();
     
     
     
     
    		FunctionalCalculator() {
    	    //first line is written to assign windowContent into a new object JPanel().
     
     
    		BorderLayout bl = new BorderLayout();
     
     
    		pl.setLayout(bl);
     
     
    		pl.add("North", Field);
     
    		JPanel pl = new JPanel();
     
     
    		GridLayout grid = new GridLayout (4,3);
     
     
    		pl.setLayout(grid);
     
     
    		pl.add(button1);
    		pl.add(button2);
    		pl.add(button3);
    		pl.add(button4);
    		pl.add(button5);
    		pl.add(button6);
    		pl.add(button7);
    		pl.add(button8);
    		pl.add(button9);
    		pl.add(button0);
    		pl.add(FullStop);
    		pl.add(Equal);
     
     
    		pl.add("Center",pl);
     
     
    		JPanel part2= new JPanel();
    		GridLayout grid2 =new GridLayout(4,1);
    		part2.setLayout(grid2);
    		part2.add(Add);
    		part2.add(Minus);
    		part2.add(Multiply);
    		part2.add(Divide);
     
    		/*the new panel is added to the east (right)*/
    		pl.add("east",part2);
    		/*the panel is assigned with a title called calculator */
    		JFrame frame = new JFrame("Calculator");
     
     
    		frame.setContentPane(pl);
     
     
    		frame.pack();
     
     
    		frame.setVisible(true);
     
     
    		CalculatorFunctionality function= new CalculatorFunctionality(this);
     
    		button0.addActionListener(function);
    		button1.addActionListener(function);
    		button2.addActionListener(function);
    		button3.addActionListener(function);
    		button4.addActionListener(function);
    		button5.addActionListener(function);
    		button7.addActionListener(function);
    		button8.addActionListener(function);
    		button9.addActionListener(function);
     
     
    		FullStop.addActionListener(function);
    	    Add.addActionListener(function);
    		Minus.addActionListener(function);
    		Divide.addActionListener(function);
    		Multiply.addActionListener(function);
    		Equal.addActionListener(function);
     
    		}
     
     
    		public static void main(String[] args){
     
    			FunctionalCalculator calculator= new FunctionalCalculator();
     
     
     
    		}
    }

    here is the second code:

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import javax.swing.JButton;
     
    public class CalculatorFunctionality implements ActionListener {
     
    	FunctionalCalculator parent; 
     
     
    	char SelectedMath=' ';
     
     
    	double currentResult=0;
     
     
    	CalculatorFunctionality(FunctionalCalculator parent){
    	this.parent=parent;
    	}
     
     
    	public void actionPerformed(ActionEvent e){
     
    	JButton ClickedButton=(JButton) e.getSource();
     
    	String DispTextField = parent.Field.getText();
     
    	double DisplayValue=0;
     
    	if(!"".equals(DispTextField)){
    		DisplayValue= Double.parseDouble(DispTextField);		
     
    	}
    	//
    	Object src = e.getSource();
     
    	if(src==parent.Add){
    		SelectedMath='+';
    		currentResult=DisplayValue;
     
    		parent.Field.setText("");
    	}
     
    	else if(src==parent.Minus){
    		SelectedMath='-';
    		currentResult=DisplayValue;
    		//clears text box when user presses new button in calculator.
    		parent.Field.setText("");
    	}
     
    	else if(src==parent.Divide){
    		SelectedMath='/';
    		currentResult=DisplayValue;
     
    		parent.Field.setText("");
    	}
     
    	else if(src==parent.Multiply){
    		SelectedMath='*';
    		currentResult=DisplayValue;
     
    		parent.Field.setText("");
    	}
     
    	else if(src==parent.Equal){
     
    		if(SelectedMath=='+')
    		currentResult=DisplayValue;
    		parent.Field.setText(""+currentResult);
     
    	}
     
    	else if(SelectedMath=='-'){
    		currentResult=DisplayValue;
    		parent.Field.setText(""+currentResult);
    	}
     
    	else if(SelectedMath=='/'){
    		currentResult=DisplayValue;
    		parent.Field.setText(""+ currentResult);
    	}
     
    	else if(SelectedMath=='*'){
    		currentResult=DisplayValue;
    		parent.Field.setText(""+ currentResult);
    	}
     
    	else{
    		String ButtonLabelClicked= ClickedButton.getText();
     
    		parent.Field.setText(DispTextField + ButtonLabelClicked);
     
    	}
     
    	}
     
     
    	}


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
    		pl.add("Center",pl);
    This is certainly wrong. You can't add a component into itself.

    And also note, the add version:

    add(String name, Component comp)

    is obsolete, please use:

    add(Component comp, Object constraints)

    And for BorderLayout areas, don't use literal strings, instead use the declared constants, e.g. BorderLayout.NORTH
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    how do i change the pl.add("center",pl) to cos if i remove it then the buttons and components in the calculator is positioned incorrectly.

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    how do i change the pl.add("center",pl) to cos if i remove it then the buttons and components in the calculator is positioned incorrectly.
    I don't/can't know what are your expectations about the layout/appearance of your user interface .... clarify and explain please.

    (in other words: describe with words your UI)
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    calculator.jpg

    this is how the calculator should look like

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    calculator.jpg

    this is how the calculator should look like
    Firstly, you need a "container" with a BorderLayout. This container could be directly the content-pane of JFrame or a JPanel which in turn is into the content-pane .... in the end it's not very different.

    In the NORTH area you can put a JTextField and here we can't have doubts. In the CENTER area you need another container (JPanel) with another layout manager. If you use GridLayout you can have many JButton in grid, but note that they will have the same size. I see in the picture that the last column on the right has a smaller width. If you want this difference, GridLayout is not appropriate. You need for example GridBagLayout, however it is more complex than GridLayout.

    Evalutate what I have described, ask if necessary or if you have any further questions.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    calc without textbox.jpg

    i just need to insert the textbox at the top but I do not know how to. plz help

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    import javax.swing.*;
    //lays out a grid by row and column.
    //http://books.google.co.uk/books?id=kRuphQfpHioC&pg=PA388&dq=gridlayout+java& hl=en&sa=X&ei=O0HEUq3zAdSZ0QWxq4DoDw&ved=0CFgQ6AEw Bg#v=onepage&q=gridlayout%20java&f=false
    import java.awt.GridLayout;

    /*used to call the BorderLayout. Arranges layout by north, east, south and west.
    * North= top, east=right, south=bottom and west=left.
    * Sams Teach Yourself Java in 24 Hours (Covering Java 7 and Android) - Rogers Cadenhead - Google Books
    */
    import java.awt.BorderLayout;

    //event library used to call events to make the calculator button function


    public class FunctionalCalculator{

    JTextField Field = new JTextField(30);
    JButton button0=new JButton("0");
    JButton button1=new JButton("1");
    JButton button2=new JButton("2");
    JButton button3=new JButton("3");

    JButton button4=new JButton("4");
    JButton button5=new JButton("5");
    JButton button6=new JButton("6");
    JButton button7=new JButton("7");
    JButton button8=new JButton("8");
    JButton button9=new JButton("9");
    JButton Minus=new JButton ("-");
    JButton Add=new JButton ("+");
    JButton Divide=new JButton ("/");
    JButton Multiply=new JButton ("*");
    //buttonPoint is assigned with a full stop (.) and Equal is assigned with an equal sign
    //(=).
    JButton FullStop= new JButton(".");
    JButton Equal= new JButton("=");
    JPanel pl = new JPanel();
    //the variable p1 is set as a JPanel object.

    /*JPanel sets the panel of the calculator. JTextField to display the text field.
    * JButton to assign the buttons from 0 to 9 and the equal and decimal button. JPanel is
    * used organise the contents in the panel.
    * the names in the blue are the names in blue are the names assigned for each of the
    * buttons. */

    //calculator method
    FunctionalCalculator() {

    pl = new JPanel();
    BorderLayout bl = new BorderLayout ();
    pl.setLayout(bl);
    Field = new JTextField(30);
    pl.add("North", Field);
    //first line is written to assign windowContent into a new object JPanel().

    /*Second line
    * used to create a BorderLayout object with the variable name b1. b1 has been assigned
    * the BorderLayout layout for the calculator. The BorderLayout sets the content using
    * north, east, south, west and centre. north=top, east=right, south=down, west=left and
    * center=centre. */

    //windowContent (the panel) is assigned the BorderLayout layout.
    pl.setLayout(bl);

    /*assigns the TextBox into an object named JTextField() with the size 30.*/
    BorderLayout fld = new BorderLayout();
    pl.setLayout(fld);
    pl.add(Field);

    /*the TextBox is set at the top of the panel.*/




    //button0 to button9 are assigned with the numbers from 0 to 9.


    //the variable p1 is set as a JPanel object.
    JPanel pl = new JPanel();

    /*the variable g1 is assigned with a GridLayout with the column 4 and row 3.*/
    GridLayout grid = new GridLayout (4,1);

    //the JPanel assigned variable p1 been set the GridLayout (row by column).
    pl.setLayout(grid);

    /*the contents are assigned to the calculator panel.*/

    pl.add(button1);
    pl.add(button2);
    pl.add(button3);
    pl.add(Add);
    pl.add(button4);
    pl.add(button5);
    pl.add(button6);
    pl.add(Minus);
    pl.add(button7);
    pl.add(button8);
    pl.add(button9);
    pl.add(Multiply);
    pl.add(button0);
    pl.add(FullStop);
    pl.add(Equal);
    pl.add(Divide);

    /*the contents within the variable p1 are added to the panel and are displayed at the
    * centre*/


    /**/

    /*the new panel is added to the east (right)*/

    /*the panel is assigned with a title called calculator */
    JFrame frame = new JFrame("Calculator");

    //adds the components within windowContent (see above for contents within it)
    //inside the calculator panel.
    //http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html (reference link)
    frame.setContentPane(pl);

    //pack() is used to automatically set the size of the panel to fit the contents within it.
    //found this out by removing the code.
    frame.pack();

    //allows the calculator to be viewable instead of being invisible.
    frame.setVisible(true);


    CalculatorFunctionality function= new CalculatorFunctionality(this);

    button0.addActionListener(function);
    button1.addActionListener(function);
    button2.addActionListener(function);
    button3.addActionListener(function);
    button4.addActionListener(function);
    button5.addActionListener(function);
    button7.addActionListener(function);
    button8.addActionListener(function);
    button9.addActionListener(function);


    FullStop.addActionListener(function);
    Add.addActionListener(function);
    Minus.addActionListener(function);
    Divide.addActionListener(function);
    Multiply.addActionListener(function);
    Equal.addActionListener(function);

    }


    public static void main(String[] args){

    FunctionalCalculator calculator= new FunctionalCalculator();



    }
    }


    i still cant seem to add the textbox. plz help

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

    Default Re: what does this Java programming error mean?

    Welcome! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

  10. #10
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    import javax.swing.*;
    //lays out a grid by row and column.
    	//http://books.google.co.uk/books?id=kRuphQfpHioC&pg=PA388&dq=gridlayout+java&hl=en&sa=X&ei=O0HEUq3zAdSZ0QWxq4DoDw&ved=0CFgQ6AEwBg#v=onepage&q=gridlayout%20java&f=false
    	import java.awt.GridLayout;
     
    /*used to call the BorderLayout. Arranges layout by north, east, south and west.
     *  North= top, east=right, south=bottom and west=left.
     *  [url=http://books.google.co.uk/books?id=Mog8BYlK1fQC&pg=PA190&dq=borderlayout+java&hl=en&sa=X&ei=bEDEUravMseb0wXhroD4Ag&ved=0CDcQ6AEwAQ#v=onepage&q=borderlayout%20java&f=false]Sams Teach Yourself Java in 24 Hours (Covering Java 7 and Android) - Rogers Cadenhead - Google Books[/url]
     */
    	import java.awt.BorderLayout;
     
    	//event library used to call events to make the calculator button function
     
     
    public class FunctionalCalculator{
     
    	JTextField Field = new JTextField(30);
    	JButton button0=new JButton("0");
    	JButton button1=new JButton("1");
    	JButton button2=new JButton("2");
    	JButton button3=new JButton("3");
     
    	JButton button4=new JButton("4");
    	JButton button5=new JButton("5");
    	JButton button6=new JButton("6");
    	JButton button7=new JButton("7");
    	JButton button8=new JButton("8");
    	JButton button9=new JButton("9");
    	JButton Minus=new JButton ("-");
    	JButton Add=new JButton ("+");
    	JButton Divide=new JButton ("/");
    	JButton Multiply=new JButton ("*");
    	//buttonPoint is assigned with a full stop (.) and Equal is assigned with an equal sign
    	//(=).
    	JButton FullStop= new JButton(".");
    	JButton Equal= new JButton("=");
    	JPanel pl = new JPanel();
    	JPanel text=new JPanel();
     
    	//the variable p1 is set as a JPanel object.
     
    		/*JPanel sets the panel of the calculator. JTextField to display the text field.
    		 * JButton to assign the buttons from 0 to 9 and the equal and decimal button. JPanel is 
    		 * used organise the contents in the panel.
    		 * the names in the blue are the names in blue are the names assigned for each of the 
    		 * buttons.  */
     
    		//calculator method
    		FunctionalCalculator() {
     
     
     
     
    		/*Second line 
    		 * used to create a BorderLayout object with the variable name b1. b1 has been assigned
    		 * the BorderLayout layout for the calculator. The BorderLayout sets the content using
    		 * north, east, south, west and centre. north=top, east=right, south=down, west=left and
    		 * center=centre. */
    			BorderLayout bl = new BorderLayout();
    		//windowContent (the panel) is assigned the BorderLayout layout.
    		pl.setLayout(bl);
     
    		/*assigns the TextBox into an object named JTextField() with the size 30.*/
     
    		/*the TextBox is set at the top of the panel.*/
    		JPanel text= new JPanel();
    		BorderLayout textlay= new BorderLayout();
     
    		text.setLayout(textlay);
     
    		text.add("North",Field);
     
    		//button0 to button9 are assigned with the numbers from 0 to 9.
     
     
    		//the variable p1 is set as a JPanel object.
    		JPanel pl = new JPanel();
     
    		/*the variable g1 is assigned with a GridLayout with the column 4 and row 3.*/
    		GridLayout grid = new GridLayout (4,1);
     
    		//the JPanel assigned variable p1 been set the GridLayout (row by column).
    		pl.setLayout(grid);
     
    		/*the contents are assigned to the calculator panel.*/
     
    		pl.add(button1);
    		pl.add(button2);
    		pl.add(button3);
    		pl.add(Add);
    		pl.add(button4);
    		pl.add(button5);
    		pl.add(button6);
    		pl.add(Minus);
    		pl.add(button7);
    		pl.add(button8);
    		pl.add(button9);
    		pl.add(Multiply);
    		pl.add(button0);
    		pl.add(FullStop);
    		pl.add(Equal);
    		pl.add(Divide);
     
    		/*the contents within the variable p1 are added to the panel and are displayed at the 
    		 * centre*/
     
     
    		/**/
     
    		/*the new panel is added to the east (right)*/
     
    		/*the panel is assigned with a title called calculator */
    		JFrame frame = new JFrame("Calculator");
     
    		//adds the components within windowContent (see above for contents within it) 
    		//inside the calculator panel.
    		//http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html (reference link)
    		frame.setContentPane(pl);
     
    		//pack() is used to automatically set the size of the panel to fit the contents within it.
    		//found this out by removing the code.
    		frame.pack();
     
    		//allows the calculator to be viewable instead of being invisible.
    		frame.setVisible(true);
     
     
    		CalculatorFunctionality function= new CalculatorFunctionality(this);
     
    		button0.addActionListener(function);
    		button1.addActionListener(function);
    		button2.addActionListener(function);
    		button3.addActionListener(function);
    		button4.addActionListener(function);
    		button5.addActionListener(function);
    		button7.addActionListener(function);
    		button8.addActionListener(function);
    		button9.addActionListener(function);
     
     
    		FullStop.addActionListener(function);
    	    Add.addActionListener(function);
    		Minus.addActionListener(function);
    		Divide.addActionListener(function);
    		Multiply.addActionListener(function);
    		Equal.addActionListener(function);
     
    		}
     
     
    		public static void main(String[] args){
     
    			FunctionalCalculator calculator= new FunctionalCalculator();
     
     
     
    		}
    }

    i tried modifying the code but I still could not get the text box to appear on the calculator.
    Here is the image of how the calculator:
    calc without textbox.jpg

    if the text box is set at the top if the buttons, then my calculator should be fixed. plz help

  11. #11
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    pl = new JPanel();
    BorderLayout bl = new BorderLayout ();
    pl.setLayout(bl);
    Field = new JTextField(30);
    pl.add("North", Field);
     
    pl.setLayout(bl);
     
    BorderLayout fld = new BorderLayout();
    pl.setLayout(fld);
    pl.add(Field);
     
    JPanel pl = new JPanel();
     
    GridLayout grid = new GridLayout (4,1);
     
    pl.setLayout(grid);
     
     
    pl.add(button1);
    pl.add(button2);
    pl.add(button3);
    pl.add(Add);
    pl.add(button4);
    pl.add(button5);
    pl.add(button6);
    pl.add(Minus);
    pl.add(button7);
    pl.add(button8);
    pl.add(button9);
    pl.add(Multiply);
    pl.add(button0);
    pl.add(FullStop);
    pl.add(Equal);
    pl.add(Divide);
     
    JFrame frame = new JFrame("Calculator");
     
    frame.setContentPane(pl);
    Sorry, but the code I see here is very confused and with useless things.

    First, you set 3 times a BorderLayout on pl. You added 2 times Field in pl, one time in "North" (before, I have told you not to use that add version and not to use literal strings like "North") and the second time with no constraint (which means: put in the center area!).

    Then you defined a local variable pl with the same name of the instance variable (bad choice) and you set on this a GridLayout.
    And the worst part is that you set this last pl (the grid) as the new content pane, vanishing all the rest you have done.

    Try to be more accurate, clear and think carefully each step. As long as you write code so confused .... you will not go very far ...
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  12. #12
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    import javax.swing.*;
    //lays out a grid by row and column.
    	//http://books.google.co.uk/books?id=kRuphQfpHioC&pg=PA388&dq=gridlayout+java&hl=en&sa=X&ei=O0HEUq3zAdSZ0QWxq4DoDw&ved=0CFgQ6AEwBg#v=onepage&q=gridlayout%20java&f=false
    	import java.awt.GridLayout;
     
    /*used to call the BorderLayout. Arranges layout by north, east, south and west.
     *  North= top, east=right, south=bottom and west=left.
     *  [url=http://books.google.co.uk/books?id=Mog8BYlK1fQC&pg=PA190&dq=borderlayout+java&hl=en&sa=X&ei=bEDEUravMseb0wXhroD4Ag&ved=0CDcQ6AEwAQ#v=onepage&q=borderlayout%20java&f=false]Sams Teach Yourself Java in 24 Hours (Covering Java 7 and Android) - Rogers Cadenhead - Google Books[/url]
     */
    	import java.awt.BorderLayout;
     
    	//event library used to call events to make the calculator button function
     
     
    public class FunctionalCalculator{
     
    	JTextField Field = new JTextField(30);
    	JButton button0=new JButton("0");
    	JButton button1=new JButton("1");
    	JButton button2=new JButton("2");
    	JButton button3=new JButton("3");
     
    	JButton button4=new JButton("4");
    	JButton button5=new JButton("5");
    	JButton button6=new JButton("6");
    	JButton button7=new JButton("7");
    	JButton button8=new JButton("8");
    	JButton button9=new JButton("9");
    	JButton Minus=new JButton ("-");
    	JButton Add=new JButton ("+");
    	JButton Divide=new JButton ("/");
    	JButton Multiply=new JButton ("*");
    	//buttonPoint is assigned with a full stop (.) and Equal is assigned with an equal sign
    	//(=).
    	JButton FullStop= new JButton(".");
    	JButton Equal= new JButton("=");
    	JPanel pl = new JPanel();
    	JPanel text=new JPanel();
     
    	//the variable p1 is set as a JPanel object.
     
    		/*JPanel sets the panel of the calculator. JTextField to display the text field.
    		 * JButton to assign the buttons from 0 to 9 and the equal and decimal button. JPanel is 
    		 * used organise the contents in the panel.
    		 * the names in the blue are the names in blue are the names assigned for each of the 
    		 * buttons.  */
     
    		//calculator method
    		FunctionalCalculator() {
     
     
     
     
    		/*Second line 
    		 * used to create a BorderLayout object with the variable name b1. b1 has been assigned
    		 * the BorderLayout layout for the calculator. The BorderLayout sets the content using
    		 * north, east, south, west and centre. north=top, east=right, south=down, west=left and
    		 * center=centre. */
    			BorderLayout bl = new BorderLayout();
    		//windowContent (the panel) is assigned the BorderLayout layout.
    		pl.setLayout(bl);
     
    		/*assigns the TextBox into an object named JTextField() with the size 30.*/
     
    		/*the TextBox is set at the top of the panel.*/
    		JPanel text= new JPanel();
    		BorderLayout textlay= new BorderLayout();
     
    		text.setLayout(textlay);
     
    		text.add("North",Field);
     
    		//button0 to button9 are assigned with the numbers from 0 to 9.
     
     
    		//the variable p1 is set as a JPanel object.
    		JPanel pl = new JPanel();
     
    		/*the variable g1 is assigned with a GridLayout with the column 4 and row 3.*/
    		GridLayout grid = new GridLayout (4,1);
     
    		//the JPanel assigned variable p1 been set the GridLayout (row by column).
    		pl.setLayout(grid);
     
    		/*the contents are assigned to the calculator panel.*/
     
    		pl.add(button1);
    		pl.add(button2);
    		pl.add(button3);
    		pl.add(Add);
    		pl.add(button4);
    		pl.add(button5);
    		pl.add(button6);
    		pl.add(Minus);
    		pl.add(button7);
    		pl.add(button8);
    		pl.add(button9);
    		pl.add(Multiply);
    		pl.add(button0);
    		pl.add(FullStop);
    		pl.add(Equal);
    		pl.add(Divide);
     
    		/*the contents within the variable p1 are added to the panel and are displayed at the 
    		 * centre*/
     
     
    		/**/
     
    		/*the new panel is added to the east (right)*/
     
    		/*the panel is assigned with a title called calculator */
    		JFrame frame = new JFrame("Calculator");
     
    		//adds the components within windowContent (see above for contents within it) 
    		//inside the calculator panel.
    		//http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html (reference link)
    		frame.setContentPane(pl);
     
    		//pack() is used to automatically set the size of the panel to fit the contents within it.
    		//found this out by removing the code.
    		frame.pack();
     
    		//allows the calculator to be viewable instead of being invisible.
    		frame.setVisible(true);
     
     
    		CalculatorFunctionality function= new CalculatorFunctionality(this);
     
    		button0.addActionListener(function);
    		button1.addActionListener(function);
    		button2.addActionListener(function);
    		button3.addActionListener(function);
    		button4.addActionListener(function);
    		button5.addActionListener(function);
    		button7.addActionListener(function);
    		button8.addActionListener(function);
    		button9.addActionListener(function);
     
     
    		FullStop.addActionListener(function);
    	    Add.addActionListener(function);
    		Minus.addActionListener(function);
    		Divide.addActionListener(function);
    		Multiply.addActionListener(function);
    		Equal.addActionListener(function);
     
    		}
     
     
    		public static void main(String[] args){
     
    			FunctionalCalculator calculator= new FunctionalCalculator();
     
     
     
    		}
    }


    --- Update ---

    this is the newly updated code (above)

  13. #13
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    		JPanel pl = new JPanel();
     
    		GridLayout grid = new GridLayout (4,1);
     
    		pl.setLayout(grid);
     
    		pl.add(button1);
    		pl.add(button2);
    		pl.add(button3);
    		pl.add(Add);
    		pl.add(button4);
    		pl.add(button5);
    		pl.add(button6);
    		pl.add(Minus);
    		pl.add(button7);
    		pl.add(button8);
    		pl.add(button9);
    		pl.add(Multiply);
    		pl.add(button0);
    		pl.add(FullStop);
    		pl.add(Equal);
    		pl.add(Divide);
     
    		JFrame frame = new JFrame("Calculator");
     
    		frame.setContentPane(pl);
    Did you understand that this 'pl', being set as the content pane is the only thing visible in the frame?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  14. #14
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    i tried adding another JFrame for the textbox but that did not work. because i seem to only make one visible with JFrame and not both.

  15. #15
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    i tried adding another JFrame for the textbox but that did not work. because i seem to only make one visible with JFrame and not both.
    Listen, try to understand and follow these macro-steps:

    1) Create a JTextField, named e.g. inputField.
    2) Create a JPanel, named e.g. buttonsPanel, set a GridLayout on it and add all the N buttons.
    3) Get the content pane and add the inputField in north. Use the general form:
    frame.getContentPane().add(theComponent, BorderLayout.XYZ);
    (XYZ one of NORTH, etc)
    4) Get the content pane and add the buttonsPanel in center.

    Or as alternative:

    1) / 2) same as above
    3) Create a second JPanel, named e.g. containerPanel, set a BorderLayout on it.
    4) Add inputField in north of containerPanel.
    5) Add buttonsPanel in center of containerPanel.
    6) Get the content pane and add the containerPanel in center OR, better, set containerPanel as the content pane.

    At your choice.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  16. #16
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    it does not work, will you be able to show me a code to write and try. Cos I tried your method as how I understood it and it did not seem to work.
    import javax.swing.*;
    //lays out a grid by row and column.
    	//http://books.google.co.uk/books?id=kRuphQfpHioC&pg=PA388&dq=gridlayout+java&hl=en&sa=X&ei=O0HEUq3zAdSZ0QWxq4DoDw&ved=0CFgQ6AEwBg#v=onepage&q=gridlayout%20java&f=false
    	import java.awt.GridLayout;
     
    /*used to call the BorderLayout. Arranges layout by north, east, south and west.
     *  North= top, east=right, south=bottom and west=left.
     *  [url=http://books.google.co.uk/books?id=Mog8BYlK1fQC&pg=PA190&dq=borderlayout+java&hl=en&sa=X&ei=bEDEUravMseb0wXhroD4Ag&ved=0CDcQ6AEwAQ#v=onepage&q=borderlayout%20java&f=false]Sams Teach Yourself Java in 24 Hours (Covering Java 7 and Android) - Rogers Cadenhead - Google Books[/url]
     */
    	import java.awt.BorderLayout;
     
    	//event library used to call events to make the calculator button function
     
     
    public class FunctionalCalculator {
     
    	JTextField Field = new JTextField(30);
    	JButton button0=new JButton("0");
    	JButton button1=new JButton("1");
    	JButton button2=new JButton("2");
    	JButton button3=new JButton("3");
     
    	JButton button4=new JButton("4");
    	JButton button5=new JButton("5");
    	JButton button6=new JButton("6");
    	JButton button7=new JButton("7");
    	JButton button8=new JButton("8");
    	JButton button9=new JButton("9");
    	JButton Minus=new JButton ("-");
    	JButton Add=new JButton ("+");
    	JButton Divide=new JButton ("/");
    	JButton Multiply=new JButton ("*");
    	//buttonPoint is assigned with a full stop (.) and Equal is assigned with an equal sign
    	//(=).
    	JButton FullStop= new JButton(".");
    	JButton Equal= new JButton("=");
    	JPanel pl = new JPanel();
     
    	//the variable p1 is set as a JPanel object.
     
    		/*JPanel sets the panel of the calculator. JTextField to display the text field.
    		 * JButton to assign the buttons from 0 to 9 and the equal and decimal button. JPanel is 
    		 * used organise the contents in the panel.
    		 * the names in the blue are the names in blue are the names assigned for each of the 
    		 * buttons.  */
     
    		//calculator method
    		public FunctionalCalculator() {
     
     
     
     
    		/*Second line 
    		 * used to create a BorderLayout object with the variable name b1. b1 has been assigned
    		 * the BorderLayout layout for the calculator. The BorderLayout sets the content using
    		 * north, east, south, west and centre. north=top, east=right, south=down, west=left and
    		 * center=centre. */
    			BorderLayout bl = new BorderLayout(4,1);
    		//windowContent (the panel) is assigned the BorderLayout layout.
    		pl.setLayout(bl);
     
    		/*assigns the TextBox into an object named JTextField() with the size 30.*/
     
    		/*the TextBox is set at the top of the panel.*/
     
     
    		//button0 to button9 are assigned with the numbers from 0 to 9.
     
     
    		//the variable p1 is set as a JPanel object.
    		JPanel pl = new JPanel();
     
    		/*the variable g1 is assigned with a GridLayout with the column 4 and row 3.*/
    		GridLayout grid = new GridLayout (4,1);
     
     
     
     
     
    		//the JPanel assigned variable p1 been set the GridLayout (row by column).
     
     
    		/*the contents within the variable p1 are added to the panel and are displayed at the 
    		 * centre*/
     
     
     
    		pl.setLayout(grid);
     
    		/*the contents are assigned to the calculator panel.*/
     
    		pl.add(button1);
    		pl.add(button2);
    		pl.add(button3);
    		pl.add(Add);
    		pl.add(button4);
    		pl.add(button5);
    		pl.add(button6);
    		pl.add(Minus);
    		pl.add(button7);
    		pl.add(button8);
    		pl.add(button9);
    		pl.add(Multiply);
    		pl.add(button0);
    		pl.add(FullStop);
    		pl.add(Equal);
    		pl.add(Divide);
    		/**/
     
    		/*the new panel is added to the east (right)*/
     
    		/*the panel is assigned with a title called calculator */
    		JFrame frame = new JFrame("Calculator");
     
    	    frame.setContentPane(pl);
     
     
     
    		//adds the components within windowContent (see above for contents within it) 
    		//inside the calculator panel.
    		//http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html (reference link)
     
    		frame.setContentPane(pl);
     
     
    		//pack() is used to automatically set the size of the panel to fit the contents within it.
    		//found this out by removing the code.
    		frame.pack();
     
     
     
    		//allows the calculator to be viewable instead of being invisible.
    		frame.setVisible(true);
     
     
     
     
    		//calls the codes in CalculatorFunctionality to be used on this class. Also 
    		//Also 
    		CalculatorFunctionality function= new CalculatorFunctionality(this);
     
    		button0.addActionListener(function);
    		button1.addActionListener(function);
    		button2.addActionListener(function);
    		button3.addActionListener(function);
    		button4.addActionListener(function);
    		button5.addActionListener(function);
    		button7.addActionListener(function);
    		button8.addActionListener(function);
    		button9.addActionListener(function);
     
     
    		FullStop.addActionListener(function);
    	    Add.addActionListener(function);
    		Minus.addActionListener(function);
    		Divide.addActionListener(function);
    		Multiply.addActionListener(function);
    		Equal.addActionListener(function);
     
    		}
     
     
    		public static void main(String[] args){
     
    			FunctionalCalculator calculator= new FunctionalCalculator();
     
     
     
    		}
    }


    --- Update ---

    if this text box is sorted, then I can move on to making the calculator function as best as I possibly could. I feel irritated that I cannot seem to figure out how to fix this problem. I even tried to create a new JPanel seperately for the text box but unfortunately that does not work.

  17. #17
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    I tried your method as how I understood it
    And then it means that you have not understood ..... From the last code I see, you have not done any progress, you are still using the same ugly names (e.g. pl as instance variable and also as local variable) and now there are also 2 frame.setContentPane(pl);, one is obviously useless. And you have not done any of the things I said.

    It seems you are very confused. Please take a big breathe, remove all things you have done (or start creating a new class), and try one of the options I have described, preferably the first that is more simple:

    1) Create a JTextField, named e.g. inputField.
    2) Create a JPanel, named e.g. buttonsPanel, set a GridLayout on it and add all the N buttons.
    3) Get the content pane and add the inputField in north. Use the general form:
    frame.getContentPane().add(theComponent, BorderLayout.XYZ);
    (XYZ one of NORTH, etc)
    4) Get the content pane and add the buttonsPanel in center.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  18. The Following User Says Thank You to andbin For This Useful Post:

    gopster01 (January 3rd, 2014)

  19. #18
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what does this Java programming error mean?

    what do you mean 'XYZ of NORTH'? do you means the XYZ coordinates?

    --- Update ---

    i don't get the third part, could you explain that more clearly, plz.

    --- Update ---

    //to assign 
    import java.awt.*;
    //
    import javax.swing.*;
     
     
    public class Calculator {
    //sets the frame which is the window of the calculator. Where the buttons and the contents are
    //presented. Basically its the background box. This frame is assigned the name "Calculator".
    JFrame Frame = new JFrame("Calculator");	
    //sets the textbox for the calculator. the maximum field size is 30.
    JTextField inPutText = new JTextField(30);
    //this JPanel is created to seperately to add the buttons uniquely on its on section.
    //its almost like a graphical variable to store the buttons.
    JPanel buttonPanel = new JPanel();
    //number buttons
    JButton btn1 = new JButton("1");
    JButton btn2 = new JButton("2");
    JButton btn3 = new JButton("3");
    JButton btn4 = new JButton("4");
    JButton btn5 = new JButton("5");
    JButton btn6 = new JButton("6");
    JButton btn7 = new JButton("7");
    JButton btn8 = new JButton("8");
    JButton btn9 = new JButton("9");
    JButton btn0 = new JButton("0");
    //math buttons
    JButton btnPlus = new JButton("+");
    JButton btnMinus = new JButton("-");
    JButton btnMultiply = new JButton("*");
    JButton btnDivide = new JButton("/");
    //full stop and equal button.
    JButton btnEqual = new JButton("=");
    JButton btnBulletPoint = new JButton(".");
     
     
    public Calculator(){
    	//sets a grid layout for the calculator 4 by 1.
    	GridLayout buttonBorder = new GridLayout(4,1);
    	//sets the fram visible to view the calculator itself.
    	Frame.setVisible(true);
    	//sets the frame size by 200 by 200.
    	Frame.setSize(200,200);
    	//sets the button panel visible to view the buttons.
    	buttonPanel.setVisible(true);
    	//the button panel is assigned witt grid layout.
    	buttonPanel.setLayout(buttonBorder);
    	//added the buttons onto the Frame (Window)
    	 Frame.add(buttonPanel);
    	//assigning the buttons to the Frame using the panel made for the buttons.
    	buttonPanel.add(btn1);
    	buttonPanel.add(btn2);
    	buttonPanel.add(btn3);
    	buttonPanel.add(btnPlus);
    	buttonPanel.add(btn4);
    	buttonPanel.add(btn5);
    	buttonPanel.add(btn6);
    	buttonPanel.add(btnMinus);
    	buttonPanel.add(btn7);
    	buttonPanel.add(btn8);
    	buttonPanel.add(btn9);
    	buttonPanel.add(btnMultiply);
    	buttonPanel.add(btn0);
    	buttonPanel.add(btn0);
    	buttonPanel.add(btnBulletPoint);
    	buttonPanel.add(btnEqual);
    	buttonPanel.add(btnDivide);
     
     
    	//assigning the text box on the Frame itself at the top of the box.
    	Frame.add(inPutText,BorderLayout.NORTH);
     
     
    	//public Calculator end bracket	
     
    }
    	public static void main(String[] args)
    	{
    		//
    		Calculator calc = new Calculator();
     
     
    //end of main 
    	}
    //end of class Calculator.	
    }

    i managed to lay it out successfully.

    --- Update ---

    thanks for having patience in me and helping me out andbin. You have really helped me out.
    thank you.

  20. #19
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what does this Java programming error mean?

    Quote Originally Posted by gopster01 View Post
    what do you mean 'XYZ of NORTH'? do you means the XYZ coordinates?
    XYZ .... one of NORTH, SOUTH etc in BorderLayout.
    Please, see and always keep handy the javadoc documentation!

    BorderLayout (Java Platform SE 7 )

    Quote Originally Posted by gopster01 View Post
    i managed to lay it out successfully.
    Your code is still a bit "confused" and not well written but the end result is what you wanted. However it can even be improved.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Polymorphism Java Programming HELPP P.S. I am a beginner in Java Programming
    By judemartin99 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2013, 02:21 PM
  2. Linked Lists Java Programming HELPP P.S. I am a beginner in Java Programming
    By judemartin99 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 20th, 2013, 02:19 PM
  3. [SOLVED] Easy error to solve in a minute. Help please, I don't want to fail Java Programming again.
    By ihatejava in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2012, 04:30 PM
  4. Blackjack programming error (programmer is new to programming)
    By JSingh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 09:13 PM
  5. java programming error
    By priya_501 in forum Member Introductions
    Replies: 2
    Last Post: May 3rd, 2011, 11:15 AM

Tags for this Thread