Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Class util for working with component Java JTable
package util;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableUtil {
/**
* Get model jTable in type DefaultTableModel.
* @param jTable
* @return (DefaultTableModel) model
*/
public static DefaultTableModel getModel(JTable jTable) {
return ((DefaultTableModel) jTable.getModel());
}
/**
* Get index row selected.
* @param jTable
* @return int
*/
public static int getIndexRowSelected(JTable jTable) {
return jTable.getSelectedRow();
}
/**
* Get data (Object) of row selected.
* @param jTable
* @return Object
* @throws Exception
*/
public static Object getDataRowSelected(JTable jTable) throws Exception {
int rowSelected = jTable.getSelectedRow();
int column = 0;
if ( rowSelected < 0 )
throw new Exception("No row not selected.");
return getModel(jTable).getValueAt(rowSelected, column);
}
/**
* Insert new row at end of jTable.
* @param jTable
* @param rowData
*/
public static void addLinha(JTable jTable, Object[] rowData) {
getModel(jTable).addRow(rowData);
}
/**
* Clear all rows of table.
* @param jTable
*/
public static void clearTable(JTable jTable) {
int qtdRows = jTable.getRowCount() - 1;
for (int i = qtdRows; i >= 0; i--)
getModel(jTable).removeRow(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.