Create your own widget
We explained in a previous topic that it is not recommended to use directly swing components as some mechanisms proposed by GENIUS (as conditional display or before/after) will not be active. Anyway, writing put(obj) where obj is a pure swing object will even not compile
So, two solutions are available if you do not dispose of the right widget :
- to create a new high level widget using predefined GENIUS widgets (GComponentList was created in this way).
- to create a basic widget a bit as GButton was developped.
Here we are going to explain how to achieve the second solution.
In fact, it is relatively simple ...
How to display the widget:
First, we will have to create a new class extending GComponent and implementing at least the GDisplay interface. On the following example, we will create a new class called GTableList using the swing widget JTable.
As we extend the GComponent class, we will have to override the getComponent() method returning the swing component corresponding to the widget (here the JScrollPane which surrounds the JTable).
Also, as we implement the GDisplay interface, we will also have to override the display() method as indicated below.
That is all ...
public class GTableList extends GComponent implements GDisplay {
private JTable m_JTable;
private JScrollPane m_JScrollPane; // To get the column names displayed
private Object[][] m_data;
private String[] m_columnNames;
public GTableList (Object[][] data, String[] columnNames) {
super(null);
m_data = data;
m_columnNames = columnNames;
m_JTable = new JTable(m_data, m_columnNames);
m_JScrollPane = new JScrollPane(m_JTable);
}
@Override
public void display() {
doDisplay(this);
}
@Override
public Component getComponent() {
return m_JScrollPane;
}
}
How to catch a GENIUS event:
To use the GListener interface and thus, the before/after mechanism, we will only have to implement one of the listener used by the swing widget. Here we will take TableModelListener
So, implementing this interface, we will have to override the tableChanged() method then adding this listener to m_JTable as described below. In the following example, we will test if one of the cells have its value changed.
public class GTableList extends GComponent implements GDisplay, TableModelListener {
private JTable m_JTable;
...
// Adding the listener
m_JTable.getModel().addTableModelListener(this);
@Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
Object data = model.getValueAt(row, column);
System.out.println("New data is "+data);
GEvent gevent = new GEvent();
processGEvent(gevent, this); // Redirection of the event !
}
}
With the two last lines of code of this implementation, the swing event is redirected towards the GENIUS GListener] mechanism and, in the class calling this new GTableList object, we will be able to use the before/after methods.
private GTableList table;
...
table = new GTableList(data, titles);
...
@Override
public void display() throws GException {
put(table);
}
...
public void after(GEvent arg0) throws GException {
if ( arg0.contains(table) ) {
System.out.println("Data is modified ...");
}
}