Results 1 to 5 of 5
  1. #1
    powpowpenguin is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Question Exception in thread "main" java.lang.IllegalArgumentException: illegal component posi

    Hi , having a bit of a problem keep getting this error sorted out , can someone have a look at my code and help me find the problem

    Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
    at java.awt.Container.addImpl(Container.java:1035)
    at java.awt.Container.add(Container.java:408)
    at hangman_game.<init>(hangman_game.java:87)
    at hangman_game.main(hangman_game.java:327)
    Java Result: 1

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    
    public class hangman_game extends JFrame implements ActionListener
    {
    
    
        
    	Button Keypad[]= new Button[26];
    	char wordGuess;
    	String SrandomWord;
    	char[] wordChars;
    	StringBuffer randomWord = new StringBuffer();
    	boolean buttonClicked;
    	JLabel word = new JLabel("HangMan");
    	FlowLayout Flow = new FlowLayout(FlowLayout.CENTER);
    	JButton Restart = new JButton("Restart");
    	JButton addNewWord = new JButton("Add New Word");
    	JButton help = new JButton("Help");
    	JButton exit = new JButton("Exit");
    
    
    	public hangman_game()
    	{
                //all of the components are places and organised
    		Container con = getContentPane();
    		Panel keyPanel = new Panel();
    		Panel navPanel = new Panel();
    		Panel picture = new Panel();
    		ImageIcon pic1 = new ImageIcon("HangManPic1.jpg");
    		ImageIcon pic2 = new ImageIcon("HangManPic2.jpg");
    		ImageIcon pic3 = new ImageIcon("HangManPic3.jpg");
    		ImageIcon pic4 = new ImageIcon("HangManPic4.jpg");
    		ImageIcon pic5 = new ImageIcon("HangManPic5.jpg");
    		ImageIcon pic6 = new ImageIcon("HangManPic6.jpg");
    		keyPanel.setLayout(Flow);
    		navPanel.setLayout(Flow);
    		picture.setLayout(Flow);
    		con.setLayout(Flow);
    
    
    
                    //for loop which creates buttons
    		for (int i=0; i<26; i++){
    			Keypad[i] = new Button(String.valueOf(i));
    		}
    
    
    		//buttons are
                    navPanel.add(word);
    		for (int a=0; a < 26; a++)
                    {
    			keyPanel.add(Keypad[a]);
                    }
    
                    //futher buttons are added to the nav panel here
    			navPanel.add(word);
    			navPanel.add(Restart);
    			navPanel.add(addNewWord);
    			navPanel.add(help);
    			navPanel.add(exit);
    
                             //Pictures are added
    			JLabel pic = new JLabel(pic1);
    			picture.add(pic);
    
                   //listeners are added for thr letter of the alphabet
    		for (int i=0; i < 26; i++)
                    {
    			Keypad[i].addActionListener(this);
                    }
    
                            //listeners for the bottom buttons
    		Restart.addActionListener(this);
    		addNewWord.addActionListener(this);
    		help.addActionListener(this);
    		exit.addActionListener(this);
    
    		con.add(keyPanel, FlowLayout.CENTER);
    		con.add(picture, FlowLayout.CENTER);
    		con.add(navPanel, FlowLayout.CENTER);
    		setVisible(true);
    
    
    	}
                    
    		public void actionPerformed(ActionEvent e)
    		   {
    			buttonClicked = false;
    			for (int i=0; i<Keypad.length && !buttonClicked; i++)
    			{
    				if(e.getSource() == Keypad[i])
    				{
    				buttonClicked = true;
    				switch(i)
    	      		{case 0:
                                    wordGuess = 'A';
    				Keypad[i].setEnabled(false);
    				break;
    	      		case 1:
    	      			wordGuess  = 'B';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 2:
    	      			wordGuess = 'C';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 3:
    	      			wordGuess  = 'D';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 4:
    	      			wordGuess  = 'E';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 5:
    	      			wordGuess  = 'F';
    				Keypad[i].setEnabled(false);
    				break;
    	      		case 6:
    	      			wordGuess  = 'G';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 7:
    	      			wordGuess  = 'H';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 8:
    	      			wordGuess  = 'I';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 9:
    	      			wordGuess  = 'J';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 10:
    	      			wordGuess  = 'K';
    				Keypad[i].setEnabled(false);
    				break;
    	      		case 11:
    	      			wordGuess  = 'L';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 12:
    	      			wordGuess  = 'M';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 13:
    	      			wordGuess  = 'N';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 14:
    	      			wordGuess  = 'O';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 15:
    	      			wordGuess  = 'P';
    				Keypad[i].setEnabled(false);
    				break;
    	      		case 16:
    	      			wordGuess  = 'Q';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 17:
    	      			wordGuess  = 'R';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 18:
    	      			wordGuess  = 'S';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 19:
    	      			wordGuess  = 'T';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 20:
    	      			wordGuess  = 'U';
    				Keypad[i].setEnabled(false);
    				break;
    	      		case 21:
    	      			wordGuess = 'V';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 22:
    	      			wordGuess = 'W';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 23:
    	      			wordGuess  = 'X';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 24:
    	      			wordGuess  = 'Y';
    	      			Keypad[i].setEnabled(false);
    	      			break;
    	      		case 25:
    	      			wordGuess = 'Z';
    				Keypad[i].setEnabled(false);
    				break;
    	      		}
    					if(validateGuess(wordGuess, SrandomWord))
    					{
    						randomWord = insertValue(wordChars,wordGuess, randomWord);
    						SrandomWord = randomWord.toString();
    						word.setText(SrandomWord);
    					}
    					else
    					{
    						// Display next image
    					}
    				}
    				else if(e.getSource() == Restart)
    				{
    					buttonClicked = true;
    				}
    				else if(e.getSource() == addNewWord)
    				{
    					buttonClicked = true;
    				}
    				else if(e.getSource() == help)
    				{
    					buttonClicked = true;
    				}
    				else if(e.getSource() == exit)
    				{
    					buttonClicked = true;
    				}
    
    			}
    		   }
    
    
    
    	public void  fillArray(String[] array, int difficulty)
    	{
    		String file;
    		DataInputStream inputStream;
    
    		switch(difficulty)
    		{
    
    		default:
    			file = "Words.txt";
    		}
    			int i = 0;
    	while(array.length > -1)
    		 {
    
    		try{
    			inputStream = new DataInputStream(new FileInputStream(file));
    		array[i] = inputStream.readUTF();
    		}catch(IOException e){
    			System.exit(1);
    		}
    			i += 1;
    		}
    		 }
    
    	public String  randomWordPicker(String[] array)
    	{
    		double randomNumber;
    		float random;
    		int num;
    		randomNumber =Math.floor(Math.random()*11);
    		random = (float)randomNumber;
    		num = (int)random;
    
    		String word = array[num];
    		return word;
    
    
    	}
    
    	public void loadCharArray(char[] array,String word)
    	{
    		array = word.toCharArray();
    	}
    
    	public StringBuffer subWord(char[] array)
    	{
    		int Len = array.length;
    		StringBuffer temp = new StringBuffer("");
    		for(int i = 0; i != Len; i++)
    		{
    			temp.insert(i, "_");
    
    		}
    		return temp;
    	}
    
    	public boolean validateGuess(char c, String word)
    	{
    		int index = word.indexOf(c);
    		if(index == -1){
    			return false;
    		}
    		else{
    			return true;
    		}
    	}
    
    	public StringBuffer insertValue(char[] c, char Guess, StringBuffer display)
    
    	{
    		for(int i = 0; i < c.length; i++){
    			if(Guess ==c[i] )
    					{
    						display.setCharAt(i, c[i]);
    					}
    			else{}
    		}
    		return display;
    	}
    
    	public static void main(String[] args) {
    		hangman_game hang = new hangman_game();
    		hang.setTitle("Hang Man");
    		hang.pack();
    		hang.setSize(600, 500);
    		hang.setVisible(true);
    }
    
    }
    Help will be muuuuuuuuuuuuuuuch appreciated!!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.IllegalArgumentException: illegal component

    Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
    at java.awt.Container.addImpl(Container.java:1035)
    at java.awt.Container.add(Container.java:408)
    at hangman_game.<init>(hangman_game.java:87)
    What does the call to the add method at line 87 do that is illegal?
    If you can't tell, add a println to display all the values used on line 87 to see.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main" java.lang.IllegalArgumentException: illegal component

    I didn't know that a single LayoutManager could be used for several components ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    powpowpenguin is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.IllegalArgumentException: illegal component

    Thanks for the replies , I suspect it might be the layout manager didnt really think of that, unfortunately I already had to submit this as it was an assignment , I'll try fix it as soon as i have time either way though.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Exception in thread "main" java.lang.IllegalArgumentException: illegal component

    What Jos said. Layout managers aren't designed to be shared. Give each Container its own instance of FlowLayout, don't set the same instance to several Containers.

    Additionally, I recommend you go through Code Conventions for the Java(TM) Programming Language: Contents and learn that class names start with an uppercase letter, and method and variable names with a lowercase letter, and also that outdents should match indents.

    db

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. Replies: 6
    Last Post: 03-19-2011, 07:10 PM
  3. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  4. Replies: 12
    Last Post: 05-26-2009, 01:48 PM
  5. Replies: 1
    Last Post: 08-06-2007, 06:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •