interfaceを実装したクラスでinterfaceには無いメッソッドを記述したいのですが、エラーになります。
以下の既存のクラス内に新たにinterfaceには無いメッソッド
public int getIndex(String sid){ return m_data.indexOf(sid); }
を追加したいのですが、そのメソッドを呼び出すメソッドを記述すると
シンボルを見つけられません
シンボル: メソッド getIndex(String)
場所: インターフェース ListModel
とエラー表示されます。
int index; index = jListCustomers.getModel().getIndex(sid);
既存のクラス
private static class MyListData extends javax.swing.AbstractListModel { private java.util.List m_data; public MyListData(java.util.List data) { m_data = data; } public Object getElementAt(int index) { return m_data.get(index); } public int getSize() { return m_data.size(); } }
既存のクラスにメソッドを追加
private static class MyListData extends javax.swing.AbstractListModel { private java.util.List m_data; public MyListData(java.util.List data) { m_data = data; } public Object getElementAt(int index) { return m_data.get(index); } public int getSize() { return m_data.size(); } //これを追加 public int getIndex(String sid){ return m_data.indexOf(sid); } }
既存のクラスの親クラス
public abstract class AbstractListModel<E> implements ListModel<E>, Serializable { protected EventListenerList listenerList = new EventListenerList(); /** * Adds a listener to the list that's notified each time a change * to the data model occurs. * * @param l the <code>ListDataListener</code> to be added */ public void addListDataListener(ListDataListener l) { listenerList.add(ListDataListener.class, l); } /** * Removes a listener from the list that's notified each time a * change to the data model occurs. * * @param l the <code>ListDataListener</code> to be removed */ public void removeListDataListener(ListDataListener l) { listenerList.remove(ListDataListener.class, l); } /** * Returns an array of all the list data listeners * registered on this <code>AbstractListModel</code>. * * @return all of this model's <code>ListDataListener</code>s, * or an empty array if no list data listeners * are currently registered * * @see #addListDataListener * @see #removeListDataListener * * @since 1.4 */ public ListDataListener[] getListDataListeners() { return listenerList.getListeners(ListDataListener.class); } /** * <code>AbstractListModel</code> subclasses must call this method * <b>after</b> * one or more elements of the list change. The changed elements * are specified by the closed interval index0, index1 -- the endpoints * are included. Note that * index0 need not be less than or equal to index1. * * @param source the <code>ListModel</code> that changed, typically "this" * @param index0 one end of the new interval * @param index1 the other end of the new interval * @see EventListenerList * @see DefaultListModel */ protected void fireContentsChanged(Object source, int index0, int index1) { Object[] listeners = listenerList.getListenerList(); ListDataEvent e = null; for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == ListDataListener.class) { if (e == null) { e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1); } ((ListDataListener)listeners[i+1]).contentsChanged(e); } } } /** * <code>AbstractListModel</code> subclasses must call this method * <b>after</b> * one or more elements are added to the model. The new elements * are specified by a closed interval index0, index1 -- the enpoints * are included. Note that * index0 need not be less than or equal to index1. * * @param source the <code>ListModel</code> that changed, typically "this" * @param index0 one end of the new interval * @param index1 the other end of the new interval * @see EventListenerList * @see DefaultListModel */ protected void fireIntervalAdded(Object source, int index0, int index1) { Object[] listeners = listenerList.getListenerList(); ListDataEvent e = null; for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == ListDataListener.class) { if (e == null) { e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1); } ((ListDataListener)listeners[i+1]).intervalAdded(e); } } }
親クラスのinterface
public interface ListModel<E> { /** * Returns the length of the list. * @return the length of the list */ int getSize(); /** * Returns the value at the specified index. * @param index the requested index * @return the value at <code>index</code> */ E getElementAt(int index); /** * Adds a listener to the list that's notified each time a change * to the data model occurs. * @param l the <code>ListDataListener</code> to be added */ void addListDataListener(ListDataListener l); /** * Removes a listener from the list that's notified each time a * change to the data model occurs. * @param l the <code>ListDataListener</code> to be removed */ void removeListDataListener(ListDataListener l); }
使用環境はJava SE1.8とNETBeans IDE 8.2です。
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/29 01:13
2019/09/29 05:52
2019/09/29 11:55