Friday, November 28, 2008

Show Image In JTable

In this short post I present, how can we put and show images in JTable component.

Problem
Sometimes we need to put and show images in JTable. A typical situation is when we have a boolean true or false and we don't want to write true/false or yes/no captions.

Solution
The solution is very simply. We have to override the
getColumnClass(int columnIndex)

method of TableModel class, like this:

DefaultTableModel model = new DefaultTableModel() {
@Override
public Class getColumnClass(int columnIndex) {
//Set the index of the column, where the images will be.
final int imageIndex = 5;
return columnIndex == imageIndex ? ImageIcon.class : Object.class;
}
};

I hope it will be useful.

Tuesday, November 25, 2008

Solve IllegalStateException: Illegal to call this method from injected, managed EntityManager

Problem
If we injected an EntityManager into our SessionBean and call the entityManager.getTransaction().begin() method then
IllegalStateException: Illegal to call this method from injected, managed EntityManager
exception is threw.

Java Enterprise version: JEE5
Used server: JBOSS 5.0.0.RC1
EJB version: EJB3


Why did this problem occur?
We wanted to use User-managed transaction, but this is not supported this way.

Solution
To solve this problem, we should use UserTransaction. To do this, inject a javax.ejb.SessionContext instance as Resource:
@Resource
SessionContext sc;


After this we can get an UserTransaction instrance in our method this way:
public void someMethod() {
UserTransaction ut = scgetUserTransaction();
ut.begin();
//We have an active transaction, we can access database use EntityManager intance.
ut.commit();
}

I hope it will be useful.