views:

116

answers:

1

Hi everybody I created a main class which displays a frame, then two JPanel are created and pointed. It is supposed for those Jpanel to give a visual display of some values incoming via serial port.

Everhing work fine..but the problem is that those panel are identical and I would need that one Japnel processes and visualizes value from one source and the other panel displays data coming from another source.

Because the JPanel are identical and have same characteristics how can I make sure that evary panel access to the variables one's source and the other to the other source?

Even saving values in static variable, is it possible to create a class that "realize" the existence of the other one so chooses to access variables of the second source???

I hope I va been clear enough, if not please tell me :) I wll clarify...

Thanks

+1  A: 

Your class should extend JPanel and get a reference to your "source" in the constructor. Keep this reference in a non-static member variable, so that each instance of the class will have its own reference.

Unless you define static variables, one panel will not know of the existence of the other.

Think about a solution like that (did not try it). The JPanel knows about the SerialHandler, and the SerialHandler has an interface of the JPanel to inform it about events.

public interface SerialEventListener {
    public void someEventOccured( String someEventData );
}

public class SerialHandler {
    private SerialEventListener myEventListener;

    public void setEventListener(SerialEventListener eventListener) {
        myEventListener = eventListener;
    }

    // some event happens in a method:
    // ..
        if( myEventListener != null ) {
            myEventListener.someEventOccured( "someRelevantData" );
        }
    // ..
}

public class SerialPanel extends JPanel implements SerialEventListener {
    private SerialHandler serialHandler;

    public SerialPanel( SerialHandler serialHandler ) {
        this.serialHandler = serialHandler;
        serialHandler.setEventListener( this );
    }

    public void someEventOccured( String someEventData ) {
        // react to event. Use SwingUtilities.invokeLater if you update Swing objects
    }
}

And in your main class:

SerialHandler serialHandler1 = new SerialHandler();
SerialPanel serialPanel1 = new SerialPanel1( serialHandler1 );
Peter Lang
Yes... thanky you...the problem is: I have a main class taht instances both JPanel from one common template class and the main class creates also the serial port class.It means that the new JPanels and the class that manages the serial port doesn't see each other.So..in this case what is your suggstion? To keep classes at minimun as possible? Creating 2 different classes from one class how can both classes speak each other??Thank you very muchHav a nice weekend
Dave
Thank you peter, it didn't solve the problem...or at least not in my case because the structure of my code.However thanks for the hints!!! In my case I created some static variables in the main class (that should be readeded from the panel)But the problem is: how to pass a referment to a static variable in order to be used for the jpanel??Is there a solution?I tried to do that but it seems it doesn t work as expected...(doesn't work at all :) :) :) )Thank you anyway for your help!!!
Dave
I don't think I understand what exactly you are doing. Why do you use static variables?
Peter Lang