/* * This file is part of the Echo Web Application Framework (hereinafter "Echo"). * Copyright (C) 2002 NextApp, Inc. * * Echo is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Echo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Echo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import nextapp.echo.Button; import nextapp.echo.Color; import nextapp.echo.ContentPane; import nextapp.echo.EchoInstance; import nextapp.echo.Label; import nextapp.echo.Window; import nextapp.echo.event.ActionEvent; import nextapp.echo.event.ActionListener; import nextapp.echoservlet.EchoServer; public class ButtonDemoServlet extends EchoServer { public EchoInstance newInstance() { return new ButtonDemo(); } } // Note that ButtonDemo implements the interface // ActionListener. class ButtonDemo extends EchoInstance implements ActionListener { private Button redButton; private Button greenButton; private Button blueButton; private ContentPane content; // The actionPerformed method is required by the // interface ActionListener. In this case, the three // buttons register this class as an ActionListener. // This method will be called when any of the buttons // is clicked. An ActionEvent will be passed in that // describes the action, and will contain information // as to which object (button) created the event (was // pressed). public void actionPerformed(ActionEvent e) { if (e.getSource() == redButton) { content.setBackground(Color.RED); } else if (e.getSource() == greenButton) { content.setBackground(Color.GREEN); } else if (e.getSource() == blueButton) { content.setBackground(Color.BLUE); } } public Window init() { // The constructor for Window can specify a title. Window window = new Window("Button Demo"); content = new ContentPane(); window.setContent(content); Label label = new Label( "Click on a button to change the background" + " color of this window:"); content.add(label); // Create the red button with the text "Red" as its // label. redButton = new Button("Red"); // Adds an action listener to the button, this // object. The button will call the // actionPerformed() method of an action listener // when it is clicked. redButton.addActionListener(this); // Set the red button's background to be red. redButton.setBackground(Color.RED); // Add the red button to the content pane. content.add(redButton); // and so on... greenButton = new Button("Green"); greenButton.addActionListener(this); greenButton.setBackground(Color.GREEN); content.add(greenButton); blueButton = new Button("Blue"); blueButton.addActionListener(this); blueButton.setBackground(Color.BLUE); content.add(blueButton); return window; } }