Java GUI programming help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Java GUI programming help

  1. #1
    Join Date
    Jan 2003
    Posts
    2

    Java GUI programming help

    Hi,

    I have a problem that is pissing me off. I am suppose to make this gui program where the user specifies an array's size and after, the user is in charge of inserting integers into the array.

    I already have two input fields and two buttons. One input field gets the array's specified size and the button sets and initializes the array's size. The other input field takes the integers that will be stored in the array and the button stores those integers into the array after the button is pressed consecutively.

    How do I make the insertData button refer to the same array that was specified when the setLength button is pressed?

    Thanks

    here is the code....

    Code:
    	public void buttonClicked (JButton buttonObj)
    	{
    		if(buttonObj == setLength)
    		{
    			int a = input.getNumber();
    			int array1[] = new int [a];
    			if(a < 0)
    			{
    				messageBox("You did not enter a positive number");
    			}
    			else
    			{
    				messageBox("The array's physical size is now " + array1.length + ".");
    			}
    		
    		}
    		else if(buttonObj == insertData)
    		{
    			int b = input.getNumber();
    			int c = input1.getNumber();
    			int i = 0;
    			if(c < 0)
    			{
    				messageBox("You did not enter a positive integer");
    			}
    			else
    			{
    			array1[i] = c;
    			i++;
    			System.out.println(array1[i]);	 
    			if(i == array1.length)
    			{
    				messageBox("SORRY: Array is full");
    			}
    			}
    		}

  2. #2
    Join Date
    Nov 2002
    Posts
    138
    it would refer to the same array only, i think, the data within the array will be reset once you specify another length. if the button, once pressed, creates/intializes a new array with this size, then....that's a new array. why don't you use a vector? it's more manageable since you don't have to worry about it's length. it's just like an array, only, it's dynamic. regular arrays are not dynamic. once they are initialized, you can't resize it without reinitializing it. if you really need to use arrays, the you'll have to figure something out...right now my mind is ................... yes, BLANK. hehehe.... =)

  3. #3
    Join Date
    Nov 2002
    Posts
    138
    why would you want to resize the array anyway, when you are not yet done filling it with data? if you need to add more into the array and it's already full, i suggest you make another array for this one. then add this to the first array.

    i didn't look at your code, silly me, but you are wondering why it "seems" once you hit the set length button it's not referring to the same array. well, just like i said above, it is the same array, but the button once hit does a -- int array1[] = new int [a]; -- so you just made a new array and the old one gets discarded since you are using the same variable for this. that's just like using the same filename... it gets overwritten.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center