⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modifiabletable.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			panel.add(removeButton);
		}
		if ((buttons&MODIFY)==MODIFY) {
			modifyButton = new RButton(msg.getString("Button.Modify"));
			modifyButton.setActionCommand(MODIFY_COMMAND);
			modifyButton.addActionListener(listener);
			modifyButton.setEnabled(false);
			panel.add(modifyButton);
		}

		// Get ready to go.
		buttonPanel.add(panel, buttonLoc2);
		return buttonPanel;

	}


/*****************************************************************************/


	/**
	 * Returns a table using the specified table model with properties set
	 * correctly for a <code>ModifiableTable</code>.
	 *
	 * @param model The table model.
	 * @param columnNames Objects to use for names of the columns.  This value
	 *        can be <code>null</code> if the model already takes care of its
	 *        column names.
	 * @return The table.
	 */
	private final JTable createTable(DefaultTableModel model,
									Object[] columnNames) {
		if (columnNames!=null)
			model.setColumnIdentifiers(columnNames);
		JTable table = new JTable(model);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(listener);
		return table;
	}


/*****************************************************************************/


	/**
	 * Notifies all listeners of a row being added, removed, or modified.
	 *
	 * @param change The change that occured.
	 * @param row The row that was added, removed or modified.
	 */
	protected void fireModifiableTableEvent(int change, int row) {
		// Guaranteed to return a non-null array.
		Object[] listeners = listenerList.getListenerList();
		if (listeners.length>0) {
			ModifiableTableChangeEvent e =
						new ModifiableTableChangeEvent(this, change, row);
			// Process the listeners last to first, notifying
			// those that are interested in this event.
			for (int i=listeners.length-2; i>=0; i-=2) {
				if (listeners[i]==ModifiableTableListener.class) {
					((ModifiableTableListener)listeners[i+1]).
										modifiableTableChanged(e);
				}
			}
		}
	}


/*****************************************************************************/


	/**
	 * Returns the data in the specified row of the table as an array of
	 * <code>Object</code>s.
	 *
	 * @param row The row in the table.
	 * @return The data.
	 */
	private Object[] getContentsOfRow(int row) {
		int columnCount = table.getColumnCount();
		Object[] data = new Object[columnCount];
		// NOTE:  We get values from the model as opposed to just the
		// table to keep logical column values in order, even if the user
		// changes their physical order by dragging.
		TableModel model = table.getModel();
		for (int i=0; i<columnCount; i++) {
			data[i] = model.getValueAt(row, i);
		}
		return data;
	}


/*****************************************************************************/


	/**
	 * Retrieves the data stored in the table as a <code>Vector</code> of
	 * <code>Vector</code>s.  Each <code>Vector</code> inside of the
	 * returned <code>Vector</code> represents a single row.<p>
	 *
	 * This method is simply a wrapper for
	 * <code>((DefaultTableModel)getTable().getModel()).getDataVector()</code>.
	 *
	 * @return The data in the table.
	 */
	public Vector getDataVector() {
		return ((DefaultTableModel)getTable().getModel()).getDataVector();
	}


/*****************************************************************************/


	/**
	 * Returns the table displayed in this modifiable table.
	 *
	 * @return The table.
	 * @see #setTable
	 */
	public JTable getTable() {
		return table;
	}


/*****************************************************************************/


	/**
	 * Modifies the contents of a row in the table.  This method is called
	 * whenever the user presses the <code>Modify</code> button.
	 *
	 * @see #addRow
	 * @see #removeRow
	 */
	protected void modifyRow() {
		if (rowHandler!=null) {
			int selectedRow = table.getSelectedRow();
			Object[] oldData = getContentsOfRow(selectedRow);
			Object[] newData = rowHandler.getNewRowInfo(oldData);
			if (newData!=null) {
				int columnCount = table.getColumnCount();
				for (int i=0; i<columnCount; i++) {
					// Call model's setValueAt(), not table's, so that if
					// they've moved columns around it's still okay.
					table.getModel().
							setValueAt(newData[i], selectedRow, i);
				}
				fireModifiableTableEvent(
						ModifiableTableChangeEvent.MODIFIED, selectedRow);
			}
		}
	}


/*****************************************************************************/


	/**
	 * Removes a listener from this modifiable table.
	 *
	 * @param l The listener to remove.
	 * @see #addModifiableTableListener
	 */
	public void removeModifiableTableListener(ModifiableTableListener l) {
		listenerList.remove(ModifiableTableListener.class, l);
	}


/*****************************************************************************/


	/**
	 * Removes the contents of the selected row in the table.  This method
	 * is called whenever the user presses the <code>Remove</code> button.
	 *
	 * @see #addRow
	 * @see #modifyRow
	 */
	protected void removeRow() {
		if (rowHandler!=null) {
			int row = table.getSelectedRow();
			if (rowHandler.shouldRemoveRow(row)) {
				DefaultTableModel model = (DefaultTableModel)table.
													getModel();
				model.removeRow(row);
				// Select a new row as old "current" row was just removed.
				int rowCount = model.getRowCount();
				if (rowCount>row)
					table.setRowSelectionInterval(row, row);
				else if (rowCount>0 && rowCount==row)
					table.setRowSelectionInterval(row-1, row-1);
				fireModifiableTableEvent(ModifiableTableChangeEvent.REMOVED,
									row);
			}
		}
	}


/*****************************************************************************/


	/**
	 * Sets the handler used when the user adds or modifies a row in the
	 * table.
	 *
	 * @param handler The new handler.
	 * @see RowHandler
	 */
	public void setRowHandler(RowHandler handler) {
		this.rowHandler = handler;
	}


/*****************************************************************************/
/*********************** INNER CLASSES ***************************************/
/*****************************************************************************/


	/**
	 * Listens for events in this modifiable table.
	 */
	class Listener implements ActionListener, ListSelectionListener {

		public Listener() {
		}

		public void actionPerformed(ActionEvent e) {
			String actionCommand = e.getActionCommand();
			if (ADD_COMMAND.equals(actionCommand)) {
				addRow();
			}
			else if (REMOVE_COMMAND.equals(actionCommand)) {
				removeRow();
			}
			else if (MODIFY_COMMAND.equals(actionCommand)) {
				modifyRow();
			}
		}

		public void valueChanged(ListSelectionEvent e) {
			if (rowHandler==null)
				return;
			int row = table.getSelectedRow();
			boolean selection = row>-1;
			if (modifyButton!=null) {
				modifyButton.setEnabled(selection);
			}
			if (removeButton!=null) {
				boolean canRemove = rowHandler.shouldRemoveRow(row);
				removeButton.setEnabled(selection && canRemove);
			}
		}

	}


/*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -