Ok, just need some guidance on this, I have an array and a few text fields but am unsure about how to actually insert the text from the fields into an array when the ok button is pressed . I thought I would have to use "getInputValue()" and create a method which was called on the ok button action event but cant seem to get it working. If anyone could point me in the right direction it would be great.

Also, on an unrelated question - is it an easy process to have 3 checkboxes - but for only one to be allowed checked at a time?

Thanks...

Recommended Answers

All 5 Replies

Ok, just an update - not sure how correct this is, but I have created an array, set my Textfields so I can get the values entered but now I am unsure of how I go about inserting these into the array and what is the best way to view the contents of the array through the GUI when its all in?

private void readtext(){
        Adult MyArray[] = new Adult[ 5 ];
        Adult memAd = new Adult (0,"","","","","");
        int counter = 0;
		
        String name = NameTextField.getText();
        String date = DateTextField.getText();
        String mobnum = MobNumTextField.getText();
        String homnum = HomeNumTextField.getText();
        
        memAd.setMemName(NameTextField.getText());
        
        counter = counter + 1;
	MyArray[counter] = memAd;


    }

Yoy seem to be on the right track here - copy the data fromn the TextFields into the fields of your Adult object (using the set... methods, or just putting all the values in the new Adult(...)), then add the Adult object to the array. NB Java arrays begin with element 0, so you want to increment the counter after adding the Adult. Ps If you use an ArrayList rather than an array, it will resize itself automatically as things are added, and thus avoid you having to guess a maximum size before you start

3 checkboxes - only 1 checked at a time? That's a set of RadioButtons - a group of on/off buttons exactly one of which is on at any time. see http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton

Display a list of multi-field objects? If you want 1 row per Adult, with columns for name etc, look at JTable http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Yoy seem to be on the right track here - copy the data fromn the TextFields into the fields of your Adult object (using the set... methods, or just putting all the values in the new Adult(...)), then add the Adult object to the array. NB Java arrays begin with element 0, so you want to increment the counter after adding the Adult. Ps If you use an ArrayList rather than an array, it will resize itself automatically as things are added, and thus avoid you having to guess a maximum size before you start

Thanks for the help, I have done the same setup without a GUI , and just used the code "MemAd.setMemName(input.next());" which would store what the user typed into the array, but am unsure how to do this using swing, is it a similar method?

... is it a similar method?

Not that I know of, but there's nothing wrong with code like

memAd.setMemName(NameTextField.getText());
 memAd.setDate(DateTextField.getText());
// etc

or

Adult memAd = new Adult (0, NameTextField.getText(), DateTextField.getText(), etc );

If you have a LOT of such fields you can create a Map of which fields relate to which Adult variables, and then automate the data transfer by looping thru the Map, but's overkill for 5 fields

Sorry about dragging this on, but I think I have my array set up now and I want to test it, but cant figure out how to load my data stored in my array to go straight into my JTable?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.