Sample2
). Their respective buttons
are created inside a Swing JPanel, which automatically aligns them
as seen in the above illustration. InteractorListener
, add the zoomInteractor
and unzoomInteractor
fields, and add the necessary interactor button fields to the Sample3
application. public class Sample3 extends JFrame implements InteractorListener { IlvManager manager; IlvManagerView mgrview; IlvSelectInteractor selectInteractor; IlvManagerViewInteractor zoomInteractor, unZoomInteractor; Button selectButton, zoomButton, unZoomButton; .... }
createInteractorButtons
method will create three buttons (Select, -, and +) that will be
stored in the selectButton
, zoomButton
, and unZoomButton
fields of the object. void createInteractorButtons() { Panel buttons = new Panel(); selectButton = new Button("Select"); selectButton.setBackground(Color.gray); selectButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (selectInteractor == null) selectInteractor = new IlvSelectInteractor(); if (mgrview.getInteractor() != selectInteractor) mgrview.setInteractor(selectInteractor); } } }); buttons.add(selectButton); unZoomButton = new Button("-"); unZoomButton.setBackground(Color.gray); unZoomButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (unZoomInteractor == null) unZoomInteractor = new IlvUnZoomViewInteractor(); if (mgrview.getInteractor() != unZoomInteractor) mgrview.setInteractor(unZoomInteractor); } }); buttons.add(unZoomButton); zoomButton = new Button("+"); zoomButton.setBackground(Color.gray); zoomButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (zoomInteractor == null) zoomInteractor = new IlvZoomViewInteractor(); if (mgrview.getInteractor() != zoomInteractor) mgrview.setInteractor(zoomInteractor); } }); buttons.add(zoomButton); getContentPane().add(buttons, BorderLayout.SOUTH); }
IlvManagerView
class. Sample3.java
file,
you can see that the class implements the interface InteractorListener
. You may also have noticed the import of a new package, package-summary, which is the package that contains the Rogue Wave JViews event classes. The InteractorListener
interface includes one method: interactorChanged
method
will be called when the interactor changes on the view (as soon as
the object, the instance of Sample3
, is
registered as a listener; see Registering the listener). The parameter is an event that contains the old and the new interactor.
You simply change the background color of the button corresponding
to the newly installed interactor to red. public void interactorChanged(InteractorChangedEvent event) { IlvManagerViewInteractor oldI = event.getOldValue(); IlvManagerViewInteractor newI = event.getNewValue(); if (oldI == selectInteractor) selectButton.setBackground(Color.gray); else if (oldI == zoomInteractor) zoomButton.setBackground(Color.gray); else if (oldI == unZoomInteractor) unZoomButton.setBackground(Color.gray); // there is no new interactor if (newI == null) return; if (newI == selectInteractor) selectButton.setBackground(Color.red); else if (newI == zoomInteractor) zoomButton.setBackground(Color.red); else if (newI == unZoomInteractor) unZoomButton.setBackground(Color.red); }
addInteractorListener
method
in the init
method.
...
manager = new IlvManager();
try {
manager.read("data/lou.ivl");
} catch (Exception e) {}
mgrview = new IlvManagerView(manager);
setLayout(new BorderLayout(0,0));
getContentPane().add(new IlvJScrollManagerView(mgrview),
BorderLayout.CENTER);
createButtons();
mgrview.addInteractorListener(this);
...