Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin.

Similar presentations


Presentation on theme: " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin."— Presentation transcript:

1  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin College

2  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(2/20) Review: Widgets & Events Almost everything in Java is-an Object … When the user interacts with a Java widget, the JRE generates an event… Object Component Container JComponent AbstractButton JButton java.util.EventObject java.awt.AWTEvent java.awt.event.ActionEvent javax.swing.ChangeEvent

3  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(3/20) Review: Java User Interface Widgets Original Java had the awt package of user-interface widgets: Java 1.2 added the swing package of lighter-weight widgets swing package Object Component ButtonCanvasCheckboxScrollbar … Container PanelScrollPaneWindow … JComponent AbstractButton JButton JFile Chooser JLabelJPanelJScrollPaneJTabbedPane …

4  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(4/20) Java encourages UI designers to use the MVC design pattern, in which: Review: MVC The application’s core functionality is isolated in a model class: This pattern lets you modify the UI (or add additional ones) without touching the app’s core functionality. The application’s user-interface is isolated in a view class: The application’s control is isolated in a controller class: Model Object View JFrame Controller i View i Controller Listener

5  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(5/20) Listeners  Events  Widgets Swing provides many widgets, that trigger various events ListenerEventWidgets ActionListener ActionEventJButton JComboBox JTextField JCheckBox AdjustmentListener AdjustmentEvent JScrollbar ChangeListener ChangeEventJSlider KeyListener KeyEventkeyboard MouseListener MouseEventmouse MouseMotionListener MouseEventmouse JRadioButton JMenu

6  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(6/20) Inner Classes It is sometimes convenient to nest a class within a class: The inner class is another attribute of the class (and so is usually declared private). public class OuterClass { // instance variables // methods // etc. } private class InnerClass { // instance variables // methods // etc. } It can access any of the attributes of the outer class!

7  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(7/20) Inner Classes: Example A GUI can define inner classes for its widgets’ listeners: public class ListenersExample1 extends JFrame { private JComboBox myBox; // generates ActionEvents private JSlider mySlider; // generates ChangeEvents... }... private class BoxListener implements ActionListener { public BoxListener() {} public void actionPerformed(ActionEvent ae) {... } } private class SliderListener implements ChangeListener { public SliderListener() {} public void stateChanged(ChangeEvent ce) {... } } myBox.addActionListener( new BoxListener() ); mySlider.addChangeListener( new SliderListener() );

8  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(8/20) Exercise: Part I Take a few minutes to work through part I of today’s exercise.

9  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(9/20) Each anonymous inner class defines the method needed to implement the listener-interface needed by that widget. Anonymous Inner Classes Unlike C++, Java lets you create anonymous inner classes: public class ListenersExample1 extends JFrame { private JComboBox myBox; // generates ActionEvents private JSlider mySlider; // generates ChangeEvents... } myBox.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) {... } } ); mySlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) {... } } );...

10  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(10/20) Anonymous Inner Class Pattern The pattern for an anonymous inner class declaration is: new SuperType ( SuperTypeConstructorParameters ) { // inner class methods, attributes, etc. } If SuperType is an interface, then the anonymous class implements that interface. For readability: Use anonymous inner classes sparingly… If SuperType is an existing class, then the anonymous class extends that class: Employee e1 = new Employee("John"), // normal e2 = new Employee("Jane") {... }; // uh-oh

11  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(11/20) Exercise: Part II Take a few minutes to work through part II of today’s exercise.

12  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(12/20) Example: SliderExample Let’s build a GUI application with three color-control sliders: We will need:  A JFrame  Several JLabel s  Three JSlider s  Some JPanel s (one per label+slider) SliderExample To change my background, use the sliders below: R G B Since there is not much in the way of a “core application”, we will build our model, view, and controller in 1 class…

13  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(13/20) Example: SliderExample (ii) We start by defining our class and its attributes: import javax.swing.*; // JLabel, JSlider,... import java.awt.*; // Color, GridLayout,... import javax.swing.event.*; class SliderExampleA extends JFrame implements ChangeListener { private JLabel myLabel; private JSlider myRedSlider, myGreenSlider, myBlueSlider; private JPanel myPane, myRedPanel, myGreenPanel, myBluePanel; private int myRed, myGreen, myBlue;...

14  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(14/20) Example: SliderExample (iii) Next, its constructor: public SliderExampleA() { myPane = new JPanel( new GridLayout(4,1) ); myLabel = new JLabel(" To change my background color," + " use the RGB sliders below: ", JLabel.CENTER); myPane.add(myLabel);... Note that we can use HTML to format the text in a JLabel, a JButton, and other Swing widgets

15  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(15/20) Example: SliderExample (iv) //... Continuation of constructor myRedSlider = new JSlider(0, 255); myRedPanel = buildAndAddPanel("R", myRedSlider); myGreenSlider = new JSlider(0, 255); myGreenPanel = buildAndAddPanel("G", myGreenSlider); myBlueSlider = new JSlider(0, 255); myBluePanel = buildAndAddPanel("B", myBlueSlider); myRed = myGreen = myBlue = 255; this.setContentPane(myPane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }...

16  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(16/20) Example: SliderExample (v)... private JPanel buildAndAddPanel(String s, JSlider js) { JPanel jp = new JPanel(); JLabel aLabel = new JLabel(s, JLabel.RIGHT); jp.add(aLabel); js.setValue(255); js.setPaintTicks(true); js.setMajorTickSpacing(20); js.setMinorTickSpacing(5); js.addChangeListener(this); jp.add(js); myPane.add(jp); return jp; }

17  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(17/20) Example: SliderExample (vi)... public void stateChanged(ChangeEvent ce) { Object es = ce.getSource(); if (es instanceof JSlider) { JSlider js = (JSlider) es; if ( js == myRedSlider ) { // pointer comparison myRed = myRedSlider.getValue(); } else if ( js == myGreenSlider ) { myGreen = myGreenSlider.getValue(); } else if ( js == myBlueSlider ) { myBlue = myBlueSlider.getValue(); } else { //...error-handling code omitted here myPane.setBackground( new Color(myRed, myGreen, myBlue) ); } We could define our slider-event-handler this way…

18  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(18/20) … and we’re finished! Example: SliderExample (vii)... public static void main(String [] args) { SliderExampleA gui = new SliderExampleA(); gui.pack(); gui.setVisible(true); } } // class SliderExampleA Finally, we define our main() method: … but inner classes give us a cleaner way to do so (as we’ll see in the exercises).

19  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(19/20) Summary Java’s Swing package provides a wide variety of widgets. Some widgets generate different kinds of events, which must be listened for with different kinds of listeners. Java’s inner classes are similar to C++ nested classes, but with more capabilities. Unlike C++, Java supports anonymous inner classes that implement an interface or extend an existing class. Many widgets generate ActionEvents, which must be listened for with an ActionListener.

20  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(20/20) Exercise: Part III Use the remaining time to work through part III of today’s exercise.


Download ppt " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin."

Similar presentations


Ads by Google