1 / 41

Adapter class

Adapter class. Adapter Class. Adapter Classes are used to simplify the process of event handling in Java. When we implement any interface all the methods defined in that interface needs to be overridden in the class, which is not desirable in the case of event handling.

aimee
Download Presentation

Adapter class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Adapter class

  2. Adapter Class • Adapter Classes are used to simplify the process of event handling in Java. • When we implement any interface all the methods defined in that interface needs to be overridden in the class, which is not desirable in the case of event handling. • Adapter classes are useful as they provide empty implementation of all methods in an event listener interface. • In this you can define a new class to act as event listener by extending one of the adapter classes and implementing only those methods that you want to use in your program.

  3. Eg: Mouselistener Methods are as follows: • public void mousePressed(MouseEvent e) • public void mouseClicked(MouseEvent e) • public void mouseReleased(MouseEvent e) • public void mouseEntered(MouseEvent e) • public void mouseExited(MouseEvent e)

  4. import java.awt.*; import java.awt.event.*; public class awt4 extends Frame implements MouseMotionListener{ Frame f; awt4() { f=new Frame("Mouse Event Test"); f.addMouseMotionListener(this); f.setVisible(true); f.setLayout(new FlowLayout()); f.setSize(500,500); } public void mouseDragged(MouseEvent me) {} public void mouseMoved(MouseEvent me) { int x=me.getX(); int y=me.getY(); System.out.println("x is:"+x+"Y is:"+y); } public static void main(String args[]) { awt4 obj1= new awt4(); } }

  5. import java.awt.*; import java.awt.event.*; public class awt5 extends Frame implements MouseMotionListener{ Frame f; awt5() { f=new Frame("Mouse Event Test"); f.addMouseMotionListener(new m1()); f.setVisible(true); f.setLayout(new FlowLayout()); f.setSize(500,500); } class m1 extends MouseMotionAdapter { //public void mouseDragged(MouseEvent me){} public void mouseMoved(MouseEvent me) { int x=me.getX(); int y=me.getY(); System.out.println("x is:"+x+"Y is:"+y); } } public static void main(String args[]) { awt5 obj1= new awt5(); } }

  6. Layout Managers

  7. Layout means the arrangement of components within the container. The task of layouting the controls is done automatically by the Layout Manager. java.awt.LayoutManager

  8. Layouts in AWT 5 Classes in the java packages implement it. • FlowLayout • BorderLayout • CardLayout • GridLayout • GridBagLayout

  9. Flow Layout • Default layout for Panel, Jpaneland Applet • To use FlowLayout in Window, FlowLayout fl = new FlowLayout(); this.setLayout(fl);

  10. this.setLayout(new FlowLayout()); this.add( new Button("Play")); this.add( new Button("Rewind")); this.add( new Button("Fast Forward")); this.add( new Button("Pause")); this.add( new Button("Stop"));

  11. FlowLayout.LEFT, FlowLayout.RIGHT or FlowLayout.CENTER are used to align the components this.setLayout(new FlowLayout(FlowLayout.LEFT)); • Space between the components are given as: FlowLayout fl = new FlowLayout(FlowLayout.LEFT, 20, 10);

  12. Flow Layout pgm import java.applet.*; import java.awt.*; public class layout1 extends Applet { public void init() { // setLayout(new FlowLayout()); [Default] for(inti=1; i<6; i++) { add(new Button("Button " + i)); } } }

  13. Border Layout • Arranges the components into 5 regions: • East , West, North, South, Center • Each region can contain only one component

  14. Border Layout

  15. import java.awt.*; import java.awt.event.*; import java.applet.*; public class awt7 extends Applet { Button b1; public void init() { b1=new Button("Button 1"); setLayout(new BorderLayout()); add(b1,"North"); add(new Button("Button 2"), BorderLayout.SOUTH); add(new Button("Button 3"), BorderLayout.EAST); add(new Button("Button 4"), BorderLayout.WEST); add(new Button("Button 5"), BorderLayout.CENTER); } }

  16. Card Layout • Stacks components on top of each other, displaying the top one

  17. import java.awt.*; import java.awt.event.*; class layoutcard1 extends Frame implements ActionListener { Frame f; Button b1,b2; CardLayout card; layoutcard1() { f=new Frame(); card=new CardLayout(40,30); b1=new Button("b1"); b2=new Button("b2"); f.setSize(300,300); f.setVisible(true); f.setLayout(card); b1.addActionListener(this); b2.addActionListener(this); f.add("card1",b1); f.add("card2",b2); } public void actionPerformed(ActionEventae) { card.next(f); } public static void main(String[] args) { layoutcard1 cl=new layoutcard1(); cl.setSize(400,400); } }

  18. Grid Layout • The grid layout arranges all components in rows and columns, just like a spreadsheet. • But, unlike a spreadsheet, the cells are always the same size. • The size of a cell is automatically calculated from the components it contains. • Ignores the preferred size of the component; each component is resized to fit into its grid cell

  19. panel.setLayout(new GridLayout(5, 4); specifies a grid with five rows and four columns. • You can specify the horizontal and vertical gaps you want. panel.setLayout(new GridLayout(5, 4, 3, 2);

  20. public class GridTest extends Applet { public void init() { setLayout(new GridLayout(2,3)); // 2 rows, 3 cols add(new Button("Button One")); add(new Button("Button Two")); add(new Button("Button Three")); add(new Button("Button Four")); add(new Button("Button Five")); add(new Button("Button Six")); } }

  21. Menu import java.awt.*; class awt6 extends Frame { awt6() { MenuBar mbar= new MenuBar(); Frame f= new Frame(); f.setMenuBar(mbar); f.setLayout(new FlowLayout()); f.setVisible(true); f.setSize(500,600); Menu File= new Menu("File"); MenuItemi1,i2; File.add(new MenuItem("New")); File.add(new MenuItem("Open")); mbar.add(File); } public static void main(String args[]) { awt6 obj=new awt6(); } }

  22. GridBagLayout • With the GridBagLayout manager, a Container is divided into a grid of rectangular cells, in which a Component occupies all or a part of a contiguous group of cells. • A gridbag layout has a rectangular grid of cells. Its components (buttons, lists, text, etc) are not all the same size. • A component can occupy one or more cells. • Position, size, and properties of components are determined by setting the GridBagConstraints of the GridBagLayout to particular values before the component is added to the container.

  23. 1 2 3 1 2 3 4

  24. • create a GridBagLayout object. The number or rows and columns of the underlying grid are automatically calculated from the information we give later • set this GridBagLayout object to the layout manager for this panel • create a GridBagConstraints object. The GridBagConstraints object specifies how components are set out in the grid • for each component we set the grid bag constraints that specify where it is to be placed on the grid, how many rows and columns it occupies, and whether it is to be resized to fully occupy its display area

  25. GridBagConstraints specify: 1. Location and size of the component on the grid. 2. Position and size of the component within the rectangular region specified in 1. 3. Behavior of the component and its region when the container is resized.

  26. Steps for Using a GridBag • Create a GridBagLayout object, say gbl, and set it to be the layout manager of the Container. GridBagLayoutgbl = new GridBagLayout(); setLayout(gbl); • Create a GridBagConstraints object, say gbc, and provide values for its public instance variables for each Component, in turn, to be placed on the Container. GridBagConstraintsgbc = new GridBagConstraints();

  27. Steps for Using a GridBag • Place component using gbl.setConstraints(component, gbc); add(component); or add(component, gbc); • Repeat the process for each component

  28. import java.awt.*; import java.awt.event.*; import java.applet.*; public class gridbaglayout1 extends Applet implements ActionListener { Button B1 = new Button("NE"); Button B2 = new Button("SouthWest"); GridBagLayoutgridbag; public void init() { setup_layout(); } private void setup_layout() { setBackground(Color.yellow); gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); c.weightx = 1; c.weighty = 1; c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.SOUTHWEST; gridbag.setConstraints(B1,c); B1.setBackground(Color.cyan); add(B1); c.weightx = 0; c.gridx = 1; c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.BOTH; gridbag.setConstraints(B2,c); B2.setBackground(Color.pink); add(B2); B1.addActionListener(this); B2.addActionListener(this); } public void actionPerformed(ActionEvent e) { GridBagConstraints c = gridbag.getConstraints(B1); if (e.getSource() == B1) { c.anchor = GridBagConstraints.NORTHEAST; showStatus("NE pushed"); } else if (e.getSource() == B2) { c.anchor = GridBagConstraints.SOUTHWEST; showStatus("SouthWest pushed"); } gridbag.setConstraints(B1,c); invalidate(); validate(); } }

  29. gridx, gridy • Specify the row and column at the upper left of the component • Fill • Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component • ipadx, ipady • Specifies the internal padding: how much to add to the minimum size of the component. • Anchor • Used when the component is smaller than its display area to determine where (within the area) to place the component. • weightx, weighty • Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); 

  30. SWINGS

  31. Swing is a part of JFC(Java Foundation Classes) that is used to create GUI application. It is built on the top of AWT and entirely written in java. • Contains more built-in controls like tabbed panes, sliders, color choosers, lists, trees ,tables etc. • Increased customization of components like border styles, text alignments etc. Images can be added to almost any control. • “Look and Feel” is not native like in the case of AWT.

  32. MVC Architecture • Model-View-Controller Architecture

  33. MVCallows for the separation of three common features of GUI applications: • the data access (typically via a database) • the business logic (how the data will be used) • user interaction (how the data and actions will be visually presented)

  34. The components are: • Model: This provides the means by which data is retrieved and manipulated. • View: This represents the visual interface components of the Graphical User Interface (GUI) application which interact with the user. The interaction is of an event-driven nature where actions are initiated via keyboard and mouse. • Controller: This joins the Model with the View and is the heart of the control logic by associating user-generated events with data actions.

  35. The interactions correspond to the diagram as follows: • All actions begin in the view through events generated by the user. The controller provides listeners for the events. The controller also has the ability to manipulate the state of the view objects in a way which offers a response to the events generated by the user. • The controller interacts with the model by either requesting information from the data source based on user-generated events, or by modifying the data based on these events. • The model provides the programming interface which the controller must use to access the database, i.e., the controller does not interact directly with the database. In particular the notion of "connection" is never seen in the controller. Furthermore, the model also provides means to avoid, in most cases, direct SQL queries to the database. • The view interacts with the model only to know about type information and other data abstractions held in the model. The relatively weak link indicates that the View/Model interaction should be "minimal" because the manipulation of data is to be done within the event handlers of the controller.

  36. MVC separates the classes into corresponding packages: • the controller package holds at least one Controller class which contains the main function used to initiate the application. • the views package holds at least one class, TheFrame, an extension of the JFrame class. • the models package holds classes, typically in which JDBC is used to access database records, tables and connection.

More Related