Java Code Examples for javax.swing.table.TableModel#addTableModelListener()

The following examples show how to use javax.swing.table.TableModel#addTableModelListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultOutlineModel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Creates a new instance of DefaultOutlineModel.  <strong><b>Note</b> 
 * Do not fire table structure changes from the wrapped TableModel (value
 * changes are okay).  Changes that affect the number of rows must come
 * from the TreeModel.
 * @param treeModel The tree model
 * @param tableModel The table model
 * @param largeModel <code>true</code> if it's a large model tree, <code>false</code> otherwise.
 * @param nodesColumnLabel Label of the node's column
 */
protected DefaultOutlineModel(TreeModel treeModel, TableModel tableModel, boolean largeModel, String nodesColumnLabel) {
    this.treeModel = treeModel;
    this.tableModel = tableModel;
    if (nodesColumnLabel != null) {
        this.nodesColumnLabel = nodesColumnLabel;
    }
    
    layout = largeModel ? (AbstractLayoutCache) new FixedHeightLayoutCache() 
        : (AbstractLayoutCache) new VariableHeightLayoutCache();
        
    broadcaster = new EventBroadcaster (this);
    
    layout.setRootVisible(true);
    layout.setModel(this);
    treePathSupport = new TreePathSupport(this, layout);
    treePathSupport.addTreeExpansionListener(broadcaster);
    treePathSupport.addTreeWillExpandListener(broadcaster);
    treeModel.addTreeModelListener(broadcaster);
    tableModel.addTableModelListener(broadcaster);
    if (tableModel instanceof ProxyTableModel) {
        ((ProxyTableModel) tableModel).setOutlineModel(this);
    }
}
 
Example 2
Source File: DataViewTableUI.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public void setModel(TableModel dataModel) {
    if (!(dataModel instanceof DataViewTableUIModel)) {
        throw new IllegalArgumentException("DataViewTableUI only supports"
                + " instances of DataViewTableUIModel");
    }
    if (getModel() != null) {
        getModel().removeTableModelListener(dataChangedListener); // Remove ChangeListener on replace
    }
    super.setModel(dataModel);
    dataModel.addTableModelListener(dataChangedListener); // Add new change listener
    if (dataviewUI != null) {
        dataviewUI.handleColumnUpdated();
    }
}
 
Example 3
Source File: PropertySheetTable.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Overriden to register a listener on the model. This listener
 * ensures editing is cancelled when editing row is being changed.
 * 
 * @see javax.swing.JTable#setModel(javax.swing.table.TableModel)
 * @throws IllegalArgumentException if dataModel is not a
 *           {@link PropertySheetTableModel}
 */
public void setModel(TableModel newModel) {
  if (!(newModel instanceof PropertySheetTableModel)) {
    throw new IllegalArgumentException("dataModel must be of type "
        + PropertySheetTableModel.class.getName());
  }

  if (cancelEditing == null) {
    cancelEditing = new CancelEditing();
  }

  TableModel oldModel = getModel();
  if (oldModel != null) {
    oldModel.removeTableModelListener(cancelEditing);
  }
  super.setModel(newModel);
  newModel.addTableModelListener(cancelEditing);

  // ensure the "value" column can not be resized
  getColumnModel().getColumn(1).setResizable(false);
}
 
Example 4
Source File: SheetCell.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellEditorComponent(JTable table,
                                         Object value,
                                         boolean isSelected,
                                         int r, int c) {
   TableModel tableModel = outline.getModel();
   tableModel.addTableModelListener(this);
   return super.getTableCellEditorComponent(table, value, isSelected, r, c);
}
 
Example 5
Source File: JTableEx.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void setModel(TableModel dataModel)
{
	Objects.requireNonNull(dataModel, "Cannot set a null TableModel");
	if (this.dataModel != dataModel)
	{
		TableModel old = this.dataModel;
		if (old != null)
		{
			old.removeTableModelListener(this);
		}
		this.dataModel = dataModel;
		dataModel.addTableModelListener(this);

		tableChanged(new TableModelEvent(dataModel, TableModelEvent.HEADER_ROW));

		firePropertyChange("model", old, dataModel);

		if (getAutoCreateRowSorter())
		{
			if (dataModel instanceof SortableTableModel)
			{
				super.setRowSorter(new SortableTableRowSorter((SortableTableModel) dataModel));
			}
			else
			{
				super.setRowSorter(new TableRowSorter<>(dataModel));
			}
		}
	}
}
 
Example 6
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JEditorPane editor = new JEditorPane("text/html", PLACEHOLDER);
  editor.setOpaque(false);
  editor.setEditable(false);
  editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
  editor.addHyperlinkListener(e -> {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      UIManager.getLookAndFeel().provideErrorFeedback((Component) e.getSource());
    }
  });

  String[] columnNames = {"Integer", "String", "Boolean"};
  TableModel model = new DefaultTableModel(null, columnNames) {
    @Override public Class<?> getColumnClass(int column) {
      switch (column) {
        case 0: return Integer.class;
        case 2: return Boolean.class;
        default: return String.class;
      }
    }
  };
  model.addTableModelListener(e -> {
    DefaultTableModel m = (DefaultTableModel) e.getSource();
    editor.setVisible(m.getRowCount() == 0);
  });
  JTable table = new JTable(model);
  table.setAutoCreateRowSorter(true);
  table.setFillsViewportHeight(true);
  table.setComponentPopupMenu(new TablePopupMenu());
  table.setLayout(new GridBagLayout());
  table.add(editor);
  add(new JScrollPane(table));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 7
Source File: UserActivityWatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void propertyChange(PropertyChangeEvent evt) {
  TableModel oldModel = (TableModel)evt.getOldValue();
  if (oldModel != null) {
    oldModel.removeTableModelListener(myTableModelListener);
  }

  TableModel newModel = (TableModel)evt.getNewValue();
  if (newModel != null) {
    newModel.addTableModelListener(myTableModelListener);
  }

  if (oldModel != null) {
    fireUIChanged();
  }
}
 
Example 8
Source File: FormattedTable.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
public void setModel(final TableModel model) {
    super.setModel(model);

    // format if in the correct state
    if (shouldFormat()) {
        formatTable();
    }

    // add listener to format the table once the table contains data
    if (!isFormatted) {
        // if table is not formatted then add a listener so that the table can be formatted once data is in it
        model.addTableModelListener(formattedListener);
    }
}
 
Example 9
Source File: ExtendedJTable.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void setModel(final TableModel model) {
	boolean shouldSort = this.sortable && checkIfSortable(model);

	if (shouldSort) {
		this.tableSorter = new ExtendedJTableSorterModel(model);
		this.tableSorter.setTableHeader(getTableHeader());
		super.setModel(this.tableSorter);
	} else {
		super.setModel(model);
		this.tableSorter = null;
	}

	originalOrder = new String[model.getColumnCount()];
	for (int c = 0; c < model.getColumnCount(); c++) {
		originalOrder[c] = model.getColumnName(c);
	}

	// initializing arrays for cell renderer settings
	cutOnLineBreaks = new boolean[model.getColumnCount()];
	maximalTextLengths = new int[model.getColumnCount()];
	Arrays.fill(maximalTextLengths, Integer.MAX_VALUE);

	model.addTableModelListener(new TableModelListener() {

		@Override
		public void tableChanged(final TableModelEvent e) {
			int oldLength = cutOnLineBreaks.length;
			if (oldLength != model.getColumnCount()) {
				cutOnLineBreaks = Arrays.copyOf(cutOnLineBreaks, model.getColumnCount());
				maximalTextLengths = Arrays.copyOf(maximalTextLengths, model.getColumnCount());
				if (oldLength < cutOnLineBreaks.length) {
					Arrays.fill(cutOnLineBreaks, oldLength, cutOnLineBreaks.length, false);
					Arrays.fill(maximalTextLengths, oldLength, cutOnLineBreaks.length, Integer.MAX_VALUE);
				}
			}
		}
	});
}
 
Example 10
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());

  String[] columnNames = {"String", "Number", "Boolean"};
  Object[][] data = {
    {"aaa", 1, false}, {"bbb", 20, false},
    {"ccc", 2, false}, {"ddd", 3, false},
    {"aaa", 1, false}, {"bbb", 20, false},
    {"ccc", 2, false}, {"ddd", 3, false},
  };
  TableModel model = new DefaultTableModel(data, columnNames) {
    @Override public Class<?> getColumnClass(int column) {
      return getValueAt(0, column).getClass();
    }

    @Override public boolean isCellEditable(int row, int col) {
      return col == BOOLEAN_COLUMN;
    }
  };
  JTable table = makeTable(model);
  // TEST: JTable table = makeTable2(model);
  model.addTableModelListener(e -> {
    if (e.getType() == TableModelEvent.UPDATE) {
      // System.out.println("TableModel: tableChanged");
      rowRepaint(table, table.convertRowIndexToView(e.getFirstRow()));
    }
  });
  table.setAutoCreateRowSorter(true);
  table.setFillsViewportHeight(true);
  table.setShowGrid(false);
  table.setIntercellSpacing(new Dimension());
  table.setRowSelectionAllowed(true);
  // table.setSurrendersFocusOnKeystroke(true);
  // table.putClientProperty("JTable.autoStartsEdit", false);
  add(new JScrollPane(table));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 11
Source File: PropertyEditorPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public PropertyEditorPanel(Properties initalValue, boolean editable) {
    initComponents();
    this.value = initalValue;
    this.editable = editable;
    propertyTable.putClientProperty(
            "terminateEditOnFocusLost", Boolean.TRUE);              //NOI18N
    updateTableFromEditor();
    final TableModel tm = propertyTable.getModel();
    tm.addTableModelListener(new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent tme) {
            synchronized (PropertyEditorPanel.this) {
                if (updateing) {
                    return;
                }
                updateing = true;
                Properties p = new Properties();
                for (int i = 0; i < tm.getRowCount(); i++) {
                    p.setProperty((String) tm.getValueAt(i, 0), (String) tm.getValueAt(i, 1));
                }
                Properties oldValue = value;
                value = p;
                firePropertyChange(PROP_VALUE, oldValue, value);
                updateing = false;
            }
        }
    });
    propertyTable.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent lse) {
                    updateRemoveButtonSensible();
                }
            });
    updateAddButtonSensible();
    updateRemoveButtonSensible();
}
 
Example 12
Source File: UserActivityWatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void processComponent(final Component parentComponent) {
  if (parentComponent instanceof JTextComponent) {
    ((JTextComponent)parentComponent).getDocument().addDocumentListener(myDocumentListener);
  }
  else if (parentComponent instanceof ItemSelectable) {
    ((ItemSelectable)parentComponent).addItemListener(myItemListener);
  }
  else if (parentComponent instanceof JList) {
    ((JList)parentComponent).getModel().addListDataListener(myListDataListener);
  } else if (parentComponent instanceof JTree) {
    ((JTree)parentComponent).getModel().addTreeModelListener(myTreeModelListener);
  } else if (parentComponent instanceof DocumentBasedComponent) {
    ((DocumentBasedComponent)parentComponent).getDocument().addDocumentListener(myIdeaDocumentListener);
  }

  if (parentComponent instanceof JComboBox) {
    ComboBoxEditor editor = ((JComboBox)parentComponent).getEditor();
    if (editor != null) {
      register(editor.getEditorComponent());
    }
  }

  if (parentComponent instanceof JTable) {
    JTable table = (JTable)parentComponent;
    table.addPropertyChangeListener("model", myTableListener);
    TableModel model = table.getModel();
    if (model != null) {
      model.addTableModelListener(myTableModelListener);
    }
  }

  if (parentComponent instanceof JSlider) {
    ((JSlider)parentComponent).addChangeListener(myChangeListener);
  }

  if (parentComponent instanceof UserActivityProviderComponent) {
    ((UserActivityProviderComponent)parentComponent).addChangeListener(myChangeListener);
  }
}
 
Example 13
Source File: TableSelectionModel.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
/**
* When the TableModel changes, the TableSelectionModel
* has to adapt to the new Model. This method is called
* if a new TableModel is set to the JTable.
*/
// implements PropertyChangeListener
public void propertyChange(PropertyChangeEvent evt) {
    if ("model".equals(evt.getPropertyName())) {
        TableModel newModel = (TableModel)(evt.getNewValue());
        setColumns(newModel.getColumnCount());
        TableModel oldModel = (TableModel)(evt.getOldValue());
        if (oldModel != null)
            oldModel.removeTableModelListener(this);
        //TableSelectionModel must be aware of changes in the TableModel
        newModel.addTableModelListener(this);
    }
}
 
Example 14
Source File: TableMap.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void setModel(TableModel model) {
    this.model = model; 
    model.addTableModelListener(this); 
}
 
Example 15
Source File: DisplayTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void addTableModelListener(TableModelListener tml) {
TableModel tableModel = getModel();
if (tableModel != null) {
    tableModel.addTableModelListener(tml);
}
   }
 
Example 16
Source File: TableMap.java    From yawl with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void setModel(TableModel model) {
    this.model = model;
    model.addTableModelListener(this);
}
 
Example 17
Source File: TableMap.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setModel(TableModel model) {
    this.model = model; 
    model.addTableModelListener(this); 
}
 
Example 18
Source File: TableMap.java    From charliebot with GNU General Public License v2.0 4 votes vote down vote up
public synchronized void setModel(TableModel tablemodel) {
    model = tablemodel;
    tablemodel.addTableModelListener(this);
}
 
Example 19
Source File: SortingTableModelDecorator.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Creates a new model that decorates the underlying model with sorting
 * capabilities
 * 
 * @param base
 *            Underlying model this model is built on top of.
 */
public SortingTableModelDecorator(TableModel base) {
	this.base = base;
	base.addTableModelListener(this);
	resetIndeci();
}
 
Example 20
Source File: TableHelper.java    From CodenameOne with GNU General Public License v2.0 votes vote down vote up
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;
  }