Advanced Graphical User Interface Components - Java Handout 12 | CS 402, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Advanced Studies II; Subject: Computer Science; University: Illinois Institute of Technology; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-mrw
koofers-user-mrw 🇺🇸

10 documents

1 / 77

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS402
Java Handout # 12
Advanced Graphical User Interface
Components
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d

Partial preview of the text

Download Advanced Graphical User Interface Components - Java Handout 12 | CS 402 and more Study notes Computer Science in PDF only on Docsity!

CS

Java Handout # 12

Advanced Graphical User Interface

Components

Outline

• Introduction

• JTextArea

  • Example: TextAreaDemo

• JPanel

  • Example: CustomPanel and CustomPanelTest
  • Example: SelfContainedPanel and SelfContainedPanelTest

• JSlider

  • Example: SelfContainedPanel

• Designing Programs that Execute as Applets or

Applications

  • Example: DrawShapes

Introduction

• Advanced GUI components

– Text areas

– Sliders

– Menus

• Advanced layout managers

– BoxLayout

– CardLayout

– GridBagLayout

JTextArea

• JTextArea

  • Area for manipulating multiple lines of text
  • Inherits from JTextComponent

Example: TextAreaDemo

22 String string = "This is a demo string to\n" + 23 "illustrate copying text\n" + 24 "from one TextArea to \n" + 25 "another TextArea using an\n" + "external event\n"; 26 27 // set up textArea 28 textArea1 = new JTextArea( string, 10, 15 ); 29 box.add( new JScrollPane( textArea1 ) ); 30 31 // set up copyButton 32 copyButton = new JButton( "Copy >>>" ); 33 copyButton.addActionListener( 34 36 new ActionListener() { 37 38 // set text in textArea2 to selected 39 // text from textArea 40 public void actionPerformed( ActionEvent event ) 41 { 42 textArea2.setText( textArea1.getSelectedText() ); 43 }

Example: TextAreaDemo

45 } // end anonymous inner class 46 47 ); // end call to addActionListener 48 49 box.add( copyButton ); 50 51 // set up textArea 52 textArea2 = new JTextArea( 10, 15 ); 53 textArea2.setEditable( false ); 54 box.add( new JScrollPane( textArea2 ) ); 55 56 // add box to content pane 57 Container container = getContentPane(); 58 container.add( box ); // place in BorderLayout.CENTER 59 60 setSize( 425, 200 ); 61 setVisible( true ); 62 } 63

Creating a Customized Subclass

of JPanel

• Extend JPanel to create new components

• Dedicated drawing area

• Method paintComponent of class JComponent

Example: CustomPanel and

CustomPanelTest

1 // CustomPanel.java 2 // A customized JPanel class. 3 4 // Java core packages 5 import java.awt.; 6 7 // Java extension packages 8 import javax.swing.; 9 10 public class CustomPanel extends JPanel { 11 public final static int CIRCLE = 1, SQUARE = 2; 12 private int shape; 13 14 // use shape to draw an oval or rectangle 15 public void paintComponent( Graphics g ) 16 { 17 super.paintComponent( g ); 18 19 if ( shape == CIRCLE ) 20 g.fillOval( 50, 10, 60, 60 ); 21 else if ( shape == SQUARE )

Example: CustomPanel and

CustomPanelTest

1 // CustomPanelTest.java 2 // Using a customized Panel object. 3 4 // Java core packages 5 import java.awt.; 6 import java.awt.event.; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class CustomPanelTest extends JFrame { 12 private JPanel buttonPanel; 13 private CustomPanel myPanel; 14 private JButton circleButton, squareButton; 15 16 // set up GUI 17 public CustomPanelTest() 18 { 19 super( "CustomPanel Test" ); 20

Example: CustomPanel and

CustomPanelTest

21 // create custom drawing area 22 myPanel = new CustomPanel(); 23 myPanel.setBackground( Color.green ); 24 25 // set up squareButton 26 squareButton = new JButton( "Square" ); 27 squareButton.addActionListener( 28 29 // anonymous inner class to handle squareButton events 30 new ActionListener() { 31 32 // draw a square 33 public void actionPerformed( ActionEvent event ) 34 { 35 myPanel.draw( CustomPanel.SQUARE ); 36 } 37 38 } // end anonymous inner class 39 40 ); // end call to addActionListener 41

Example: CustomPanel and

CustomPanelTest

64 // attach button panel & custom drawing area to content pane 65 Container container = getContentPane(); 66 container.add( myPanel, BorderLayout.CENTER ); 67 container.add( buttonPanel, BorderLayout.SOUTH ); 68 69 setSize( 300, 150 ); 70 setVisible( true ); 71 } 72 73 // execute application 74 public static void main( String args[] ) 75 { 76 CustomPanelTest application = new CustomPanelTest(); 77 78 application.setDefaultCloseOperation( 79 JFrame.EXIT_ON_CLOSE ); 80 } 81 82 } // end class CustomPanelTest

Creating a Self-Contained

Subclass of JPanel

• JPanel

  • Does not support conventional events
    • e.g., events offered by buttons, text areas, etc.
  • Capable of recognizing lower-level events
    • e.g., mouse events, key events, etc.
  • Self-contained panel
    • Listens for its own mouse events

Example: SelfContainedPanel

and SelfContainedPanelTest

22 // anonymous inner class for mouse pressed and 23 // released event handling 24 new MouseAdapter() { 25 26 // handle mouse press event 27 public void mousePressed( MouseEvent event ) 28 { 29 x1 = event.getX(); 30 y1 = event.getY(); 31 } 32 33 // handle mouse release event 34 public void mouseReleased( MouseEvent event ) 35 { 36 x2 = event.getX(); 37 y2 = event.getY(); 38 repaint(); 39 } 40 41 } // end anonymous inner class 42

Example: SelfContainedPanel

and SelfContainedPanelTest

43 ); // end call to addMouseListener 44 45 // set up mouse motion listener 46 addMouseMotionListener( 47 48 // anonymous inner class to handle mouse drag events 49 new MouseMotionAdapter() { 50 51 // handle mouse drag event 52 public void mouseDragged( MouseEvent event ) 53 { 54 x2 = event.getX(); 55 y2 = event.getY(); 56 repaint(); 57 } 58 59 } // end anonymous inner class 60 61 ); // end call to addMouseMotionListener 62 63 } // end constructor