1 / 29

Java Programming

Java Programming. Introduction to Swing Components Chapter 14. What is Java Swing?. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program with a graphical user interface (GUI)

kyros
Download Presentation

Java Programming

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. Java Programming Introduction to Swing Components Chapter 14

  2. What is Java Swing? • Part of the Java Foundation Classes (JFC) • Provides a rich set of GUI components • Used to create a Java program with a graphical user interface (GUI) • table controls, list controls, tree controls, buttons, and labels, and so on…

  3. Java Foundation Classes • In April 1997, JavaSoft announced the Java Foundation Classes (JFC). • a major part of the JFC is a new set of user interface components called Swing. AWT Swing Accessibility Java 2D Drag And Drop

  4. AWT java.awt java.awt.color java.awt.datatransfer java.awt.event java.awt.font java.awt.geom java.awt.image Swing javax.accessibility javax.swing javax.swing.colorchooser javax.swing.event javax.swing.filechooser javax.swing.plaf javax.swing.table javax.swing.text.html javax.swing.tree GUI Packages

  5. Components • A GUI consists of different graphic Component objects which are combined into a hierarchy using Container objects. • Component class • An abstract class for GUI components such as menus, buttons, labels, lists, etc. • Container • An abstract class that extends Component. Containers can hold multiple components.

  6. Components • Container • Type of component that holds other components • Can treat group as single entity • Defined in Container class • Often takes form of window • Drag • Resize • Minimize • Restore • Close

  7. Swing Features • Swing provides: • A wide variety of components (tables, trees, sliders, progress bars, internal frame, …) • Swing components can have tooltips placed over them • Arbitrary keyboard events can be bound to components • Additional debugging support. • Support for parsing and displaying HTML based information

  8. JFrames • A JFrame is a Window with all of the adornments added • JFrame inherits from Frame, Window, Container, Component, and Object • A JFrame provides the basic building block for screen-oriented applications JFramewindow = new JFrame(" title ");

  9. Creating a JFrame import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFramewindow = new JFrame( "My First GUI Program" ); window.setVisible(true); } }

  10. JFrames • Set size and title window.setSize(200, 100); window.setTitle("My frame"); • Close JFrame • Click Close button • JFrame becomes hidden and application keeps running • Default behavior • To change this behavior • Use setDefaultCloseOperation()method window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

  11. Creating a JFrame • import javax.swing.*; • import java.awt.*; • public class SwingFrame { • public static void main( String args[] ) { • JFramewindow = new JFrame( "My First GUI Program" ); • window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); • window.setSize( 250, 150); • window.setVisible(true); • } • }

  12. JFrame • JFrames have several panes: • Components are placed in the Content Pane Layered pane Glass pane Menu bar Content pane

  13. Swing Components • JComponent • JComboBox, JLabel, JList, JMenuBar, JPanel, JPopupMenu, JScrollBar, JScrollPane, JTable, JTree, JInternalFrame, JOptionPane, JProgressBar, JRootPane, JSeparator, JSlider, JSplitPane, JTabbedPane, JToolBar, JToolTip, Jviewport, JColorChooser, JTextComponent, …

  14. JLabels • JLabels are components that you can fill with text. • When creating a label you can specify the initial value and the alignment you wish to use within the label. • You can use getText() and setText() to get and change the value of the label. • You can use setFont() to change the font, style and size of your text label = new JLabel("text", JLabel.RIGHT); Font typeSet = new Font("Arial", Font.BOLD, 36 );

  15. JLabels import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFramewindow = new JFrame( "My First GUI Program" ); JLabel label = new JLabel( "Hello World" ); Font typeSet = new Font("Century", Font.BOLD, 28 ); label.setFont(typeSet); window.add( label ); window.setSize(250, 100); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } }

  16. JButtons • JButton extends Component , displays a string, and delivers an ActionEvent for each mouse click. • Normally buttons are displayed with a border • In addition to text, JButtons can also display icons button = new JButton( ”text“ );

  17. Buttons import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFramewindow = new JFrame( "My First GUI Program" ); JButton button = new JButton( "Click Me!!" ); window.add( button ); window.setSize(250, 100); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } }

  18. Layout Manager • Layout Manager • An interface that defines methods for positioning and sizing objects within a container. Java defines several default implementations of LayoutManager. • Geometrical placement in a Container is controlled by a LayoutManager object

  19. Components, Containers, and Layout Managers • Containers may contain components (which means containers can contain containers!!). • All containers come equipped with a layout manager which positions and shapes (lays out) the container's components. • Much of the action in Swing occurs between components, containers, and their layout managers.

  20. Layout Managers • Layouts allow you to format components on the screen in a platform-independent way • The standard JDK provides many classes that implement the LayoutManager interface, including: • FlowLayout • GridLayout • BorderLayout • BoxLayout • CardLayout • OverlayLayout • GridBagLayout

  21. Changing the Layout • Invoke the setLayout() method on the container to use the new layout. setLayout( new FlowLayout() ); • The layout manager should be established before any components are added to the container • JFrame will otherwise stack all components on top of each other

  22. FlowLayout • When you add components to the screen, they flow left to right (centered) based on the order added and the width of the screen. • Very similar to word wrap and full justification on a word processor. • If the screen is resized, the components' flow will change based on the new width and height • FlowLayout is the default layout for the JPanel class.

  23. import javax.swing.*; import java.awt.*; public class LayOut { public static void main( String args[] ) { JFrame window = new JFrame( "Layout Program" );JLabel title = new JLabel( "Layout Example" );JLabel name = new JLabel( "Enter your name" );JTextFieldtextInput = new JTextField( 12 );JButton button = new JButton( "Enter your name" );window.setSize(200, 150); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);window.setVisible(true);window.setLayout( new FlowLayout() );window.add( title );window.add( name );window.add( textInput );window.add( button ); }}

  24. Flow Layout import javax.swing.*; import java.awt.*; public class ShowFlowLayout { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.getContentPane().setLayout( new FlowLayout() ); for ( inti = 0; i < 10; i++ ) { win.getContentPane().add( new JButton( String.valueOf( i ) ) ); } win.setVisible(true); } }

  25. FlowLayout You can resize with the mouse and the buttons will move to fill in the space

  26. Event Driven Programming • Programs respond to events that are generated outside the control of the program • User types a key • The left mouse button is pressed • A CD is removed from the CD drive • When an event occurs, it is handled by an event handler • Event driven programming involves • writing the handlers, and • arranging for the handler to be notified when certain events occur

  27. Event Handling • An event is represented by an object that gives information about the event and identifies the event source • Event sources are typically components, but other kinds of objects can also be event sources • A listener is an object that wants to be notified when a particular event occurs • An event source can have multiple listeners registered on it • A single listener can register with multiple event sources

  28. Listeners • In order for an object to be notified when a particular event occurs, the object • must implement the appropriate Listener interface • must be registered as an event listener on the appropriate event source

  29. Swing Listeners

More Related