开发者

add arraylist data to JTable

i hav a ArreayList which is contain PRIvariable(name of the class) class data. Below shows my part of the java code. So now i want to put this Arraylist data to Jtable. how can i do that. Here i already added pri.dateText , pri.sum , pri.count to ar(arraylist)

PRIvariable pri=new PRIvariable();
   while (reader.ready()) {
  开发者_开发百科           String line = reader.readLine();
             String[] values = line.split(",");
             if(values[2].equals(pri.incDate)){
                 if(values[4].equals("KI")){
             pri.dateText=values[2]+"    "+values[4];
             pri.count=pri.count+1;
             pri.sum = pri.sum+Integer.parseInt(values[7]);
         }
             }
         }
     System.out.println(pri.dateText+"  "+pri.sum+" "+pri.count);
     ar.add(pri);


Like all Swing components, a JTable relies upon the MVC pattern (at multiple levels, but that's not the subject).

You have one view (the JTable), one model (I'll come back on it later), and a controller (implemented here as a set of event listeners : one controller for each kind of control).

The array you have could be a good model starting point. However, Swing provides far better way to inject your data in JTable. Indeed, a JTable uses as model an implementation of TableModel. Hopefully, there already exist an implementation : DefaultTableModel.

So, here is what I suggest to you : create DefaultTableModel, put in its rows/columns all the data you want to display in your table, then call JTable#setModel(TableModel) to have thze table display your data.

Obviously, you'll soon find various misfits between DefaultTableModel and what you want to do. It will then be time for you to create our very own table model. But that's another question.

Besides, don't forget to take a look at Swing tutorial, it's usually a good thing when dealing with Swing components.


I don't see where you have an ArrayList anywhere.

First you create a PRIvariable object. Then you keep looping as you read a line of data from the file. For each line of data you split it into individual tokens and then add some of the token data to the PRIvariable ojbect. The problem is that you only have a single PRIVariable object. So every time you read a new line of data you change the values of the PRIvariable object. After all the looping you add this single PRIvariable object to your ArrayList, but you will only ever have one object in the ArrayList.

The easier solution is to update the TableModel as you get the data. Something like:

DefaultTableModel model = new DefaultTableModel(...);
JTable table = new JTable( model );
...
...

while (reader.ready()) 
{              
    String line = reader.readLine();              
    String[] values = line.split(",");
    String[] row = new String[3];
    row[0] = values[?];
    row[1] = values[?];
    row[2] = values[?];
    model.addRow( row );
}


Look into GlazedLists. They make it extremely easy to create a suitable TableModel object from your list.

ArrayList< PRIvariable> myList = new ArrayList<PRIvariable>();
... fill up the list ...
// This assumes your object has a getName() and getAge() methods
String[] propertyNames = {"name","age"};
String[] columnLabels = {"Name","Age"};
// Are these columns editable?
boolean[] writeable = {false, false};

EventList<PRIvariable> eventList = GlazedLists.eventList(myList);
EventTableModel<PRIvariable> tableModel = new EventTableModel<PRIvariable>(eventList,propertyNames,columnLabels,writable);
JTable table = new JTable(tableModel);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜