0

My question is conceptual about synchronizing data and events in programming a gui. (This example shows batch state being the facilitator of taking classes that implement the same interface and dynamical updating cells in two different frames.This code is where I got my idea. )

I am assuming that I will create new instances of this batch state object specifically in the classes where an event is triggered(panel) and the reaction to that event(another panel) . I will do this by adding the classes that need to communicate to the a list of bsListeners. Then call the batch state function like "setSelectedCell()" to iterate over each class to synchronize them.

The Problem This would work perfect if the object both shared the same arrayList but since they are both new instances they don't. I tried changing things to static and it is freaking out especially in the interface. Is this approach logical I am brand new to programming gui's? Sorry this is a novel.

interface BatchStateListener {

    public void valueChanged(Cell cell, String newValue);

    public void selectedCellChanged(Cell newSelectedCell)
}

class BatchState {

    private String[][] values;
    private Cell selectedCell;
    private List<BatchStateListener> listeners;

    public BatchState(int records, int fields) {
        values = new String[records][fields];
        selectedCell = null;
        listeners = new ArrayList<BatchStateListener>();
    }

    public void addListener(BatchStateListener l) {
        listeners.add(l);
    }

    public void setValue(Cell cell, String value) {

        values[cell.record][cell.field] = value;

        for (BatchStateListener l : listeners) {
            l.valueChanged(cell, value);
        }
    }

    public String getValue(Cell cell) {
        return values[cell.record][cell.field];
    }

    public void setSelectedCell(Cell selCell) {

        selectedCell = selCell;

        for (BatchStateListener l : listeners) {
            l.selectedCellChanged(selCell);
        }
    }

    public Cell getSelectedCell() {
        return selectedCell;
    }
}

1 Answer 1

0

My questions was a bit confusing, but I came to my answer. I was just wondering how to implement this BatchState class in my code. I found that if I make it in main and pass it to the constructors frames/panels that need to communicate with each other they all can share reference to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.