JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ •...

22
Java Programming Unit 5 Intro to GUI. Listeners (c) Farata Systems, L.L.C. 2011

Transcript of JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ •...

Page 1: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Java  Programming    Unit  5  

Intro  to  GUI.  Listeners    

(c)  Farata  Systems,  L.L.C.    2011  

Page 2: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

AWT,  Swing,  JavaFX  

•  First  there  was  Abstract  Windowing  Toolkit  (AWT)    

•  Swing  library  of  components  has  replaced  AWT,  but  sMll  uses  some  of  AWT’s  classes    

•  The  next  incarnaMon  of  Java  UI  is  called  JavaFX  and  its  version  2.0  will  be  released  in  2011.  It’s  an  aPempt  to  compete  with  Adobe  Flex  and  MicrosoR  Silverlight.  

(c)  Farata  Systems,  L.L.C.    2011  

Page 3: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

HelloWorld  in  Swing  

(c)  Farata  Systems,  L.L.C.    2011  

import  javax.swing.JFrame;      public  class  HelloWorld  extends  JFrame  {              public  HelloWorld(){            setSize(200,300);            setTitle("Hello  World");              setVisible(true);          }  

 public  staMc  void  main(String[]  args)  {            HelloWorld  myHello  =  new  HelloWorld();    }  

}  

Add  UI  controls  to  a  container  (e.g.  JFrame,  JPanel),  for  example:    JBuPon  myBuPon  =  new  JBuPon  ("Click  me");      add(myBuPon);    

Page 4: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Layout  Managers  Arrange  UI  Components  inside  the  Container    

1.  Create  an  instance  of  JPanel    2.  Assign  a  layout  manager  to  it    3.    InstanMate  some  Swing  controls  and  add  them  to  the  panel.      4.  Add  the  panel  to  the  top-­‐level  container  -­‐  JFrame  -­‐  by  calling  setContentPane()  method.    5.  Set  the  frame’s  size  and  make  it  visible.    

(c)  Farata  Systems,  L.L.C.    2011  

How  to  add  controls  to  a  panel  and  then  a  panel  to  a  frame:  

Page 5: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Two  tasks  of  UI  programming  

 1.  Create  a  nice  looking  layout  of  your  GUI  components    2.  Write  the  code  to  react  on  user’s  and  system  events    

(c)  Farata  Systems,  L.L.C.    2011  

Page 6: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Calculator’s  UI  with  FlowLayout  

(c)  Farata  Systems,  L.L.C.    2011  

import  javax.swing.*;  import  java.awt.FlowLayout;      public  class  SimpleCalculator  {    public  staMc  void  main(String[]  args)  {      //  1.  Create  a  panel  

 JPanel  windowContent=  new  JPanel();        

   //  2.  Set  a  layout  manager  for  this  panel    FlowLayout  fl  =  new  FlowLayout();      windowContent.setLayout(fl);  

     //  3.  Create  controls  in  memory    

 JLabel  label1  =  new  JLabel("Number  1:");    JTextField  field1  =  new  JTextField(10);    JLabel  label2  =  new  JLabel("Number  2:");    JTextField  field2  =  new  JTextField(10);    JLabel  label3  =  new  JLabel("Sum:");    JTextField  result  =  new  JTextField(10);    JBuPon  go  =  new  JBuPon("Add");        

   //  4.  Add  controls  to  the  panel    windowContent.add(label1);      windowContent.add(field1);    windowContent.add(label2);    windowContent.add(field2);    windowContent.add(label3);    windowContent.add(result);    windowContent.add(go);  

//5.    Create  the  frame  and  add  the  panel  to  it        JFrame  frame  =  new  JFrame("My  First  Calculator");          //  6.  Add  the  panel  to  the  top-­‐level  container      frame.setContentPane(windowContent);  

         //  7.  set  the  size  and  make  the  window  visible      frame.setSize(400,100);      frame.setVisible(true);    }  }    }      

!

Page 7: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Swing  Layout  Managers  

(c)  Farata  Systems,  L.L.C.    2011  

•  FlowLayout  •  GridLayout  •  BoxLayout  •  BorderLayout  •  CardLayout  •  GridBagLayout      First,  instanMate  a  layout  manager,  and  then  assign    this  instance  to  a  container  by  calling    setLayout().      

Page 8: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

GridLayout  

(c)  Farata  Systems,  L.L.C.    2011  

Say,  your  container  needs  to  allocate  8  elements  of  the  same  size,  you  may  do  it  in  four  columns  and  two  rows.    4x2=8  cells                    JPanel  windowContent=  new  JPanel();      

 //  Set  the  layout  manager  for  the  panel    GridLayout  gl  =  new  GridLayout(4,2);      windowContent.setLayout(gl);  

                 //  Code  to  add  components  to  the  panel  goes  here      

!

//  To  disallow  resizing  the  window  frame.setResizable(false);  

Page 9: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Walkthrough  1  

(c)  Farata  Systems,  L.L.C.    2011  

1.  Download  and  import  into  Eclipse  the  source  code  of  the  Lesson8    2.  Run  SimpleCalculator.  Try  to  stretch  the  window  and  observe          the  change  in  the  window  layout.    3.  Run  SimpleCalculatorGrid.  Try  to  stretch  the  window  and  observe              the  change  in  the  window  layout.    

Page 10: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

BorderLayout  

(c)  Farata  Systems,  L.L.C.    2011  

BorderLayout  divides  a  UI  container  into  imaginary  areas  South,  West,  North,  East,  and  Center.      

!

Add  components  to  all  or  some  of  these    areas  in  your  container.      The  calculator  below  uses  only  the  North    and  Center  areas.    The  Center  area  uses    the  GridLayout  for  allocaMng  the  buPons.  

Page 11: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Card  Layout  

•  Think  of  a  deck  of  cards:  only  the  top  one  is  visible    

•  Use  CardLayout  if  you  need  to  display  a  number  of  panel  one  at  a  Mme    

•  See  the  CardLayout  demo  at  hPp://download.oracle.com/javase/tutorial/uiswing/layout/card.html    

(c)  Farata  Systems,  L.L.C.    2011  

Page 12: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Absolute  Layout  

It’s  like  not  having  any  layout.  You  are  responsible  for  specifying  X  and  Y  coordinates  of  each  component  :    windowContent.setLayout(null);    …    JBuPon  myBuPon  =  new  BuPon("New  Game");          myBuPon.setBounds(100,200,40,20);  

(c)  Farata  Systems,  L.L.C.    2011  

Page 13: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

GridBagLayout  Allows  laying  out  components  of  different  sizes  by  assigning  constraints  to  each  grid  element.    For  example  this  cell  will  be  twice  as  wide  as  regular  cells  in  a  grid  

(c)  Farata  Systems,  L.L.C.    2011  !

Page 14: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Using  GridBagConstraints  class  

(c)  Farata  Systems,  L.L.C.    2011  

//  Set  the  GridBagLayout  for  the  window’s  content  pane    GridBagLayout  gb  =  new  GridBagLayout();    this.setLayout(gb);      //  Create  an  instance  of  the  GridBagConstraints  //  You’ll  have  to  repeat  these  lines  for  each  component  //  that  you’d  like  to  add  to  the  grid  cell    GridBagConstraints  constr  =  new  GridBagConstraints();      //seNng  constraints  for  the  Calculator’s  displayField:            constr.x=0;  //  x  coordinate  of  the  cell  in  the  grid    constr.y=0;  //  y  coordinate  of  the  cell  in  the  grid                        //  this  cell  has  the  same  height  as  other  cells    constr.gridheight  =1;      //  this  cell  is  as  wide  as  6  others    constr.gridwidth=  6;                    //  fill  all  space  in  the  cell    constr.fill=  constr.BOTH;    

//  proporSon  of  horizontal  space    taken  by    this  //  component    constr.weightx  =  1.0;              //  proporSon  of    verScal  space  taken  by    this  component            constr.weighty  =  1.0;              //  posiSon  of  the  component  within  the  cell    constr.anchor=constr.CENTER;              displayField  =  new  JTextField();    //  set  constrains  for  this  field    gb.setConstraints(displayField,constr);        //  add  the  text  field  to  the  window    windowContent.add(displayField);      

Page 15: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Event  Listeners  

•  There  are  2  types  of  events:  user  and  system  generated    

•  Click  on  the  buPon  is  an  event,  and  if  you  want  to  process  it,  create  an  AcMonListener  for  this  buPon.    

•  To  process  mouse  moves,  create  a  MouseMoMonListener,  etc.  

(c)  Farata  Systems,  L.L.C.    2011  

Page 16: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

AcMonListener  Interface  

(c)  Farata  Systems,  L.L.C.    2011  

This  interface  declares  just  one  callback  method:  acMonPerformed():      public  interface  AcMonListener  extends  EventListener              void  acMonPerformed(AcMonEvent  e);  }    

To  process  buPon  events  in  your  Calculator,  there  should  be  some  class  that  implements  the  AcMonListener.  It  can  be  the  same  class  or  another  one,  i.e.  CalculatorEngine      public  class  CalculatorEngine  implements  AcMonListener  {            public  void  acMonPerformed(AcMonEvent  e){                //  Place  the  click-­‐processing    code  here            }  }    

Page 17: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Register  components  with  listeners  

(c)  Farata  Systems,  L.L.C.    2011  

 CalculatorEngine  calcEngine  =  new  CalculatorEngine(this);            

buPon0.addAcMonListener(calcEngine);  buPon1.addAcMonListener(calcEngine);  buPon2.addAcMonListener(calcEngine);  

The  code  above  will  be  located  inside  the  class  Calculator  

Page 18: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Finding  the  source  of  the  event  

(c)  Farata  Systems,  L.L.C.    2011  

public  class  CalculatorEngine  implements  AcMonListener  {                public  void  acMonPerformed(AcMonEvent  e){                    //  Get  the  source  of  this  acMon            JBuWon  clickedBuWon=(JBuWon)  e.getSource();                            //  Get  the  buPon's  label                  String  clickedBuPonLabel  =                                                              clickedBuPon.getText();                      //  Concatenate  the  buPon's  label                //  to  the  text  of  the  message  box                  JOpMonPane.showConfirmDialog(null,                                "You  pressed  "  +  clickedBuPonLabel,                                "Just  a  test",                                JOpMonPane.PLAIN_MESSAGE);        }  }    

Page 19: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Passing  Data  Between  Objects  

(c)  Farata  Systems,  L.L.C.    2011  

Say,  you  need  to  reach  a  field  in  the  Calculator  from  the  CalculatorEngine.      You  can  pass  the  reference  of  one  object  to  another,  e.g.  the  Calculator  instance    passes  the  reference  to  itself  to  the  CalculatorEngine:    CalculatorEngine  calcEngine  =  new  CalculatorEngine  (this);      The  engine’s  constructor  can  store  the  value  from  the  variable  this  in  its  own  variable,    say  parent,  and  use  it  in  the  method  acMonPerformed()  to  access  the  calculator’s    display  field.    Bad  pracSce:  parent.displayField.getText();    Never  try  to  access  children  of  another  object  directly.  Add  some  public  methods    to  do  it.    

Page 20: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Adding  Public  API  to  Calculator  

(c)  Farata  Systems,  L.L.C.    2011  

public  class  Calculator{          private  JTextField  displayField;              public  void  setDisplayValue(String  val){                  displayField.setText(val);          }              public  String  getDisplayValue()  {                  return  displayText.getText();          }                //  The  rest  of  the  code  goes  here  }    

Page 21: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Walkthrough  2  

1.  Download  and  import  the  code  from  Lesson  9  and  review  it  with  the  instructor    2.  Run  the  Calculator  program  and  see  if  it  reacts  to  the  buPon  clicks.  

(c)  Farata  Systems,  L.L.C.    2011  

Page 22: JavaProgramming$$ Unit5$myflex.org/yf/java/Unit5_GUI.pdf · AWT,Swing, JavaFX$ • Firstthere$was$AbstractWindowing$Toolkit(AWT)$ $ • Swing$library$of$components$has$replaced$AWT,$

Homework  

 1.  Get  familiar  with  the  GridBagLayout    2.  Do  the  assignment  from  the  Try  It  secMon  from  Lesson  8  and  9  from  the  textbook    Do  addiMonal  reading  about    Java  Swing  library:  hPp://java.sun.com/docs/books/tutorial/uiswing.    

(c)  Farata  Systems,  L.L.C.    2011