Frage

so basically I need to have 2 windows and in the first one I have (an initially empty) JList and in the 2nd one I have a JTable. Once I double click on an item from the JTable it needs to add a corresponding item to the JList (I am not yet sure what this will be, perhaps the String value of the first cell in the row in which I double-clicked). Furthermore, if I ever have any items in my JList then their corresponding values in the table must be highlighted in a different colour, so the row in which I double-clicked should be set to red. Additionally, if I remove an item from the JList at some point, it should change the colour of the corresponding row in the table back to black.

I am just wondering if somebody could give me advice on the best approach to design an application like this and in particular (if nobody can suggest anything else) would it be more effiecient to use observers, rather than some getter methods? I have not yet tried working with observers but if they are a better choice in this case then I'm happy to try and learn.

Thanks

War es hilfreich?

Lösung

You are going to have to use observers when you create listeners to listen for these click events. Try to write code that listens for the relevant events from one Swing component, and setting the corresponding changes to the other Swing component. You can probably also when you remove an item from the JList make the changes to the Table without having to listen for the add event from the JList (not sure if there even is one).

Andere Tipps

The observer pattern:

"Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically."

Usually I end up using code like the following. Let me know if you need more help:

import java.util.EventListener;
import java.util.EventObject;

import javax.swing.event.EventListenerList;

class MyEvent extends EventObject {
  public MyEvent(Object source) {
    super(source);
  }
}

interface MyEventListener extends EventListener {
  public void myEventOccurred(MyEvent evt);
}

class MyClass {
  protected EventListenerList listenerList = new EventListenerList();

  public void addMyEventListener(MyEventListener listener) {
    listenerList.add(MyEventListener.class, listener);
  }
  public void removeMyEventListener(MyEventListener listener) {
    listenerList.remove(MyEventListener.class, listener);
  }
  void fireMyEvent(MyEvent evt) {
    Object[] listeners = listenerList.getListenerList();
    for (int i = 0; i < listeners.length; i = i+2) {
      if (listeners[i] == MyEventListener.class) {
        ((MyEventListener) listeners[i+1]).myEventOccurred(evt);
      }
    }
  }
}

public class Main {
  public static void main(String[] argv) throws Exception {
    MyClass c = new MyClass();
    c.addMyEventListener(new MyEventListener() {
      public void myEventOccurred(MyEvent evt) {
        System.out.println("fired");
      }
    });

  }
}

That was pulled from:

http://www.java2s.com/Tutorial/Java/0260__Swing-Event/CreatingaCustomEvent.htm

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top