13 Replies - 17318 Views - Last Post: 20 November 2008 - 10:15 PM Rate Topic: -----

#1 progfordummies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-November 08

Adding ArrayList to comboBox

Post icon  Posted 15 November 2008 - 04:06 AM

Hi: I am a student who has just started Java -prgmg -week 3(with 2 classes a week only),

My Assgt: To Build a GUI on Netbeans & Display 3 pieces of info such as name, surname & phone number,when student id element- from ArrayList of student numbers in a comboBox is selected.


I have so far created the GUI design on Netbeans with:
3 labels-vars are: lab1,lab2,lab3
3 textfields-vars are: tf1, tf2, tf3
1 comboBox-var is:stunumCombo
1 jbutton to store-var is: updateB


Here is the Student class coding I have done so far:
public class Student {
private int stNum,phNum;
private String name1, name2;

public Student()
{
stNum=1001;//starts from 1001 
phNum=0;
name1="TBA";
name2="TBA";
}
public Student(int stNum, int phNum, String name1,String name2, char grade)
{
this.stNum=stNum;
this.phNum=phNum;
this.grade=grade;
this.name1=name1;
this.name2=name2;
}
public void setStNum(int stNum)
{
this.stNum=stNum;
}
public int getStNum()
{
return this.stNum;
}
public void setPhNum(String phNum)
{
this.phNum=phNum;
}
public String getPhNum()
{
return this.phNum;
}

public void setGrade(char grade)
{
this.grade=grade;
}
public char getGrade()
{
return this.grade;
}
public void setName1(String name1)
{
this.name1=name1;
}
public String getName1()
{
return this.name1;
}
public void setName2(String name1)
{
this.name1=name1;
}
public String getName2()
{
return this.name1;
} 
public String toString()
{
return this.stNum;
return this.phNum;
return this.name1;
return this.name2;
return this.grade;


I need some help with the 2nd assignment....on how to
store data into an ArrayList...
I need help with:
1.Firstly, how many java class files will i need in addition to the main java class Student.
2.How to write the code for reading the info into the ArrayList & loop it.
3.What wld be the code to add this ArrayList to the ComboBox?

Thanks to whoever can come forward early as submission date is due soon.rgds [code]

Is This A Good Question/Topic? 0
  • +

Replies To: Adding ArrayList to comboBox

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Adding ArrayList to comboBox

Posted 15 November 2008 - 12:10 PM

please post your code between code tags... this will make is easier for us to help you...

Your code here

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: Adding ArrayList to comboBox

Posted 15 November 2008 - 12:55 PM

Try to use more descriptive and self-documenting variables names - phoneNumber, not phNumber. That should be a String btw, not int

Consider using a JTable to display your data in
Was This Post Helpful? 0
  • +
  • -

#4 progfordummies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-November 08

Re: Adding ArrayList to comboBox

Posted 16 November 2008 - 10:34 AM

View Postg00se, on 15 Nov, 2008 - 11:55 AM, said:

Try to use more descriptive and self-documenting variables names - phoneNumber, not phNumber. That should be a String btw, not int

Consider using a JTable to display your data in


String phoneNumber , does make sense, didnt think of that,thanks

I am not sure of what a JTable does & how it can help.

what are the other classes i am going to need?
Would i need to have a StuNum class to build a student Id ArrayList and save to a File?
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: Adding ArrayList to comboBox

Posted 16 November 2008 - 11:20 AM

Quote

I am not sure of what a JTable does & how it can help.


It's good for displaying tabular data and would present your Student fields well. Just sort out the List<Student> and i can help you with the JTable if you want
Was This Post Helpful? 0
  • +
  • -

#6 progfordummies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-November 08

Re: Adding ArrayList to comboBox

Posted 17 November 2008 - 12:59 PM

View Postg00se, on 15 Nov, 2008 - 11:55 AM, said:

Try to use more descriptive and self-documenting variables names - phoneNumber, not phNumber. That should be a String btw, not int

Consider using a JTable to display your data in


I wish the assignment wld have required me to use a JTable ...

However, the requirements for the GUI is to have a ComboBox with a drop-down list of Student ids to pick from , so that when
an id is picked it shd display the student's name, phone, grade.

Anyway, thanks very much
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: Adding ArrayList to comboBox

Posted 17 November 2008 - 01:04 PM

>>shd display the student's name, phone, grade.

Display it where?
Was This Post Helpful? 0
  • +
  • -

#8 progfordummies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-November 08

Re: Adding ArrayList to comboBox

Posted 18 November 2008 - 01:48 AM

View Postg00se, on 17 Nov, 2008 - 12:04 PM, said:

>>shd display the student's name, phone, grade.

Display it where?


in the textfields
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: Adding ArrayList to comboBox

Posted 18 November 2008 - 02:34 AM

You could actually add each Student to the combo box model and then simply get the selected one to display in the fields. If you want to display just the ids, you can use the following in the Student class:


public String toString() {
    return Integer.toString(id);
}


Was This Post Helpful? 0
  • +
  • -

#10 progfordummies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-November 08

Re: Adding ArrayList to comboBox

Posted 19 November 2008 - 04:42 PM

View Postg00se, on 18 Nov, 2008 - 01:34 AM, said:

You could actually add each Student to the combo box model and then simply get the selected one to display in the fields. If you want to display just the ids, you can use the following in the Student class:


public String toString() {
    return Integer.toString(id);
}


i understood this coding. thanks.

However, as per assgt. question yes, i do require to select the id from the comboBox & display the details in the text fields.

I hv found out that
ArrayList.add(id);
..wld help to store ids in the Arraylist...is this right?/

next, how wld i link the stored ids to the variables name1, name2 & phNum & then display them in the text fields??
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: Adding ArrayList to comboBox

Posted 19 November 2008 - 05:50 PM

Quote

However, as per assgt. question yes, i do require to select the id from the comboBox & display the details in the text fields.


Yes - if you add each Student to the combo box model, just the ids will appear in there
Was This Post Helpful? 0
  • +
  • -

#12 progfordummies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-November 08

Re: Adding ArrayList to comboBox

Posted 19 November 2008 - 11:30 PM

so say for example I want to add id # in the range from 1001 to say 2000
wld this coding help ?? If so could you throw some more light on this??
Appreciate yr help :)
public String toString() { return Integer.toString(id);}


Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: Adding ArrayList to comboBox

Posted 20 November 2008 - 03:19 AM

Here's a demo (click on the items and watch the command line printout)

import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;
import javax.swing.event.*;


public class F extends JFrame implements ListSelectionListener {
    private JList studentList;

    public void valueChanged(ListSelectionEvent e) {
	if (!e.getValueIsAdjusting()) {
	    Student s = (Student)studentList.getSelectedValue();
	    printStudent(s);
	}
    }

    // Demo utility method
    private void printStudent(Student s) {
	String ss = String.format("id=%d,name=%s", s.getId(), s.getName());
	System.out.println(ss);
    }

    private void setGui() {
	try {
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container cp = getContentPane();
	    cp.setLayout(new FlowLayout());
	    studentList = new JList(buildStudents());
	    studentList.addListSelectionListener(this);
	    cp.add(studentList);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    private ListModel buildStudents() {
	DefaultListModel lm = new DefaultListModel();
	lm.addElement(new Student(1, "Alan"));
	lm.addElement(new Student(2, "Bert"));
	lm.addElement(new Student(3, "Carl"));
	return lm;
    }

    public static void main(String[] args) {
	try {
	    SwingUtilities.invokeAndWait(new Runnable() {
		public void run() {
		    F f = new F();
		    f.setGui();
		    f.setSize(200, 200);
		    f.setVisible(true);
		}
	    });
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}


class Student {
    private int id;
    private String name;

    public Student(int id, String name) {
	this.id = id;
	this.name = name;
    }
    public int getId() {
	return this.id;
    }

    public void setId(int id) {
	this.id = id;
    }

    public String getName() {
	return this.name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String toString() {
	return Integer.toString(id);
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 progfordummies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-November 08

Re: Adding ArrayList to comboBox

Posted 20 November 2008 - 10:15 PM

Thanks for this demo

will be trying to interpret coding
& figure out how it works & how it will help.

Will post soon. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1