Aew pessoal, este será o nosso penúltimo post sobre Introdução ao Hibernate.
Nossa interface é muito simples e é composta apenas de uma janela com um grid mostrando informações de categorias. O grid é representado pelo componente JTable e está dentro de um panel. Este elemento é mostrado a seguir com a classe de CategoryGridPanel.
package br.com.branquinho.hibernate.application.gui;
import java.awt.BorderLayout;
public class CategoryGridPanel extends JPanel {
private static final long serialVersionUID = -3127985955124406559L;
private final JTable table;
private final DefaultTableModel tableModel;
/**
* Create the panel.
*/
public CategoryGridPanel() {
String[] columnNames = new String []{ "ID", "Name", "Description"};
this.tableModel = new DefaultTableModel(columnNames, 0);
setLayout(new MigLayout("", "[grow][grow]", "[grow][grow]"));
this.table = new JTable(this.tableModel);
this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.table.setCellSelectionEnabled(true);
add(this.table, "cell 0 0 2 2,grow");
add(this.table.getTableHeader(), BorderLayout.NORTH);
}
public void addCategoryRow(CategoryBean category) {
this.tableModel.addRow(new String[] {
String.valueOf(category.getId()),
category.getName(),
category.getDescription() });
}
}
Nossa janela é representada pela classe HibernateApplicationWindow, apresentada a seguir.
package br.com.branquinho.hibernate.application.gui;
import java.awt.EventQueue;
public class HibernateApplicationWindow {
private JFrame frmHibernateSample;
private static final IServicesFactory serviceFactory = new ServicesFactory();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
HibernateApplicationWindow window = new HibernateApplicationWindow();
window.frmHibernateSample.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public HibernateApplicationWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
this.frmHibernateSample = new JFrame();
this.frmHibernateSample.setTitle("Hibernate Sample");
this.frmHibernateSample.setBounds(100, 100, 800, 600);
this.frmHibernateSample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frmHibernateSample.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
CategoryGridPanel categoryGridPanel = new CategoryGridPanel();
this.frmHibernateSample.getContentPane().add(categoryGridPanel);
loadCategories(categoryGridPanel);
}
private void loadCategories(CategoryGridPanel categoryGridPanel) {
List<CategoryBean> categories = serviceFactory.getCategoryService().findAllCategories();
for (CategoryBean category : categories) {
categoryGridPanel.addCategoryRow(category);
}
}
}
O resultado final de nossa aplicação é mostrado na figura e nos vídeos a seguir.

Vídeo 13
Vídeo 14
No próximo e último post irei falar um pouco sobre o resultado final de nosso trabalho.
Ficamos por aqui, até amanhã a todos.