下面列出了javax.swing.JTable#addPropertyChangeListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Constructor to create a new {@link RowNumberTable} instance.
*
* @param contentTable
* the table the {@link RowNumberTable} should be created for
*/
public RowNumberTable(final JTable contentTable) {
this.contentTable = contentTable;
contentTable.addPropertyChangeListener(listener);
setFocusable(false);
setAutoCreateColumnsFromModel(false);
TableColumn column = new TableColumn();
column.setCellRenderer(new RowNumberRenderer());
addColumn(column);
// calculate preferred width dynamically
int rowCount = contentTable.getRowCount();
preferredWidth = 25;
if (rowCount > 0) {
preferredWidth = 15 + 6 * String.valueOf(rowCount).length();
}
getColumnModel().getColumn(0).setPreferredWidth(preferredWidth);
setPreferredScrollableViewportSize(getPreferredSize());
}
/**
* Create a TableColumnManager for a table.
*
* @param table the table whose TableColumns will managed.
* @param menuPopup enable or disable a popup menu to allow the users to
* manager the visibility of TableColumns.
*/
public TableColumnManager(JTable table, boolean menuPopup)
{
this.table = table;
setMenuPopup( menuPopup );
table.addPropertyChangeListener( this );
reset();
}
public ImprovedFileChooser(File currentDirectory, FileSystemView fsv) {
super(currentDirectory, fsv);
AbstractButton detailsViewButton = getDetailsViewButton(this);
if (detailsViewButton == null) {
// mac osx does not have a details button. It is recommended to use
// a java.awt.FileDialog box on MacOSX for the proper look and feel.
// so, don't warn if on a mac.
// https://developer.apple.com/library/mac/documentation/Java/Conceptual/Java14Development/07-NativePlatformIntegration/NativePlatformIntegration.html
if (!isMacOs) {
logger.warn("Couldn't find Details button!");
}
return;
}
detailsViewButton.doClick(); // Programmatically switch to the Details View.
List<JTable> tables = SwingUtils.getDescendantsOfType(JTable.class, this);
JTable detailsTable;
if (tables.size() != 1) {
logger.warn("Expected to find 1 JTable in the file chooser, but found " + tables.size());
return;
} else if ((detailsTable = tables.get(0)) == null) {
logger.warn("The details view table was null!");
return;
}
// Set the preferred column widths so that they're big enough to display all data without truncation.
ColumnWidthsResizer resizer = new ColumnWidthsResizer(detailsTable);
detailsTable.getModel().addTableModelListener(resizer);
detailsTable.getColumnModel().addColumnModelListener(resizer);
// Left-align every cell, including header cells.
TableAligner aligner = new TableAligner(detailsTable, SwingConstants.LEADING);
detailsTable.getColumnModel().addColumnModelListener(aligner);
// Every time the directory is changed in a JFileChooser dialog, a new TableColumnModel is created.
// This is bad, because it discards the alignment decorators that we installed on the old model.
// So, we 're going to listen for the creation of new TableColumnModels so that we can reinstall the decorators.
detailsTable.addPropertyChangeListener(new NewColumnModelListener(detailsTable, SwingConstants.LEADING));
// It's quite likely that the total width of the table is NOT EQUAL to the sum of the preferred column widths
// that our TableModelListener calculated. In that case, resize all of the columns an equal percentage.
// This will ensure that the relative ratios of the *actual* widths match those of the *preferred* widths.
detailsTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
}
private TableEditorStopper(JTable table)
{
this.table=table;
table.addPropertyChangeListener("tableCellEditor", this);
}
/**
* Adds striping to the background of the given {@link JTable}. The actual
* striping is installed on the containing {@link JScrollPane}'s
* {@link JViewport}, so if this table is not added to a {@code JScrollPane}
* , then no stripes will be painted. This method can be called before the
* given table is added to a scroll pane, though, as a
* {@link PropertyChangeListener} will be installed to handle "ancestor"
* changes.
*
* @param table the table to paint row stripes for.
*/
public static void setViewPortListeners(JTable table) {
table.addPropertyChangeListener("ancestor", createAncestorPropertyChangeListener(table));
// Install a listener to cause the whole table to repaint when a column
// is resized. We do this because the extended grid lines may need to be
// repainted. This could be cleaned up, but for now, it works fine.
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
table.getColumnModel().getColumn(i).addPropertyChangeListener(createAncestorPropertyChangeListener(table));
table.getColumnModel().addColumnModelListener(createTableColumnModelListener(table));
}
}
public static PropertyChangeListener addModelTracker(JTable p_Table,
final TableModelListener p_Listener) {
PropertyChangeListener propListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
TableModel oldModel = (TableModel) event.getOldValue();
TableModel newModel = (TableModel) event.getNewValue();
if (oldModel != null)
oldModel.removeTableModelListener(p_Listener);
if (newModel != null)
newModel.addTableModelListener(p_Listener);
}
};
p_Table.addPropertyChangeListener("model", propListener);
p_Table.getModel().addTableModelListener(p_Listener);
return propListener;
}
public static PropertyChangeListener addColumnModelTracker(JTable p_Table,
final TableColumnModelListener p_Listener) {
PropertyChangeListener propListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
TableColumnModel oldModel = (TableColumnModel) event.getOldValue();
TableColumnModel newModel = (TableColumnModel) event.getNewValue();
if (oldModel != null)
oldModel.removeColumnModelListener(p_Listener);
if (newModel != null)
newModel.addColumnModelListener(p_Listener);
}
};
p_Table.addPropertyChangeListener("columnModel", propListener);
p_Table.getColumnModel().addColumnModelListener(p_Listener);
return propListener;
}