Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Listing array of data in tables  (Read 628 times)
0 Members and 1 Guest are viewing this topic.
Offline Sequalit

Junior Member





« Posted 2006-03-25 03:19:54 »

What would be the best wya to display a list of data in a GUI, my GUI will only need a few buttons to submit and stuff, but the main part is a bunch of table cells that can be editable and calculated off of.?

I would think its something like JPanel painting, but that could get messy.

while(gettingTired())
     crankOutMoreCode();
     if(asleep()){
          wakeUp();
          makeCoffee();
          chugCoffee();
     }
}

-Sequalit
Offline Mr_Light

Senior Member




shiny.


« Reply #1 - Posted 2006-03-25 13:34:56 »

JTable
Custom cell renders
ditch the array and take a List of objects.
write a custom tablemodel using the http://jakarta.apache.org/commons/beanutils/ to wrap the model around your POJO/javabean

this is what I cooked up before I remembered the commons lib:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
public class RecordListModel<E> extends AbstractTableModel {

    protected List<E> list;
    protected String[] fields;
   
    private String[] collumNames;
    private Method[] methodes;    

    public RecordListModel(List<E> list, String[] fields) {
        this(list,fields,fields);
    }
   
    public RecordListModel(List<E> list, String[] fields, String[] collumNames) {
        this.list = list;
        this.fields = fields;
        this.collumNames = collumNames;
        methodes = new Method[fields.length];
        if(fields.length!=collumNames.length)
            throw new IllegalArgumentException("not the same amount of fields and collum names specified.");
    }
   
    public int getRowCount() {
        return list.size();
    }
   
    public int getColumnCount() {
        return fields.length;
    }
   
    public String getColumnName(int columnIndex) {
        if(!(columnIndex<0) && !(columnIndex>collumNames.length))
                return collumNames[columnIndex];
        return super.getColumnName(columnIndex);
    }
   
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object value = null;
       
        E object = list.get(rowIndex);
        Method method = methodes[columnIndex];
        if(method==null) {
            method = getMethodFor(object.getClass(),fields[columnIndex]);
            methodes[columnIndex] = method;
        }
       
        try {
            value = method.invoke(object);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("problem with invoking methode; getters should not have parameters");
        } catch (IllegalAccessException e) {
            /* should not occour since getMethodFor class already throws the exception*/
            throw new RuntimeException("field not accessable; make sure theres an public getter for the field");
        } catch (InvocationTargetException e) {
            throw new RuntimeException("Error invoking while invoking the getter; getter threw an exception");
        }
        return value;
    }

    private Method getMethodFor(Class<? extends Object> forClass, String field) {
        try {
            return forClass.getMethod("get"+field);
        } catch (SecurityException e) {
            throw new RuntimeException("field not accessable through getter; make sure theres an public getter for the field");
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("(getter-)Methode unknown ('"+forClass.getName()+".get"+field+"()'), check your constructor call / make sure theres a getter for the field");
        }
    }
   
    private Class<?> getPrimitiveWrapper(Class primitive) {
        if(primitive.equals(short.class)) {
            return Short.class;
        } else if(primitive.equals(int.class)) {
            return Integer.class;    
        } else if(primitive.equals(long.class)) {
            return Long.class;    
        } else if(primitive.equals(float.class)) {
            return Float.class;    
        } else if(primitive.equals(double.class)) {
            return Double.class;    
        } else if(primitive.equals(boolean.class)) {
            return Boolean.class;    
        } else if(primitive.equals(byte.class)) {
            return Byte.class;    
        } else if(primitive.equals(char.class)) {
            return Character.class;    
        }
        return Object.class;
    }

    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
        fireTableDataChanged();
    }
   
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if(list.isEmpty())
            return super.getColumnClass(columnIndex);
        Class forClass = getFieldClass(columnIndex);
        //      to avoid renderer getting nullpointerException because the super class of a primitive is non existent
       if(forClass.isPrimitive())
            return getPrimitiveWrapper(forClass);
        return forClass;
    }    
   
    protected Class getFieldClass(int numberField) {
        Method method = methodes[numberField];
        if(method==null) {
            method = getMethodFor(list.get(0).getClass(),fields[numberField]);
            methodes[numberField] = method;
        }        
        return method.getReturnType();
    }
}

srry about the lack of commented code.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00

Cero (60 views)
2014-07-16 00:42:17

Riven (57 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!