New Summer Look :)
![]() |
(1) Mapping of classes to packages (2) Design for Containment (3) Design for Layout Management (4) Design for the Peers |
java.awt.applet java.awt java.awt.color java.awt.datatransfer java.awt.dnd java.awt.event java.awt.font java.awt.geom java.awt.im java.awt.image java.awt.print |
Applet & relevant Interfaces Components, Layouts, Graphics ColorSpaces Clipboard, DataFlavor, Transferable DragSource, DropTarget, Events, Listeners Events, Listeners, Adapters Font graphics rendering classes Shapes, AffineTransform, GeneralPath InputContext, InputMethodRequests Image Processing classes PrinterJob, Printable, PageFormat |
![]() |
- Arbitrary deep containment hierarchy - Container HAS Components - Thus Container can mix containing Containers & Component - This design is called the "composite design pattern" |
Naive Design Solution 1
![]() |
- Positioning components by specifying absolute coordinates
is problematic - We need to give to Containers algorithms for layout their components relatively |
Naive Design Solution 2
|
![]() |
Java's Solution (Strategy Design Pattern) - LayoutManager knows Container? - When we need absolute Coordinates we can set the layout to null |
![]() |
![]() |
- How do we get a multiplatform gui api ? - Using native implementations for each platform (AWT) - Write all the gui functionality in java (SWING) - Peer Interfaces decouple AWT components from a specific platform |
- Native Implementations stay completely hidden |
![]() |
- The instantiation of a Peer initiates when a Component is added
to a Container - Object creation is hidden from the Clients and delegated to the Abstract Factory Class named Toolkit |
Are the Toolkits Hardcoded? |
![]() |
Java Foundation classes consists of: Swing Components are built on top of AWT |
javax.swing : javax.swing.border : javax.swing.colorchooser : javax.swing.event : javax.swing.filechooser : javax.swing.plaf : javax.swing.table : javax.swing.text : javax.swing.text.html : javax.swing.text.rtf : javax.swing.tree : javax.swing.undo : |
Components, Managers, Models 8 Types of Borders Customisation classes Listeners, Events, Adapters Customisation classes UI Abstractions for Plug&Play GUI Customisation classes Text Editor Classes HTML Editor classes RTF Editor kit Customisation classes Classes for undo functionality |
![]() |
- Provide pluggable look-and-feel ability |
![]() |
- The Parent of all Swing Components - Overides and extends Containersd - Gives the ability to define for every Swing Component: Borders, ToolTipText, and UI look&feel |
-JButton, JLabel -JPanel, Box -JMenu, JMenuItem, JSeparator,JCheckMenuItem, JRadioButtonMenuItem, JMenuBar, JPopupMenu -JToggleButton, JRadioButton, JCheckBox -JFileChooser -JComboBox, Jlist -JInternalFrame, JDesktopPane, JDesktopIcon -JOptionPane -JProgressBar, JSlider, JScrollBar -JScrollPane, JViewPort -JSpiltPane, JTabbedPane -JTable, JTree -JTextField, JTextArea, JPasswordField, JEditorPane -JToolBar, JToolTip -JApplet -JWindow, JDialog, Jframe |
import javax.swing.*; import java.awt.*; import javax.swing.border.*; import java.awt.event.*; public class ExampleUsingJFC extends JFrame { public static void main(String[] argv) { ExampleUsingJFC myExample = new ExampleUsingJFC("ExampleUsingJFC"); } public ExampleUsingJFC(String title) { super(title); setSize(150,150); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { dispose(); System.exit(0); } }); init(); setVisible(true); } private void init() { JPanel my_panel = new JPanel(); my_panel.setLayout(new GridLayout(3,3)); for (int i=1;i<10;i++) { ImageIcon icon = new ImageIcon(i+".gif"); JButton jb = new JButton(icon); jb.setToolTipText(i+".gif"); my_panel.add(jb); } getContentPane().add(my_panel); my_panel.setBorder(BorderFactory.createEtchedBorder()); } } -Borders Available: Bevel, Compound, Empty, Etched, Line, Matte, Soft, Titled |
![]() |
BoxLayout - Horizontally or Vertically OverlayLayout - Arranging components over the top of each other. ScrollPanelLayout - JScrollPane's layout ViewportLayout - JViewport's layout SizeRequirements - Helper Class for Layout Managers |
![]() |
MVC architectural pattern Dates back to late 70's Well known OO design solution The parts can be exchanged at runtime. Can change L&F very easily More than one view for a model always in synchronisation Notification through events (+) Clean separation of concerns better maintainability and reusability (-) Many more classes involved In java View and Controller are grouped to a UIdelegate |
BoundedRangeModel : ButtonModel : ListModel : ComboBoxModel : MutableComboBoxModel : ListSelectionModel : SingleSelectionModel : ColorSelectionModel : TableModel : TableColumnModel : TreeModel : TreeSelectionModel : Document : |
4int -> value, extent,
min, max boolean armed A collection of objects A collection of objects and a selected object A Vector of objects and a selected object Indices of selected list or table items The index of the selection in a collection A Color A two dimensional array of objects Many attributes Objects that can be displayed in a tree Selected rows Content in text or styled text and images |
Metal L&F
![]() |
Motif L&F
![]() |
Windows L&F |
public void changeLookTo(String cName) { try { UIManager.setLookAndFeel(cName); } catch (Exception e) { System.out.println("Could not change l&f"); } SwingUtilities.updateComponentTreeUI(this); this.pack(); } |
||
ChooseFrom: - "javax.swing.plaf.metal.MetalLookAndFeel"; - "javax.swing.plaf.motif.MotifLookAndFeel"; - "javax.swing.plaf.windows.WindowsLookAndFeel"; |
Managers accept customisations Managers include : - DesktopManager, DefaultDesktopManager - FocusManager, DefaultFocusManager - MenuSelectionManager - RepaintManager - ToolTipManager - UIManager |
![]() |
Classes: - BorderFactory - ImageIcon, Icon - LookAndFeel - ProgressMonitor, ProgressMonitorInputStream - SwingUtilities - GrayFilter - Timer |
private void init() { JTabbedPane jtb = new JTabbedPane(); for (int i=1;i<10;i++) { ImageIcon icon = new ImageIcon(i+".gif"); ImageIcon icon2 = new ImageIcon(i+"a.jpg"); JScrollPane jsp = new JScrollPane(new JLabel(icon2)); jtb.addTab(i+"-tab",icon,jsp); } getContentPane().add(jtb); jtb.setBorder(BorderFactory.createEtchedBorder()); } |
String data[][] = { {"John","Sutherland","Student"}, {"George","Davies","Student"}, {"Melissa","Anderson","Associate"}, {"Stergios","Maglaras","Developer"}, }; String fields[] = {"Name","Surname","Status"}; private void init() { JTable jt = new JTable(data,fields); JScrollPane pane = new JScrollPane(jt); getContentPane().add(pane); } |
![]() |
Many classes involved - JTree - TreeModel, DefaultTreeModel - TreeNode, MutableTreeNode, DefaultMutableTreeNode - TreeSelectionModel, TreeSelectionListener, TreeSelEvent - TreePath - TreeCellRenderer - TreeNodeListener, TreeNodeEvent |
|
JTree (L&F:metal)
![]() |
JTree (L&F:motif)
![]() |
JTree (L&F:windows)
![]() |
|
private void init() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar"); DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months"); root.add(months); String monthLabels[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; for (int i=0; i<monthLabels.length; i++) months.add( new DefaultMutableTreeNode(monthLabels[i])); DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks"); root.add(weeks); String weekLabels[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; for (int i=0; i<weekLabels.length; i++) weeks.add( new DefaultMutableTreeNode(weekLabels[i])); JScrollPane js = new JScrollPane(new JTree(root)); getContentPane().add(js); } |
![]() |
- JInternalFrames are placed on a JLayeredPane - JLayeredPane provides Z ordering, but only for lightweight components (order 0 to N-1) - JInternalFrames can be: closed, maximized, iconified, resized |
private void init() { JLayeredPane layers = new JDesktopPane(); setLayeredPane(layers); for (int i=0; i<9; i++) { ImageIcon icon = new ImageIcon((i+1)+"a.jpg"); JScrollPane jsp = new JScrollPane(new JLabel(icon)); jsp.setPreferredSize(new Dimension(120,140)); //JInternalFrame(title, resizable, closable,maximizable, iconifiable) JInternalFrame jif = new JInternalFrame(i+" frame",true,true,true,true); jif.setLocation((i%4)*140,(i/4)*180); jif.getContentPane().add(jsp); jif.pack(); layers.add(jif); } } |
|