Mega Code Archive
Multi Span Cell Table Example
// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.plaf.basic.BasicTableUI;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
/**
* @version 1.0 11/26/98
*/
public class MultiSpanCellTableExample extends JFrame {
MultiSpanCellTableExample() {
super( "Multi-Span Cell Example" );
AttributiveCellTableModel ml = new AttributiveCellTableModel(10,6);
/*
AttributiveCellTableModel ml = new AttributiveCellTableModel(10,6) {
public Object getValueAt(int row, int col) {
return "" + row + ","+ col;
}
};
*/
final CellSpan cellAtt =(CellSpan)ml.getCellAttribute();
final MultiSpanCellTable table = new MultiSpanCellTable( ml );
JScrollPane scroll = new JScrollPane( table );
JButton b_one = new JButton("Combine");
b_one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] columns = table.getSelectedColumns();
int[] rows = table.getSelectedRows();
cellAtt.combine(rows,columns);
table.clearSelection();
table.revalidate();
table.repaint();
}
});
JButton b_split = new JButton("Split");
b_split.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int column = table.getSelectedColumn();
int row = table.getSelectedRow();
cellAtt.split(row,column);
table.clearSelection();
table.revalidate();
table.repaint();
}
});
JPanel p_buttons = new JPanel();
p_buttons.setLayout(new GridLayout(2,1));
p_buttons.add(b_one);
p_buttons.add(b_split);
Box box = new Box(BoxLayout.X_AXIS);
box.add(scroll);
box.add(new JSeparator(SwingConstants.HORIZONTAL));
box.add(p_buttons);
getContentPane().add( box );
setSize( 400, 200 );
setVisible(true);
}
public static void main(String[] args) {
MultiSpanCellTableExample frame = new MultiSpanCellTableExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}
/**
* @version 1.0 11/22/98
*/
class AttributiveCellTableModel extends DefaultTableModel {
protected CellAttribute cellAtt;
public AttributiveCellTableModel() {
this((Vector)null, 0);
}
public AttributiveCellTableModel(int numRows, int numColumns) {
Vector names = new Vector(numColumns);
names.setSize(numColumns);
setColumnIdentifiers(names);
dataVector = new Vector();
setNumRows(numRows);
cellAtt = new DefaultCellAttribute(numRows,numColumns);
}
public AttributiveCellTableModel(Vector columnNames, int numRows) {
setColumnIdentifiers(columnNames);
dataVector = new Vector();
setNumRows(numRows);
cellAtt = new DefaultCellAttribute(numRows,columnNames.size());
}
public AttributiveCellTableModel(Object[] columnNames, int numRows) {
this(convertToVector(columnNames), numRows);
}
public AttributiveCellTableModel(Vector data, Vector columnNames) {
setDataVector(data, columnNames);
}
public AttributiveCellTableModel(Object[][] data, Object[] columnNames) {
setDataVector(data, columnNames);
}
public void setDataVector(Vector newData, Vector columnNames) {
if (newData == null)
throw new IllegalArgumentException("setDataVector() - Null parameter");
dataVector = new Vector(0);
setColumnIdentifiers(columnNames);
dataVector = newData;
//
cellAtt = new DefaultCellAttribute(dataVector.size(),
columnIdentifiers.size());
newRowsAdded(new TableModelEvent(this, 0, getRowCount()-1,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
public void addColumn(Object columnName, Vector columnData) {
if (columnName == null)
throw new IllegalArgumentException("addColumn() - null parameter");
columnIdentifiers.addElement(columnName);
int index = 0;
Enumeration eeration = dataVector.elements();
while (eeration.hasMoreElements()) {
Object value;
if ((columnData != null) && (index < columnData.size()))
value = columnData.elementAt(index);
else
value = null;
((Vector)eeration.nextElement()).addElement(value);
index++;
}
//
cellAtt.addColumn();
fireTableStructureChanged();
}
public void addRow(Vector rowData) {
Vector newData = null;
if (rowData == null) {
newData = new Vector(getColumnCount());
}
else {
rowData.setSize(getColumnCount());
}
dataVector.addElement(newData);
//
cellAtt.addRow();
newRowsAdded(new TableModelEvent(this, getRowCount()-1, getRowCount()-1,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
public void insertRow(int row, Vector rowData) {
if (rowData == null) {
rowData = new Vector(getColumnCount());
}
else {
rowData.setSize(getColumnCount());
}
dataVector.insertElementAt(rowData, row);
//
cellAtt.insertRow(row);
newRowsAdded(new TableModelEvent(this, row, row,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
public CellAttribute getCellAttribute() {
return cellAtt;
}
public void setCellAttribute(CellAttribute newCellAtt) {
int numColumns = getColumnCount();
int numRows = getRowCount();
if ((newCellAtt.getSize().width != numColumns) ||
(newCellAtt.getSize().height != numRows)) {
newCellAtt.setSize(new Dimension(numRows, numColumns));
}
cellAtt = newCellAtt;
fireTableDataChanged();
}
/*
public void changeCellAttribute(int row, int column, Object command) {
cellAtt.changeAttribute(row, column, command);
}
public void changeCellAttribute(int[] rows, int[] columns, Object command) {
cellAtt.changeAttribute(rows, columns, command);
}
*/
}
/**
* @version 1.0 11/22/98
*/
class DefaultCellAttribute
// implements CellAttribute ,CellSpan {
implements CellAttribute ,CellSpan ,ColoredCell ,CellFont {
//
// !!!! CAUTION !!!!!
// these values must be synchronized to Table data
//
protected int rowSize;
protected int columnSize;
protected int[][][] span; // CellSpan
protected Color[][] foreground; // ColoredCell
protected Color[][] background; //
protected Font[][] font; // CellFont
public DefaultCellAttribute() {
this(1,1);
}
public DefaultCellAttribute(int numRows, int numColumns) {
setSize(new Dimension(numColumns, numRows));
}
protected void initValue() {
for(int i=0; i