Afficher un arrayList dans Table swt
Bonjour, je suis débutant en programmation java. Je voudrais afficher le résultat de mon arrayList dans une table.
J'ai 2 fichiers. L'un pour le ArrayList et l'autre pour l'interface contenant la table.
Merci de votre aide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
package viewer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.lynda.javatraining.db.ConnectionManager;
public class AdminList {
private static Connection conn = ConnectionManager.getInstance().getConnection();
public static ArrayList<String> getPersonList() throws SQLException {
String sql = "SELECT * FROM admin";
ResultSet rs = null;
ArrayList<String> result = new ArrayList <>();
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
result.add(rs.getString("userName"));
result.add(rs.getString("password"));
}
} catch (Exception e) {
System.err.println(e);
}
finally {
rs.close();
}
ConnectionManager.getInstance().close();
return result;
}
} |
Interface avec tableau
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
package viewer;
import java.sql.SQLException;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class FirstView {
public static void main(String[] args) throws SQLException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Application");
Table table = new Table(shell, SWT.BORDER);
table.setSize(500, 300);
TableColumn tableColumn1 = new TableColumn(table, SWT.LEFT);
tableColumn1.setText("Admin Username");
tableColumn1.setWidth(150);
TableColumn tableColumn2 = new TableColumn(table, SWT.LEFT);
tableColumn2.setText("Admin Password");
tableColumn2.setWidth(150);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableItem fillLine1 = new TableItem(table, SWT.None);
fillLine1.setText(new String[] {AdminList.getPersonList().toString()});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
} |