GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Feb-2007, 00:40
stephano stephano is offline
New Member
 
Join Date: Feb 2007
Location: In the city!
Posts: 3
stephano is on a distinguished road

but... I thought I understood arrays!


Hi, I am new here. Both to the forum, and to Java. This is my first Java program, and I'm having issues with my arrays, of all things! Please note that I know this is spaghetti code, but I haven't had time to add "object orientation" to my program... I'm still working on understanding the stuff I'm calling.

The code below works, but as you read in the comments up top, if you change my arrays back to [5] and/or [5][2] respectively, i get an array runtime error.

Feel free to ask questions, and thank you all sooooo much in advance for helping. Also, feel free to comment on any other mistakes... I'm sure there are plenty.

I need more friends who speak java!

JAVA Code:

import java.util.*;
import java.awt.*;
import javax.swing.*;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;


public class MainWindow extends JFrame implements ActionListener
{
	//change arrays back to [5] (for JPanel, JLabel, and JTextField) and [5][2] for JButton
	//errors out when you reach the end of the array for some reason
	//click "create heat element" then click "start"... runtime error will come at end of array
	//
	
	Container c;
	
	JPanel[] myPanels = new JPanel[8];  
	JLabel[] myLabels = new JLabel[8];
	JTextField[] myTextFields = new JTextField[8];
	JButton[][] myButtons = new JButton[8][4];
	
	int numPanel;	//number of burners    !!!change name
	Integer count;		//timer counter
	
    public MainWindow() 
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		numPanel = 0;
		count = new Integer(0);
    }

	public void addDefaults()
	{
		c = getContentPane();
		
		//------------------------------create a buttons
		JButton b = new JButton("Create Heat Element");
		b.setActionCommand("Create");
		b.addActionListener(this);
		
		//------------------------------create a JPanel
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());

		//------------------------------add button to panel
        p.add(b, BorderLayout.SOUTH);

 		//------------------------------put the panel in the container
        c.setLayout(new BorderLayout());
        c.add(p, BorderLayout.SOUTH);

        pack();
        setVisible(true);
	}
	
	public void addHeatElement(int i)
	{
		myPanels[i] = new JPanel();
		c.add(myPanels[i], BorderLayout.EAST);
		
		myLabels[i] = new JLabel("Burner: " + i);
		myPanels[i].add(myLabels[i], BorderLayout.NORTH);
		
		myTextFields[i] = new JTextField("", 10);
		myPanels[i].add(myTextFields[i], BorderLayout.SOUTH);
		
		
		for(int j=0; j < 3 ; j++)
		{
			String buttonText = new String();
			String locationText = new String();
			switch(j)
			{
				case 0: buttonText="Start: " + j; 
						locationText="WEST"; 
						break;
				case 1: buttonText="Stop: " + j;
						locationText="CENTER";
						break;
				case 2: buttonText="Reset: " + j; 
						locationText="EAST";
						break;
			}
			myButtons[i][j] = new JButton(buttonText);
			myButtons[i][j].setActionCommand(buttonText);
			myButtons[i][j].addActionListener(this);
			myPanels[i].add(myButtons[i][j]);
		}
		
		pack();
	}

	public void actionPerformed(ActionEvent e) 
	{	
        if ("Create".equals(e.getActionCommand())) 
		{
			addHeatElement(numPanel);
			numPanel++;
        }
		else if("Start: 0".equals(e.getActionCommand()))
		{
			countDownTimer();
		}
		else if("Stop".equals(e.getActionCommand()))
		{
			//do stuff
		}
		else if("Reset".equals(e.getActionCommand()))
		{
			//do stuff
		}
    }

	public void countDownTimer()
	{
		int delay = 1000;   // delay for 1 sec.
	    int period = 1000;  // repeat every sec.
	    java.util.Timer timer = new java.util.Timer();

	    timer.scheduleAtFixedRate(new TimerTask() {
	            public void run() {
					myLabels[0].setText(count.toString());
					count++;
	            }
	        }, delay, period);
	}


}







public class TestFrame
{
	public static void main(String[] args)
	{
		MainWindow myWindow = new MainWindow();
		myWindow.addDefaults();
	}
}




  #2  
Old 22-Feb-2007, 03:27
anders anders is offline
New Member
 
Join Date: Feb 2007
Location: Sweden
Posts: 9
anders is on a distinguished road

Re: but... I thought I understood arrays!


Check the argument when calling addHeatElement().

addHeatElement(i) will use its argument to access myPanels[i], and if you have wrote
Code:
JPanel[] myPanels = new JPanel[8];
it won't work when you try to call addHeatElement(8 ), because that will try to set myPanels[8], and that doesn't exist.

Remember that index numbering starts with zero. The myPanels array as above has 8 elements: 0, 1, 2, 3, 4, 5, 6 and 7.
  #3  
Old 22-Feb-2007, 09:35
stephano stephano is offline
New Member
 
Join Date: Feb 2007
Location: In the city!
Posts: 3
stephano is on a distinguished road

Re: but... I thought I understood arrays!


Ok, here is a shorter explanation

JAVA Code:

//JButton[][] myButtons = new JButton[5][2]; (up top)

//for i=0 (pusing "had heating element" once)

public void addHeatElement(int i)
	{
		myPanels[i] = new JPanel();
		c.add(myPanels[i], BorderLayout.EAST);
		
		myLabels[i] = new JLabel("Burner: " + i);
		myPanels[i].add(myLabels[i], BorderLayout.NORTH);
		
		myTextFields[i] = new JTextField("", 10);
		myPanels[i].add(myTextFields[i], BorderLayout.SOUTH);
		
		
		for(int j=0; j < 3 ; j++)
		{
			String buttonText = new String();
			String locationText = new String();
			switch(j)
			{
				case 0: buttonText="Start: " + j; 
						locationText="WEST"; 
						break;
				case 1: buttonText="Stop: " + j;
						locationText="CENTER";
						break;
				case 2: buttonText="Reset: " + j; 
						locationText="EAST";
						break;
			}
			myButtons[i][j] = new JButton(buttonText);
			myButtons[i][j].setActionCommand(buttonText);
			myButtons[i][j].addActionListener(this);
			myPanels[i].add(myButtons[i][j]);
		}
		
		pack();
	}


It errors out because when J hits "2" i says arrayindexoutofbounds. I even added debugging code and watched J go in as 2... throwing the error.... strange (see full code above).

Thanks!
Last edited by stephano : 22-Feb-2007 at 09:57. Reason: clarification
  #4  
Old 22-Feb-2007, 10:56
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: but... I thought I understood arrays!


That's because when you declare:
JAVA Code:
JButton[][] myButtons = new JButton[5][2];

There exists only 2 in that array... index 0 and index 1. There is no index 2, since there are only 2 elements ( 0 and 1 ). So 2 is "out of bounds".

Does that make sense?

So you have:
myButtons[0][0], myButtons[0][1]
myButtons[1][0], myButtons[1][1]
etc.

There is no myButtons[0][2], that is out of bounds.

Did that help?
  #5  
Old 22-Feb-2007, 14:59
stephano stephano is offline
New Member
 
Join Date: Feb 2007
Location: In the city!
Posts: 3
stephano is on a distinguished road

Re: but... I thought I understood arrays!


Thank you!

I feel awful silly. I started counting from zero when I didn't need to.

Arrays sometimes hurt my head; esp. multi-dimensional arrays!

Thanks again!
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 16:54
Noob question on c arrays and functions brett C Programming Language 1 20-Apr-2005 04:59
Knight tour (arrays help needed) dilmv C++ Forum 7 18-Oct-2004 15:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.