Skip to content

Instantly share code, notes, and snippets.

@imamhidayat92
Last active December 16, 2015 10:18
Show Gist options
  • Save imamhidayat92/5418619 to your computer and use it in GitHub Desktop.
Save imamhidayat92/5418619 to your computer and use it in GitHub Desktop.
This helper class can be used to help java programmer dealing with JTable.
package helper;
/*
Author: Imam Hidayat (mokhamad.imam[at]students.paramadina.ac.id)
Universitas Paramadina
Source: https://gist.github.com/imamhidayat92/5418619
This helper class can be used to simplify JTable usage in Java.
*/
import java.sql.*;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableHelper {
public static DefaultTableModel convertResultSetToDefaultTableModel(ResultSet results, String[] columns, String[] dbColumns) throws SQLException {
ArrayList<String[]> data = new ArrayList<String[]>();
while (results.next()) {
String[] row = new String[dbColumns.length];
for (int i = 0; i < dbColumns.length; i++) {
row[i] = results.getString(dbColumns[i]);
}
data.add(row);
}
Object[][] tableModelData = new Object[data.size()][columns.length];
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < columns.length; j++) {
tableModelData[i][j] = data.get(i)[j];
}
}
return new DefaultTableModel(tableModelData, columns);
}
public static DefaultTableModel convertResultSetToDefaultTableModel(ResultSet results, String[] columns, int[] dbColumns) throws SQLException {
ArrayList<String[]> data = new ArrayList<String[]>();
while (results.next()) {
String[] row = new String[dbColumns.length];
for (int i = 0; i < dbColumns.length; i++) {
row[i] = results.getString(dbColumns[i]);
}
data.add(row);
}
Object[][] tableModelData = new Object[data.size()][columns.length];
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < columns.length; j++) {
tableModelData[i][j] = data.get(i)[j];
}
}
return new DefaultTableModel(tableModelData, columns);
}
public static DefaultTableModel convertResultSetToDefaultTableModel(ResultSet results, String[] columns, int[] dbColumns, String[][] references) throws SQLException {
ArrayList<String[]> data = new ArrayList<String[]>();
while (results.next()) {
String[] row = new String[dbColumns.length];
for (int i = 0; i < dbColumns.length; i++) {
if (references[i] != null) {
row[i] = references[i][results.getInt(dbColumns[i])];
}
else {
row[i] = results.getString(dbColumns[i]);
}
}
data.add(row);
}
Object[][] tableModelData = new Object[data.size()][columns.length];
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < columns.length; j++) {
tableModelData[i][j] = data.get(i)[j];
}
}
return new DefaultTableModel(tableModelData, columns);
}
/* Table Updater */
public static void updateTable(JScrollPane scrollPane, JTable table, DefaultTableModel tableModel) {
if (table != null) {
table.setModel(tableModel);
}
else {
table = new JTable(tableModel);
}
scrollPane.setViewportView(table);
}
public static void updateTable(JScrollPane scrollPane, JTable table, ResultSet results, String[] columns, String[] dbColumns) throws SQLException {
DefaultTableModel tableModel = JTableHelper.convertResultSetToDefaultTableModel(results, columns, dbColumns);
if (table != null) {
table.setModel(tableModel);
}
else {
table = new JTable(tableModel);
}
scrollPane.setViewportView(table);
}
public static void updateTable(JScrollPane scrollPane, JTable table, ResultSet results, String[] columns, int[] dbColumns) throws SQLException {
DefaultTableModel tableModel = JTableHelper.convertResultSetToDefaultTableModel(results, columns, dbColumns);
if (table != null) {
table.setModel(tableModel);
}
else {
table = new JTable(tableModel);
}
scrollPane.setViewportView(table);
}
public static void updateTable(JScrollPane scrollPane, JTable table, ResultSet results, String[] columns, int[] dbColumns, String[][] references) throws SQLException {
DefaultTableModel tableModel = JTableHelper.convertResultSetToDefaultTableModel(results, columns, dbColumns, references);
if (table != null) {
table.setModel(tableModel);
}
else {
table = new JTable(tableModel);
}
scrollPane.setViewportView(table);
}
public static void removeRow(JTable table, int rowNumber, int columnCount) {
ArrayList<ArrayList<Object>> newData = new ArrayList<ArrayList<Object>>();
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if (i == rowNumber) {
ArrayList<Object> row = new ArrayList<Object>();
for (int j = 0; j < columnCount; j++) {
row.add(table.getModel().getValueAt(i, j));
}
newData.add(row);
}
}
ArrayList<String> columns = new ArrayList<String>();
for (int i = 0; i < columnCount; i++) {
columns.add(table.getColumnName(i));
}
table.setModel(new DefaultTableModel(convertArrayListToObjectArray(newData, columnCount), columns.toArray()));
}
public static void appendRow(JTable table, Object[] newRow, int columnCount) {
ArrayList<ArrayList<Object>> newData = new ArrayList<ArrayList<Object>>();
for (int i = 0; i < table.getModel().getRowCount(); i++) {
ArrayList<Object> row = new ArrayList<Object>();
for (int j = 0; j < columnCount; j++) {
row.add(table.getModel().getValueAt(i, j));
}
newData.add(row);
}
ArrayList<String> columns = new ArrayList<String>();
for (int i = 0; i < columnCount; i++) {
columns.add(table.getColumnName(i));
}
table.setModel(new DefaultTableModel(convertArrayListToObjectArray(newData, columnCount), columns.toArray()));
}
/* Miscellaneous */
public static Object[][] convertArrayListToObjectArray(ArrayList<ArrayList<Object>> data, int columnCount) {
Object[][] newData = new Object[data.size()][columnCount];
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < columnCount; j++) {
newData[i][j] = data.get(i).get(j);
}
}
return newData;
}
}
@najilil
Copy link

najilil commented May 2, 2013

mantapp.. ini gimana caranya?

@imamhidayat92
Copy link
Author

Gampang, tinggal pakai di DefaultTableModel sesuai kebutuhan. :)

Contoh:

ResultSet results = MySQLConnector.executeQuery("some query");

Object[][] data = JTableHelper.convertResultSetToTableData(results);

DefaultTableModel tableModel = new DefaultTableModel(data, columns);
table.setModel(tableModel);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment