0

I am trying to insert Entity Objects into a JTable. The Object i am receiving through hibernate querys are saved here.

list = businessLayer.showAllUsers();

I am two problems.

  • columnnames from Object
  • insert objects into row

What i basically have to do is, show the user what is inside the DB through a JTable.

I declared all Entity Classes through hibernate annotations. So hibernate knows the names and what attributes are supposed to be columns. Is there a way to get the columnnames from the objects? For now i created a String with the columnnames for testing purposes.

I am absolutely no clue how i can display the objects in a row. I am not allowed to import entity classes with all getters and setters because i am using a facade pattern and the UI is not allowed to have information about the persistencelayer, right?

Inside actionPerformed(ActionEvent e){

            if(e.getSource() == btnBenutzerAnzeigen ){
            list = businessLayer.showAllUsers();
            //Die enthaltenen Objecte in die Arrays ColumnNames und data ihrgendwie reinladen
            System.out.println(list);
            System.out.println(list.size());
            columnNames = new String[] {"ID", "Surname", "Name"};

            data = new Object[list.size()];
            for(int i = 0; i < list.size(); i++){
                data[i] = list.get(i);
            }

            model = new MyTableModel(columnNames, data);
            table.setModel(model);
//          model.data = data2;
            System.out.println("Benutzer Anzeigen Button");
            }
    } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, ex.getMessage(), "Fehler",
                JOptionPane.ERROR_MESSAGE);

        // ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
        // QUESTION_MESSAGE, or PLAIN_MESSAGE

    }
  }
}

I have this code from a sampleClass from Oracle i changed it a little bit to my purpose. http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

    class MyTableModel extends AbstractTableModel { 
//  private String[] columnNames = {"First Name",
//                                    "Last Name",
//                                    "Sport",
//                                    "# of Years",
//                                    "Vegetarian"};
    private String[] columnNames = null;
    private Object[] data = null;
    private Object[][] matrix;

    public MyTableModel(String[] columnNames, Object[] data){
        this.columnNames = columnNames;
        this.data = data;
        matrix = new Object[data.length][columnNames.length];
        Object matrix[][] = {data, columnNames};
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return matrix[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
//     public Class getColumnClass(int c) {
//        return getValueAt(0, c).getClass();
/      }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");
        }

        matrix[row][col] = value;
        fireTableCellUpdated(row, col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + matrix[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}
Boermt-die-Buse
  • 94
  • 1
  • 3
  • 12

1 Answers1

0

You can take a look at EntityPersister and related classes. You can use them to programmatically obtain all sorts of meta-information about the entities.

Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110