Graphical User Interface (GUI) Layout Managment 1
Hello World Often have a run method to create and show a GUI Invoked by main calling invokelater private void run() { } 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
Collect multiple Components in Container Most often, JPanel (or Jframe.contentPane()) Use a panel for a rectangular sub-section of the GUI Add a component to a container with the add method add(component) or 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 4
Managing the Layout Manager Layout manager may be an argument to a Container Constructor e.g public JPanel (LayoutManager layout) Create a new buffered JPanel with the specified layout manager Contain class has a setlayout (and getlayout) method public void setlayout (LayoutManager mgr) Sets the layout manager for this container. Inherited by all classes which extend Container 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, the layout manager, 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 North/South expands x, West/East expands y By default, there is a 5 pixel gap between components Unused positions take no space Only one component per position Layout hint: panel.add(button,borderlayout.east); 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 Fills in left to right then top to bottom 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 We haven t covered them all SpringLayout, GroupLayout 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