Implementing GUIs in Java <ul><li>The Java Foundation Classes (JFC) are a set of packages encompassing the following APIs:...
Abstract Window Toolkit (AWT) <ul><li>Provides basic UI components: </li></ul><ul><ul><li>Buttons, lists, menus, textfield...
AWT Packages java.awt  Basic component functionality java.awt.accessibility  Assistive technologies java.awt.color  Colors...
Peers and Platform Independence <ul><li>The first AWT (Java 1.0) was rolled out in an incredible 6 weeks using peers </li>...
Using Peers Java  Program Java  AWT Native Window System Peers A Java program creates and displays an AWT component, which...
Lightweight Components <ul><li>AWT 1.1 introduced the notion of  lightweight  components which: </li></ul><ul><ul><li>are ...
Some  AWT Components Object Component Container Button List Scrollbar Label Canvas Button JComponent
AWT vs. Swing <ul><li>Swing does not  replace  the AWT; it is built on  top  of it </li></ul><ul><li>All 1.0 AWT component...
Some  Swing Components JComponent AbstractButton JButton JMenuItem JToggleButton JCheckBox JLabel JList JScrollBar JFileCh...
JComponent s <ul><li>Note that  JComponent s are containers </li></ul><ul><li>JComponent s do not extend their AWT counter...
Some  AWT  Containers Container JComponent Panel ScrollPane Window Dialog Frame Applet
Swing  Components That Are Not JComponents (in  red ) Container JComponent Panel ScrollPane Window Dialog Frame Applet JDi...
Some More Swing  Components That  Are   JComponents JComponent JLayeredPane JPanel JScrollPane JInternalFrame JDesktopPane...
Some AWT  Component  Methods <ul><li>void setBackground(Color c) </li></ul><ul><li>void setForeground(Color c) </li></ul><...
Example: A Simple Framed Window import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(...
Notes on the Example <ul><li>setSize  and  setLocation  require  java.awt.* ; the rest require  javax.swing.*   </li></ul>...
Example Output Display <ul><li>This window was managed by the K Desktop Environment (KDE) </li></ul><ul><li>Clicking the C...
Adding Color <ul><ul><li>Color.black </li></ul></ul><ul><ul><li>Color.blue </li></ul></ul><ul><ul><li>Color.cyan </li></ul...
Changing Background Color import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String...
Content Panes <ul><li>Q: Why not just:  frame.setBackground(Color.red);  ? </li></ul><ul><li>A: In order to be lightweight...
Adding a Label and Button import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String...
New Display Resized
Notes on the Code <ul><li>Since the frame is a top-level Swing window, components must be  add ed to its content pane </li...
Adding a List of Options import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[...
New Display Note that &quot;Option 3&quot; has been selected.
Adding a Check Box and Slider public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(...
New Display
Layout Management <ul><li>A layout manager determines the location and size of components placed into a container </li></u...
Changing the Layout public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(&quot;Test...
New Display Resized
Default Layout Managers <ul><li>The default layout manager for content panes is  BorderLayout .  Recall that the following...
JPanel s <ul><li>A  JPanel  object can be used for grouping components into a container, which can then be added to anothe...
JPanel  Example JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocatio...
JPanel  Example Output Note use of  SwingConstants.CENTER  argument in  JLabel  constructor.
Changing  JPanel  Layout JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.s...
New Output <ul><li>The button panel is to the west because no other component was placed there </li></ul><ul><li>The  BoxL...
Tweaking Layouts <ul><li>Some layout constructors allow  hgap  and  vgap , integers specifying the number of pixels separa...
Tweaking Example JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocati...
Tweaking Example Output <ul><li>The  LayoutManager  returned by  getLayout()  is an interface type that the  BorderLayout ...
Sizing Hints <ul><li>Layout managers often need to resize their components to make things fit </li></ul><ul><ul><li>For ex...
Upcoming SlideShare
Loading in …5
×

Java swing

3,073
-1

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,073
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
204
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Java swing

  1. 1. Implementing GUIs in Java <ul><li>The Java Foundation Classes (JFC) are a set of packages encompassing the following APIs: </li></ul><ul><ul><li>Abstract Window Toolkit (AWT) : native GUI components </li></ul></ul><ul><ul><li>Swing : lightweight GUI components </li></ul></ul><ul><ul><li>2D : rendering two-dimensional shapes, text, and images </li></ul></ul><ul><ul><li>Accessibility : allowing compatibility with, for example, screen readers and screen magnifiers </li></ul></ul>
  2. 2. Abstract Window Toolkit (AWT) <ul><li>Provides basic UI components: </li></ul><ul><ul><li>Buttons, lists, menus, textfields, etc </li></ul></ul><ul><ul><li>Event handling mechanism </li></ul></ul><ul><ul><li>Clipboard and data transfer </li></ul></ul><ul><ul><li>Image manipulation </li></ul></ul><ul><ul><li>Font manipulation </li></ul></ul><ul><ul><li>Graphics </li></ul></ul><ul><li>Platform independence is achieved through peers , or native GUI components </li></ul>
  3. 3. AWT Packages java.awt Basic component functionality java.awt.accessibility Assistive technologies java.awt.color Colors and color spaces java.awt.datatransfer Clipboard and data transfer support java.awt.dnd Drag and drop java.awt.event Event classes and listeners java.awt.font 2D API font package java.awt.geom 2D API geometry package java.awt.im Input methods java.awt.image Fundamental image manipulation classes java.awt.peer Peer interfaces for component peers java.awt.print 2D API support for printing java.awt.swing Swing components
  4. 4. Peers and Platform Independence <ul><li>The first AWT (Java 1.0) was rolled out in an incredible 6 weeks using peers </li></ul><ul><li>Thus an AWT menu on the Solaris platform, for example, actually creates a Motif menu object as its peer </li></ul><ul><li>UI components that have peers are called heavyweight because </li></ul><ul><ul><li>they are rendered in their own (opaque) windows and thus are expensive to use, </li></ul></ul><ul><ul><li>they must be rectangular and cannot have transparent backgrounds, and </li></ul></ul><ul><ul><li>they are not amenable to being subclassed </li></ul></ul>
  5. 5. Using Peers Java Program Java AWT Native Window System Peers A Java program creates and displays an AWT component, which creates and displays a native component, or peer.
  6. 6. Lightweight Components <ul><li>AWT 1.1 introduced the notion of lightweight components which: </li></ul><ul><ul><li>are contained within a heavyweight component's window </li></ul></ul><ul><ul><li>do not have peers </li></ul></ul><ul><ul><li>are rendered in their container's window rather than one of their own </li></ul></ul><ul><ul><li>do not incur performance penalties and can have transparent backgrounds </li></ul></ul><ul><li>Almost all Swing components are lightweight ones that extend either java.awt.Component or java.awt.Container </li></ul>
  7. 7. Some AWT Components Object Component Container Button List Scrollbar Label Canvas Button JComponent
  8. 8. AWT vs. Swing <ul><li>Swing does not replace the AWT; it is built on top of it </li></ul><ul><li>All 1.0 AWT components are heavyweight ; corresponding Swing components are lightweight </li></ul><ul><li>Swing component names begin with ``J'': </li></ul><ul><ul><li>Component (AWT) vs. JComponent (Swing) </li></ul></ul><ul><ul><li>Button (AWT) vs. JButton (Swing) </li></ul></ul><ul><li>Always use Swing components; however, since Swing is built on top of AWT, you will need to know some AWT methods </li></ul>
  9. 9. Some Swing Components JComponent AbstractButton JButton JMenuItem JToggleButton JCheckBox JLabel JList JScrollBar JFileChooser
  10. 10. JComponent s <ul><li>Note that JComponent s are containers </li></ul><ul><li>JComponent s do not extend their AWT counterparts: </li></ul><ul><ul><li>For example, the JButton class is not a subclass (direct or indirect) of Button </li></ul></ul><ul><li>However, some Swing components are not JComponent s </li></ul><ul><ul><li>For example, some Swing containers are direct subclasses of their AWT counterparts </li></ul></ul>
  11. 11. Some AWT Containers Container JComponent Panel ScrollPane Window Dialog Frame Applet
  12. 12. Swing Components That Are Not JComponents (in red ) Container JComponent Panel ScrollPane Window Dialog Frame Applet JDialog JWindow JFrame JApplet
  13. 13. Some More Swing Components That Are JComponents JComponent JLayeredPane JPanel JScrollPane JInternalFrame JDesktopPane JTable JTree
  14. 14. Some AWT Component Methods <ul><li>void setBackground(Color c) </li></ul><ul><li>void setForeground(Color c) </li></ul><ul><li>void setEnabled(boolean b) </li></ul><ul><li>void setVisible(boolean b) </li></ul><ul><li>void setFont(Font f) </li></ul><ul><li>void setSize(Dimension d) </li></ul><ul><li>void setLocation(int x, int y) </li></ul>All but setSize and setLocation are overridden by the JComponent class.
  15. 15. Example: A Simple Framed Window import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); frame.setVisible(true); } }
  16. 16. Notes on the Example <ul><li>setSize and setLocation require java.awt.* ; the rest require javax.swing.* </li></ul><ul><li>The JFrame constructor argument is used as a title </li></ul><ul><li>The Dimension constructor takes an integer width and height, respectively </li></ul><ul><li>The setLocation method takes a pair of integer coordinates (x,y) where (0,0) is the upper left corner of the display </li></ul><ul><li>The visibility of a JFrame is set to false by default </li></ul>
  17. 17. Example Output Display <ul><li>This window was managed by the K Desktop Environment (KDE) </li></ul><ul><li>Clicking the Close button (X) will cause the display to be hidden, but the program will continue since no listeners are set up yet </li></ul><ul><li>Can use ctl-C to kill the Java Virtual Machine </li></ul>
  18. 18. Adding Color <ul><ul><li>Color.black </li></ul></ul><ul><ul><li>Color.blue </li></ul></ul><ul><ul><li>Color.cyan </li></ul></ul><ul><ul><li>Color.darkGray </li></ul></ul><ul><ul><li>Color.gray </li></ul></ul><ul><ul><li>Color.green </li></ul></ul><ul><ul><li>Color.lightGray </li></ul></ul><ul><ul><li>Color.magenta </li></ul></ul><ul><ul><li>Color.orange </li></ul></ul><ul><ul><li>Color.pink </li></ul></ul><ul><ul><li>Color.red </li></ul></ul><ul><ul><li>Color.white </li></ul></ul><ul><ul><li>Color.yellow </li></ul></ul>The java.awt.Color class has the following static fields (data members):
  19. 19. Changing Background Color import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); contentPane.setBackground(Color.red); frame.setVisible(true); } }
  20. 20. Content Panes <ul><li>Q: Why not just: frame.setBackground(Color.red); ? </li></ul><ul><li>A: In order to be lightweight, Swing's top-level window objects must be built on top of a lightweight AWT Container object introduced in version 1.1 </li></ul><ul><li>This container is called a content pane </li></ul><ul><li>Swing top-level window classes: </li></ul><ul><ul><li>JWindow </li></ul></ul><ul><ul><li>JFrame </li></ul></ul><ul><ul><li>JApplet </li></ul></ul><ul><ul><li>JDialog </li></ul></ul><ul><ul><li>JInternalFrame </li></ul></ul>
  21. 21. Adding a Label and Button import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel(&quot;HERE IS A LABEL&quot;); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton(&quot;BUTTON&quot;); contentPane.add(button, BorderLayout.SOUTH); frame.setVisible(true); } }
  22. 22. New Display Resized
  23. 23. Notes on the Code <ul><li>Since the frame is a top-level Swing window, components must be add ed to its content pane </li></ul><ul><li>When components are added to a container, how they are placed is dependent upon the container's layout manager </li></ul><ul><li>The default layout manager for a JFrame is a BorderLayout manager (described later) </li></ul><ul><li>When adding to a container whose layout manager is BorderLayout , the second parameter should be a location defined in the BorderLayout class </li></ul>
  24. 24. Adding a List of Options import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel(&quot;HERE IS A LABEL&quot;); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton(&quot;BUTTON&quot;); contentPane.add(button, BorderLayout.SOUTH); String[] options = {&quot;Option 1&quot;, &quot;Option 2&quot;, &quot;Option 3&quot;}; JList list = new JList(options); contentPane.add(list, BorderLayout.CENTER); frame.setVisible(true); } }
  25. 25. New Display Note that &quot;Option 3&quot; has been selected.
  26. 26. Adding a Check Box and Slider public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(400,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel(&quot;HERE IS A LABEL&quot;); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton(&quot;BUTTON&quot;); contentPane.add(button, BorderLayout.SOUTH); String[] options = {&quot;Option 1&quot;, &quot;Option 2&quot;, &quot;Option 3&quot;}; JList list = new JList(options); contentPane.add(list, BorderLayout.CENTER); JCheckBox cbox = new JCheckBox(&quot;Check&quot;); contentPane.add(cbox, BorderLayout.WEST); JSlider slider = new JSlider(); contentPane.add(slider, BorderLayout.EAST); frame.setVisible(true); } }
  27. 27. New Display
  28. 28. Layout Management <ul><li>A layout manager determines the location and size of components placed into a container </li></ul><ul><li>Different layout managers use different algorithms for determining size and location: </li></ul><ul><ul><li>BorderLayout : places at compass locations and center </li></ul></ul><ul><ul><li>FlowLayout: places components in rows, left to right </li></ul></ul><ul><ul><li>GridLayout : places in rectangular grid </li></ul></ul><ul><ul><li>BoxLayout : places in a single row or column </li></ul></ul>
  29. 29. Changing the Layout public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); JLabel label = new JLabel(&quot;HERE IS A LABEL&quot;); JButton button = new JButton(&quot;BUTTON&quot;); String[] options = {&quot;Option 1&quot;, &quot;Option 2&quot;, &quot;Option 3&quot;}; JList list = new JList(options); JCheckBox cbox = new JCheckBox(&quot;Check&quot;); JSlider slider = new JSlider(); contentPane.add(label); contentPane.add(button); contentPane.add(list); contentPane.add(cbox); contentPane.add(slider); frame.setVisible(true); } }
  30. 30. New Display Resized
  31. 31. Default Layout Managers <ul><li>The default layout manager for content panes is BorderLayout . Recall that the following Swing components have content panes: </li></ul><ul><ul><li>JWindow </li></ul></ul><ul><ul><li>JFrame </li></ul></ul><ul><ul><li>JDialog </li></ul></ul><ul><ul><li>JApplet </li></ul></ul><ul><ul><li>JInternalFrame </li></ul></ul><ul><li>The other Swing container is the JPanel , whose default layout manager is FlowLayout . </li></ul>
  32. 32. JPanel s <ul><li>A JPanel object can be used for grouping components into a container, which can then be added to another container </li></ul><ul><li>The JPanel constructor with no arguments creates a panel with a FlowLayout manager </li></ul><ul><li>Another JPanel constructor takes any layout manager as an argument </li></ul><ul><li>A JPanel can also be used a a blank area for drawing custom graphics </li></ul>
  33. 33. JPanel Example JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel(&quot;HERE ARE SOME BUTTONS&quot;, SwingConstants.CENTER); JButton button1 = new JButton(&quot;BUTTON1&quot;); JButton button2 = new JButton(&quot;BUTTON2&quot;); JButton button3 = new JButton(&quot;BUTTON3&quot;); JPanel panel = new JPanel(); panel.add(button1); panel.add(button2); panel.add(button3); contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); frame.setVisible(true);
  34. 34. JPanel Example Output Note use of SwingConstants.CENTER argument in JLabel constructor.
  35. 35. Changing JPanel Layout JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel(&quot;HERE ARE SOME BUTTONS&quot;, SwingConstants.CENTER); JButton button1 = new JButton(&quot;BUTTON1&quot;); JButton button2 = new JButton(&quot;BUTTON2&quot;); JButton button3 = new JButton(&quot;BUTTON3&quot;); JPanel panel = new JPanel(); panel.setLayout (new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(button1); panel.add(button2); panel.add(button3); contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); frame.setVisible(true);
  36. 36. New Output <ul><li>The button panel is to the west because no other component was placed there </li></ul><ul><li>The BoxLayout constructor requires both the component being laid out and either: </li></ul><ul><ul><li>BoxLayout.X_AXIS </li></ul></ul><ul><ul><li>BoxLayout.Y_AXIS </li></ul></ul>
  37. 37. Tweaking Layouts <ul><li>Some layout constructors allow hgap and vgap , integers specifying the number of pixels separating components horizontally and vertically </li></ul><ul><li>FlowLayout allows the specification of whether the line of components should be left-justified, right-justified, or centered </li></ul>new FlowLayout(int align) new FlowLayout(int align, int hgap, int vgap) new BorderLayout(int hgap, int vgap) new GridLayout(int rows, int cols) new GridLayout(int rows, int cols, int hgap, int vgap)
  38. 38. Tweaking Example JFrame frame = new JFrame(&quot;Test Frame&quot;); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); LayoutManager lm = contentPane.getLayout(); ((BorderLayout)lm).setHgap(25); JLabel label = new JLabel(&quot;HERE ARE SOME BUTTONS&quot;, SwingConstants.CENTER); JButton button1 = new JButton(&quot;BUTTON1&quot;); JButton button2 = new JButton(&quot;BUTTON2&quot;); JButton button3 = new JButton(&quot;BUTTON3&quot;); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(button1); panel.add(button2); panel.add(button3); contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); frame.setVisible(true);
  39. 39. Tweaking Example Output <ul><li>The LayoutManager returned by getLayout() is an interface type that the BorderLayout class implements </li></ul><ul><li>The setHgap method we want is in the BorderLayout class </li></ul><ul><li>So we must cast the LayoutManager to BorderLayout in order to use setHgap </li></ul>
  40. 40. Sizing Hints <ul><li>Layout managers often need to resize their components to make things fit </li></ul><ul><ul><li>For example, the widths and heights of components in a BoxLayout are adjusted according to both preferred and maximum heights and widths </li></ul></ul><ul><li>If you don't like the size of the components a layout manager comes up with, you may have to give sizing hints using the following methods from the JComponent class: </li></ul><ul><ul><li>void setMinimumSize(Dimension d) </li></ul></ul><ul><ul><li>void setPreferredSize(Dimension d) </li></ul></ul><ul><ul><li>void setMaximumSize(Dimension d) </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×