Synchronized row header and row when selecting and dragging this is an example how to synchronize the row header with the row when a dragging event occurs in a JScrollPane
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
/**
*
* @author Monirul
*/
public class JTableRowHeadersAndRowsSynchronized extends JFrame {
private JTable mainTable;
private JScrollPane scroll ;
private JViewport jv;
private JTable rowHeaderTableColumn;
private JPanel panel;
private TableModel tableModel;
private DefaultTableModel mainTableModel;
private Integer[] rowHeaderData;
public JTableRowHeadersAndRowsSynchronized() {
initComponents();
}
private void initComponents() {
panel = new JPanel();
tableModel = new AbstractTableModel() {
String[] header = {"Rows"};
public int getColumnCount() {
return 1;
}
public int getRowCount() {
return mainTable.getRowCount();
}
public String getColumnName(int col) {
// this will set the row headers column name
return header[0];
}
public Object getValueAt(int row, int col) {
// this will set the row headers row data
// instead of this;
// "return rowHeaderData[col] + row;"
// this one will generate its own count for each row' cell count
return 1 + row;
}
};
TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
boolean first = true;
public void addColumn(TableColumn tableColumn) {
if (first) {
tableColumn.setMaxWidth(tableColumn.getPreferredWidth());
super.addColumn(tableColumn);
tableColumn.setMaxWidth(100);
first = false;
}
}
};
String[] columns = {"Item Code", "Item Desciption", "Item Quantity", "Item Cost",
"Cost Extension", "Retail", "Retail Extension", "Expiration Code"};
Object[][] rows = new Object[200][columns.length];
mainTableModel = new DefaultTableModel(rows, columns);
mainTable = new JTable(mainTableModel);
rowHeaderTableColumn = new JTable(tableModel, rowHeaderModel);
rowHeaderTableColumn.createDefaultColumnsFromModel();
rowHeaderTableColumn.setBackground(Color.lightGray);
rowHeaderTableColumn.setColumnSelectionAllowed(false);
rowHeaderTableColumn.setCellSelectionEnabled(false);
jv = new JViewport();
jv.setView(rowHeaderTableColumn);
jv.setPreferredSize(rowHeaderTableColumn.getMaximumSize());
jv.addChangeListener(new RowHeaderAndRowTableSynchronizerChangeListener());
mainTable.setSelectionModel(rowHeaderTableColumn.getSelectionModel());
mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
panel.setLayout(new GridLayout());
rowHeaderData = new Integer[mainTable.getRowCount()];
for (int count = 0; count <= mainTable.getRowCount() - 1; count++) {
rowHeaderData[count] = count + 1;
}
scroll = new JScrollPane(mainTable);
scroll.setRowHeader(jv);
scroll.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, rowHeaderTableColumn.getTableHeader());
panel.add(scroll);
getContentPane().add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 250);
setLocationRelativeTo(null);
setVisible(true);
}
// this listener will make the mouseDragged selection event in the rowHeader
// synchronized with the inventoryTable's rows
private class RowHeaderAndRowTableSynchronizerChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
// Keep the scrolling of the row table in sync with main table
JViewport viewport = (JViewport) e.getSource();
JScrollPane scrollPane = (JScrollPane)viewport.getParent();
scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
}
}
public static void main(String args[]) {
new JTableRowHeadersAndRowsSynchronized();
}
}
Thursday, April 8, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment