Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use tables in Swing?

Status
Not open for further replies.

louisgnarf

Programmer
Jul 11, 2002
14
US
OK, I'm not completely new to Java, and I have read a couple tutorials on tables...but I'm clueless on how to implement them in Swing. What I have is a class with a Swing interface, and in one of the panels I want to have a table which will have a static number of columns, but will be updated at runtime as far as data and number of row entries...how do I go about doing this? Where do I make my declarations of JTable, for example? What commands do I use to add data to them at runtime?
 
make sure you use a TableModel to handle the data....you can use a DefaultTableModel which will let you add Vectors of data or arrays of data (quick and dirty method), or you can define your own model extending AbstractTableModel (preferred method).

something like......

DefaultTableModel myModel = new DefaultTableModel();
JTable myTable = new JTable();
myTable.setModel(myModel);
myPanel.add(myTable);
myModel.add(new String[]("a","b","c"));
myModel.add(new String[]("d","e","f"));

....should get you started.

Because the table uses a Model/View/Controller (MVC) design when you alter data in the model it will inform the View to redisplay and you won't have to worry about refreshing the screen, etc. Jeremy Nicholson, Director of a UK-based Java and Data Warehousing consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top