Hibernate,Dao对象插入JTable(AbstractTableModel)

时间:2015-06-03 09:14:16

标签: java swing hibernate jtable abstracttablemodel

我正在尝试将实体对象插入到JTable中。我通过休眠查询接收的对象保存在这里。

list = businessLayer.showAllUsers();

我是两个问题。

    来自Object 的
  • 列名
  • 将对象插入行

我基本上要做的是,通过JTable向用户显示数据库中的内容。

我通过hibernate注释声明了所有实体类。 所以hibernate知道名称和属性应该是列。 有没有办法从对象中获取列名? 现在我创建了一个带有列名的String,用于测试目的。

我绝对不知道如何连续显示对象。 我不允许使用所有getter和setter导入实体类,因为我使用的是Facade模式,并且UI不允许有关于持久层的信息,对吗?

内部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

    }
  }
}

我从Oracle的sampleClass中获取此代码,我将其更改为我的目的。 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("--------------------------");
    }
}

1 个答案:

答案 0 :(得分:0)

您可以查看EntityPersister及相关课程。您可以使用它们以编程方式获取有关实体的各种元信息。