Created
May 19, 2017 14:26
-
-
Save edilsoncichon/a3bed4421f93f27af567cc12173e33a4 to your computer and use it in GitHub Desktop.
Class util for working with component Java JTable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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