Graphical User Interface (GUI) Layout Managment 1
Hello World Often have a static method: createandshowgui() Invoked by main calling invokelater private static void createandshowgui() { } JFrame frame = new JFrame("HelloWorldSwing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new JLabel("Hello World")); frame.pack(); frame.setvisible(true); 2
Problem GUI has lots of parts Making a GUI easy to use requires logical layout But screens should be dynamic and resizable! How do you manage layout of components? 3
Swing Components A rectangular part of a GUI window Swing Simple Components JLabel to contain output text JTextArea to allow user to type text JButton - Implements a push-button JRadioButton or JCheckButton JProgressBar JFileChooser Typically we start with one of these and extend it 4
Collect multiple Components in Container Most often, JPanel (or Jframe.contentPanel()) Use a panel for a section of the GUI Add a component to a container with the add method add(component,layout_hints) Each container has a Layout Manager Determines where to put the components in the container Invoked when Container is first built Invoked when Container is re-sized May request components to resize Default Layout Manager for JPanel is FlowLayout 5
Flow Layout Manager Like writing or reading English Components appear in the order they are added Left to right until you get to the right margin Components assume their natural (preferred) sizes Default gap between components is 5 pixels How wide is the enclosing panel? Then go to a new line 6
Box Layout Single row or column of components When creating layout, specify BoxLayout.X_AXIS or BoxLayout.Y_AXIS When creating, you have to specify container as an argument (different from other layouts) Components appear in the order they are added 7
Border Layout Specify position when adding components When resizing Center expands both x and y page_start/end expands x, line start/end expands y By default, there is a 5 pixel gap between components It s surprising how much this handles 8
Grid Layout Useful when components are in a grid Specify rows/cols as argument to constructor 0 indicates as many as needed All components same size Resized to fit container 9
GridBag Layout Extends the Grid concept by allowing components to span multiple rows/cols Each row and column can have different size Flexible and adaptable Requires the most code Specify constraints when adding components Constraints define grid coordinates of upper left corner and things like padding, grid width, grid height, etc. See the GridBagContraints class for more details 10
Card Layout Use when you want multiple versions of the display Each version is on it s own card a new Jpanel Each card can have its own layout manager You specify which card is currently displayed Similar to a tabbed windows (JTabbedPane) 11
Component Resizing It is possible to set a minimum, preferred, and maximum size on each component yellowlabel.setpreferredsize(new Dimension(200, 180)); yellowlabel.setminimumsize(new Dimension(100,20)); yellowlabel.setmaximumsize(new Dimension(600,800)); It would be nice if the layout managers all respected these constraints (they don t but some, like GridBagLayout, do) 12
Java Layout Tutorial See https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 13
Adding Borders Every component can have its own borders Borders are created by the BorderFactory class see https://docs.oracle.com/javase/tutorial/uiswing/components/bo rder.html for some examples 14
Popup Dialogs There is a JDialog class for creating custom dialogs, but Simple popup messages are just JOptionPane.showMessageDialog(this,"Amount not recognized."); 15