Event processing in multiple windows

so basically I need to have 2 windows, and in the first I have (initially empty) JList, and in the second I have JTable. After double-clicking on an element from JTableit, you need to add the corresponding element to JList(I’m not sure yet that it will be, perhaps, the String value of the first cell in the row in which I double-clicked). In addition, if I ever had elements in JList, then their corresponding values ​​in the table should be highlighted in a different color, so the line I double-clicked in should be set to red. Also, if I delete an item from JListat some point, it should change the color of the corresponding row in the table back to black.

I'm just wondering if anyone can give me advice on the best approach to developing such an application, and in particular (if no one can offer anything else), would it be more efficient to use observers rather than some methods of obtaining? I have not tried working with observers yet, but if they are the best choice in this case, I will be happy to try to learn.

thank

+4
source share
2 answers

, . , Swing Swing. , JList, JList ( , ).

+2

:

" " " , ".

, . , :

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");
      }
    });

  }
}

:

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

+1

Source: https://habr.com/ru/post/1529795/


All Articles