/** * This class is for table model. This class is designed for easy usage. * You don't need to specify the width of table. Just define the head of table. * @author Sangchun Han */ public class MyTable extends javax.swing.table.AbstractTableModel { private java.util.List<Object> data; private String[] columnNames; /** * This generates the table model object with given column names. * @param columnNames the string array of column names */ public MyTable(String[] columnNames) { data = new java.util.LinkedList<Object>(); setColumnNames(columnNames); } /** * Set the column head. * @param columnNames the list of column names */ public void setColumnNames(String[] columnNames) { removeAll(); this.columnNames = columnNames; } /** * This returns the number of rows. * @return the number of rows */ public int getRowCount() { return data.size() / columnNames.length; } /** * This returns the number of columns. * @return the number of columns */ public int getColumnCount() { return columnNames.length; } /** * This returns the element that is specified by row and column indexes. * @param rowIndex the index of row that is requested * @param columnIndex the index of column that is requested * @return the value that is required */ public Object getValueAt(int rowIndex, int columnIndex) { int pos = rowIndex * columnNames.length + columnIndex; if (pos < data.size()) { return data.get(pos); } else { return null; } } /** * This returns the column name that is required. * @param col the column index * @return the column name that is required */ @Override public String getColumnName(int col) { return columnNames[col]; } /** * Add string data type. * @param element */ public void add(String element) { if (element == null) { data.add("" + null); } else { data.add(" " + element.trim()); } } /** * Add integer data type. * @param element */ public void add(int element) { add(Ts.convertIntToString(element)); } /** * Add double data type with specified width and decimal. * Actually, it convert the double value into the string. * @param element the double value to add * @param width the length of the converted string * @param decimal the decimal that is expressed */ public void add(double element, int width, int decimal) { add(Ts.convertDoubleToString(element, width, decimal)); } /** * Add double. * @param element the double value to add. */ public void add(double element) { add(Ts.convertDoubleToString(element)); } /** * Add boolean. * @param element the boolean value to add. */ public void add(boolean element) { add(element); } /** * Add IP address. * @param addr */ public void add(java.net.InetAddress addr) { add(addr.toString().substring(1)); } /** * Remove every elements. */ public void removeAll() { data.clear(); } /** * This is simple example of using this table model. */ private static void example1() { MyTable model = new MyTable(new String[]{"Measure", "Value"}); model.add("order"); model.add(12); model.add("size"); model.add(50); model.add("avg degree"); model.add(12.5453234234); javax.swing.JTable table = new javax.swing.JTable(model); javax.swing.JFrame frame = new javax.swing.JFrame("JTable example!"); javax.swing.JScrollPane sPane = new javax.swing.JScrollPane(table); frame.setContentPane(sPane); Ts.guiFrame(frame, 400, 300); } //================================================ this is for example ===== public static void main(String[] args) { example1(); } }
public static String convertIntToString(int number) { return String.valueOf(number); } public static String convertDoubleToString(double x, int w, int d) { if (Double.isNaN(x)) { return "NaN"; } java.text.DecimalFormat fmt = new java.text.DecimalFormat(); fmt.setMaximumFractionDigits(d); fmt.setMinimumFractionDigits(d); fmt.setGroupingUsed(false); StringBuilder s = new StringBuilder(fmt.format(x)); while (s.length() < w) { s.insert(0, ' '); } return s.toString(); } public static String convertDoubleToString(double x) { if (Ts.mathIsInteger(x)) { return Ts.convertIntToString((int) x); } return convertDoubleToString(x, 5, 2); } public static String convertIntToString(int number) { return String.valueOf(number); } public static void guiFrame(final javax.swing.JFrame frame, final int width, final int height) { java.awt.EventQueue.invokeLater(new java.lang.Runnable() { public void run() { frame.setSize(width, height); frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }