Presentation is loading. Please wait.

Presentation is loading. Please wait.

Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.

Similar presentations


Presentation on theme: "Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications."— Presentation transcript:

1 Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications in that they have a component (the JComponent object) which is being placed in a container (the JFrame) for display. A GUI component object is a screen element which is used to display information, or allow the user to interact with the program in a special way (Examples of components are JComponent objects, buttons, scroll bars, text fields and radio buttons.) A container is a special type of component that is used to hold and organize other components. Frames and panels are two examples of Java containers.

2 Swing GUI Components You can create GUI components to place in a container using classes available in the Swing package ( javax.swing ) Class names start with J : JTextField, JLabel, JButton Objects are created by calling a class constructor: JLabel xLabel = new JLabel("x = "); JTextField xField = new JTextField(width); JButton moveButton = new JButton("Move",buttonIcon); JButton anotherButton = new JButton( “ go ” );

3 import javax.swing.JButton; import javax.swing.JFrame; // Demonstrates how to create and display // a button public class ButtonTester{ private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); JButton button = new JButton("Click me!"); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } Creates a frame with a button on it. BUT, nothing happens when this button is pressed ………. We need to provide code which can handle this event!!

4 Java GUI applications are event driven. They: *generate event objects when an ‘event’ happens *execute code is response to the event Typical events are moving the mouse, ‘pressing’ a button, typing in a text field, a mouse click …. you get the idea When an event occurs, an event object is created which stores information about that event. The program may then respond to the event by executing a predetermined set of statements.

5 The Event Class The class from which the event object is created depends upon the type of event which has occurred. An ActionEvent object is created if a button is pressed, or textfield data has been entered. A MouseEvent object is created if the event involved the mouse. *these event classes are provided by java.awt.event package.

6 The Event Source The event source is the visual component with which the user has interacted. For example: a button object is the event source of a button press a JComponent object is the event source of a mouse click The event source generates the event.

7 The Event Listener An event listener is an object that contains code which is to be executed when a particular event occurs. When an event occurs, if there is a event listener object waiting for that event, it will be used to react to that event. For example, if a button is pressed, an event is created, and if there is an event listener object waiting for that button to be pressed, it will provide the code that is to be executed.

8 How does it work? An event source (eg. a particular button) keeps an internal list of listener objects which are available for responding to any events that it may generate. If an event occurs (eg. the button is pressed) then listener objects on that list are ‘notified’. Any listener that is ‘waiting’ for this particular event may then respond. What does the programmer do? The programmer must: * Create the event source * provide a class from which a listener object can be created (a listener class) * create and ‘install’ the listener object with the event source

9 Back to the issue … we need to create a class, from which an event listening object can be created. Then we need to install (add) an object of this type to our button’s list of listeners. public class ButtonTester{ private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); JButton button = new JButton("Click me!"); // button needs listener!! frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

10 A JButton object will generate an ActionEvent object if an event occurs. So the event listening object must be of type ActionListener. // ActionListener is an interface. public interface ActionListener { void actionPerformed(ActionEvent event); } Need to supply a class which implements ActionListener. So it must contain the method actionPerformed. This method will contain the instructions to be executed when the button is clicked.

11 * we need to create a class, from which an ActionListener object can be created. public class ButtonListener implements ActionListener { void actionPerformed(ActionEvent event){ System.out.println(“I was clicked”); } import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

12 * we need to install (add) this object to our button’s list of listeners. public class ButtonTester{ private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); JButton button = new JButton("Click me!"); ButtonListener blisten = new ButtonListener(); button.addActionListener(blisten); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

13 Figure 1: Implementing an Action Listener Output:

14 * Which objects are the event source and the event listener in the ButtonTester program? * Why is it legal to assign a ButtonListener object to a variable of type ActionListener ? button is the event source. bListen is the event listener object. Because ButtonListener implements the ActionListener interface, so a ButtonListener object is also an object of type ActionListener.

15 Suppose we were creating an application with 15 buttons, and a number of other components. We do not want to put all of our components directly on the frame, as they may overwrite each other, and we may like to organize these components in some way. We will use another container, a JPanel to hold our components, and then put the JPanel object on our frame. JPanel panel = new JPanel(); panel.add(button1); panel.add(button2); frame.add(panel);

16 Let’s code an application with 2 buttons, titled “RED” and “BLUE” -- and one label. When a button is pushed, the text on the label will be updated to the color indicated on the pushed button.

17 import javax.swing.*; // First create the components, put them on a panel, and // put the panel on the frame public class Button4{ public static void main(String[] args) { JFrame frame = new JFrame("Four buttons"); frame.setSize(200,150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel outlbl = new JLabel("Look at me!"); // create JButton redbtn = new JButton("RED"); JButton bluebtn = new JButton("BLUE"); JPanel panel = new JPanel(); //for organization panel.add(redbtn); panel.add(bluebtn); panel.add(outlbl); frame.add(panel); //frame will display panel frame.setVisible(true); }

18 so far we have created a frame which displays 2 buttons and a label -- but nothing happens when the buttons are pushed!! we need to create ActionListener objects, and install one on each button. This is a bit different than the last button program… because these listener objects will need access to the LABEL!! SO… they cannot be in separate files.. They must be inner classes…..

19 public class ButtonFour{ public static void main(String[] args) { // statements which create and set up frame final JLabel outlbl = new JLabel("Look at me!"); // this variable will be used inside the inner listener classes, // so it MUST be declared as Final JButton redbtn = new JButton("RED"); // create listener class for red button class RedListener implements ActionListener { public void actionPerformed (ActionEvent e) { outlbl.setForeground(Color.red); outlbl.setText("RED"); } // create listener object and install RedListener rlisten = new RedListener(); redbtn.addActionListener(rlisten);

20 JButton bluebtn = new JButton("BLUE"); // create listener class for blue button class BlueListener implements ActionListener { public void actionPerformed (ActionEvent e) { outlbl.setForeground(Color.blue); outlbl.setText("Blue"); } // create listener object and install BlueListener blisten = new BlueListener(); bluebtn.addActionListener(blisten); JPanel panel = new JPanel(); // all components go on panel panel.add(redbtn); panel.add(bluebtn); panel.add(greenbtn); frame.add(panel); frame.setVisible(true); } ys so far we have created a frame which displays


Download ppt "Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications."

Similar presentations


Ads by Google