1,105,578 Community Members

need a loop to add new row(s)

Member Avatar
Light Poster
44 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

hi, i am trying to build a simple program where system asking info (name,age,sex & location) to make a table using do-while loop.

for each info will create one new row in table that i dont know how to make..

please help me...

here, i made for one single row only:

public static void main (String args[])
	String name, age, sex, location, ans;
	Scanner scanner=new Scanner(System.in);
	do {

	System.out.print("Name: ");
	System.out.print("Age: ");
	System.out.print("Gender: ");
	System.out.print("Location: ");
	System.out.print("Do you want another input??  YES / NO:") ;

	while (ans.equals("yes")); 
		System.out.print("Here is the full data");
	JFrame frame=new JFrame("TABLE");
	JPanel panel=new JPanel();
	panel.setLayout(new BorderLayout());
	DefaultTableModel model= new DefaultTableModel();

 Vector<String> vCol = new Vector<String>();
 vCol.addElement("Name"); 	vCol.addElement("Age");  	vCol.addElement("Sex");   	vCol.addElement("Location");
 Vector<String> row= new Vector<String>();
 row.addElement(name); row.addElement(age); row.addElement(sex); row.addElement(location);
Vector<Vector> vRow=new Vector<Vector>();

		model = new DefaultTableModel(vRow, vCol); 
		JTable table = new JTable(model);
     	JScrollPane	scrollpane = new JScrollPane(table);
     panel.add(scrollpane, BorderLayout.CENTER);

Member Avatar
Posting Whiz
312 posts since Nov 2009
Reputation Points: 6 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]

what now you got while run this program...

every time is it replace existing one?

if yes then you read rows from Jtable and add it in vector along with this you add new data(row)..and then add it to jtable..

Member Avatar
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague

Create a class with name, age, ... as attributes. With each loop create that object and put it into a Vector.
Then write code that displays the values of the Objects inside the Vector at the table

Question Answered as of 4 Years Ago by javaAddict and musthafa.aj
This question has already been solved: Start a new discussion instead
Start New Discussion
Tags Related to this Article