Line 3: Line 3:
 
  * This class is for table model. This class is designed for easy usage.
 
  * 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.
 
  * You don't need to specify the width of table. Just define the head of table.
 +
* @author Sangchun Han
 
  */
 
  */
public final class MyTable extends javax.swing.table.AbstractTableModel {
+
public class MyTable extends javax.swing.table.AbstractTableModel {
 
private java.util.List<Object> data;
 
private java.util.List<Object> data;
 
private String[] columnNames;
 
private String[] columnNames;
 
 
/**
 
/**
 
* This generates the table model object with given column names.
 
* This generates the table model object with given column names.
Line 16: Line 16:
 
setColumnNames(columnNames);
 
setColumnNames(columnNames);
 
}
 
}
 
 
/**
 
/**
 
* Set the column head.
 
* Set the column head.
Line 22: Line 21:
 
*/
 
*/
 
public void setColumnNames(String[] columnNames) {
 
public void setColumnNames(String[] columnNames) {
remove();
+
removeAll();
 
this.columnNames = columnNames;
 
this.columnNames = columnNames;
 
}
 
}
 
 
/**
 
/**
 
* This returns the number of rows.
 
* This returns the number of rows.
Line 33: Line 31:
 
return data.size() / columnNames.length;
 
return data.size() / columnNames.length;
 
}
 
}
 
 
/**
 
/**
 
* This returns the number of columns.
 
* This returns the number of columns.
Line 41: Line 38:
 
return columnNames.length;
 
return columnNames.length;
 
}
 
}
 
 
/**
 
/**
 
* This returns the element that is specified by row and column indexes.
 
* This returns the element that is specified by row and column indexes.
Line 56: Line 52:
 
}
 
}
 
}
 
}
 
 
/**
 
/**
 
* This returns the column name that is required.
 
* This returns the column name that is required.
Line 66: Line 61:
 
return columnNames[col];
 
return columnNames[col];
 
}
 
}
 
 
/**
 
/**
 
* Add string data type.
 
* Add string data type.
Line 72: Line 66:
 
*/
 
*/
 
public void add(String element) {
 
public void add(String element) {
data.add(" " + element.trim());
+
if (element == null) {
 +
data.add("" + null);
 +
} else {
 +
data.add(" " + element.trim());
 +
}
 
}
 
}
 
 
/**
 
/**
 
* Add integer data type.
 
* Add integer data type.
Line 82: Line 79:
 
add(Ts.convertIntToString(element));
 
add(Ts.convertIntToString(element));
 
}
 
}
 
 
/**
 
/**
 
* Add double data type with specified width and decimal.
 
* Add double data type with specified width and decimal.
Line 93: Line 89:
 
add(Ts.convertDoubleToString(element, width, decimal));
 
add(Ts.convertDoubleToString(element, width, decimal));
 
}
 
}
 
 
/**
 
/**
 
* Add double.
 
* Add double.
Line 101: Line 96:
 
add(Ts.convertDoubleToString(element));
 
add(Ts.convertDoubleToString(element));
 
}
 
}
 
 
/**
 
/**
 
* Add boolean.
 
* Add boolean.
Line 109: Line 103:
 
add(element);
 
add(element);
 
}
 
}
 
+
/**
 +
* Add IP address.
 +
* @param addr
 +
*/
 +
public void add(java.net.InetAddress addr) {
 +
add(addr.toString().substring(1));
 +
}
 
/**
 
/**
 
* Remove every elements.
 
* Remove every elements.
 
*/
 
*/
public void remove() {
+
public void removeAll() {
 
data.clear();
 
data.clear();
 
}
 
}
 
 
/**
 
/**
 
* This is simple example of using this table model.
 
* This is simple example of using this table model.
 
*/
 
*/
public static void example1() {
+
private static void example1() {
 
MyTable model = new MyTable(new String[]{"Measure", "Value"});
 
MyTable model = new MyTable(new String[]{"Measure", "Value"});
 
model.add("order");
 
model.add("order");
Line 134: Line 133:
 
Ts.guiFrame(frame, 400, 300);
 
Ts.guiFrame(frame, 400, 300);
 
}
 
}
 
+
//================================================ this is for example =====
 
public static void main(String[] args) {
 
public static void main(String[] args) {
 
example1();
 
example1();

Latest revision as of 22:25, 9 February 2011

/**
 * 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);
		}
	});
}


Back to JavaHowTo

Alumni Liaison

Have a piece of advice for Purdue students? Share it through Rhea!

Alumni Liaison