« Create your own widget » : différence entre les versions
(Page créée avec « TBW ... Return to the introduction ↑ ») |
Aucun résumé des modifications |
||
Ligne 1 : | Ligne 1 : | ||
We explained in a previous topic that it is not recommended to use directly <font color=#FF8C00>swing</font> components as some mechanisms proposed by <font color=#556B2F>'''GENIUS'''</font> (as conditional display or <font color=#4169E1>before/after</font>) will not be active. Anyway, writing <font color=#4169E1>put(obj)</font> where obj is a pure <font color=#FF8C00>swing</font> 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 <font color=#556B2F>'''GENIUS'''</font> widgets ([{{PathCurrentJavaDoc}}/fr/cnes/genius/highLevel/GComponentList.html GComponentList] was created in this way). | |||
# to create a basic widget a bit as [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GButton.html 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 [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GComponent.html GComponent] and implementing at least the [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GDisplay.html GDisplay] interface. On the following example, we will create a new class called '''GTableList''' using the <font color=#FF8C00>swing</font> widget <font color=#FF8C00>JTable</font>. | |||
As we extend the [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GComponent.html GComponent] class, we will have to override the <font color=#4169E1>getComponent()</font> method returning the <font color=#FF8C00>swing</font> component corresponding to the widget (here the <font color=#FF8C00>JScrollPane</font> which surrounds the <font color=#FF8C00>JTable</font>). | |||
Also, as we implement the [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GDisplay.html GDisplay] interface, we will also have to override the <font color=#4169E1>display()</font> method as indicated below. | |||
That is all ... | |||
<syntaxhighlight lang="java"> | |||
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; | |||
} | |||
} | |||
</syntaxhighlight> | |||
== How to catch a GENIUS event: == | |||
To use the [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GListener.html GListener] interface and thus, the <font color=#4169E1>before/after</font> mechanism, we will only have to implement one of the listener used by the <font color=#FF8C00>swing</font> widget. Here we will take <font color=#FF8C00>TableModelListener</font> | |||
So, implementing this interface, we will have to override the <font color=#FF8C00>tableChanged()</font> method. In the following example, we will test if one of the cells have its value changed. | |||
<syntaxhighlight lang="java"> | |||
public class GTableList extends GComponent implements GDisplay, TableModelListener { | |||
private JTable m_JTable; | |||
... | |||
@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 ! | |||
} | |||
} | |||
</syntaxhighlight> | |||
With the two last lines of code of this implementation, the <font color=#FF8C00>swing</font> event is redirected towards the <font color=#556B2F>'''GENIUS'''</font> [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GListener.html GListener]] mechanism and, in the class calling this new '''GTableList''' object, we will be able to use the <font color=#4169E1>before/after</font> methods. | |||
<syntaxhighlight lang="java"> | |||
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 ..."); | |||
} | |||
} | |||
</syntaxhighlight> | |||
[[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]] | [[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]] |
Version du 5 mai 2017 à 14:10
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. 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;
...
@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 ...");
}
}