Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Swing Layout Management. 2 Laying out components Manage realized components Manage realized components Determine size and position Determine size.

Similar presentations


Presentation on theme: "1 Java Swing Layout Management. 2 Laying out components Manage realized components Manage realized components Determine size and position Determine size."— Presentation transcript:

1 1 Java Swing Layout Management

2 2 Laying out components Manage realized components Manage realized components Determine size and position Determine size and position Each container has a layout manager Each container has a layout manager (usually)(usually)

3 3 Layout managers – general aspects Creating a layout manager Creating a layout manager Consulting managers Consulting managers Types of managers Types of managers Choosing managers Choosing managers Other features of component layout Other features of component layout All Covered very well here: All Covered very well here: http://java.sun.com/docs/books/tutorial/uiswing/layout/ using.htmlhttp://java.sun.com/docs/books/tutorial/uiswing/layout/ using.html

4 4 Creating a layout manager Default layout managers Default layout managers JFrame, JDialog, JApplet have BorderLayoutJFrame, JDialog, JApplet have BorderLayout JPanel has FlowLayoutJPanel has FlowLayout Except when used as a Content Pane (Border Layout) Except when used as a Content Pane (Border Layout) Setting the layout manager for a container Setting the layout manager for a container JFrame frame = new JFrame();JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); JPanel contentPane = new JPanel();JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout());

5 5 Consulting layout managers (1) Consulted automatically when container may need to change its appearance. Consulted automatically when container may need to change its appearance. These methods result in consultation, but DON’T trigger new layout These methods result in consultation, but DON’T trigger new layout add(), remove(), removeAll()add(), remove(), removeAll() getAlignmentX(), getAlignmentY()getAlignmentX(), getAlignmentY() getPreferredSize(), getMinimumSize(), getMaximumSize()getPreferredSize(), getMinimumSize(), getMaximumSize()

6 6 Consulting layout managers (2) These methods actually result in the manager performing layout. These methods actually result in the manager performing layout. JFrame.pack();JFrame.pack(); Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. JFrame.show() & JFrame.setVisible(true);JFrame.show() & JFrame.setVisible(true); Shows the component Shows the component JComponent.revalidate();JComponent.revalidate(); This method will automatically be called on this component when a property value changes. Looks for all dependent components and calls validate() on them. Validate() causes a container to lay out its subcomponents again This method will automatically be called on this component when a property value changes. Looks for all dependent components and calls validate() on them. Validate() causes a container to lay out its subcomponents again

7 7 Layout managers - types BorderLayout BorderLayout BoxLayout BoxLayout FlowLayout FlowLayout GridLayout GridLayout GridBagLayout GridBagLayout CardLayout CardLayout

8 8BorderLayout Five areas Five areas NORTH, SOUTH, EAST, WEST and CENTERNORTH, SOUTH, EAST, WEST and CENTER Not all areas must be usedNot all areas must be used Do not assume a default area for componentsDo not assume a default area for components Centre gets as much area as possibleCentre gets as much area as possible Specify location as argument of add method Specify location as argument of add method pane.setLayout(new BorderLayout());pane.setLayout(new BorderLayout()); pane.add(new JButton(“Button 1 (NORTH)”), BorderLayout.NORTH);pane.add(new JButton(“Button 1 (NORTH)”), BorderLayout.NORTH); Setting gaps between components (default = 0) Setting gaps between components (default = 0) BorderLayout.setHgap(int gap);BorderLayout.setHgap(int gap); BorderLayout.setVgap(int gap);BorderLayout.setVgap(int gap); BorderLayout(int horizontalGap, int verticalGap) - ConstructorBorderLayout(int horizontalGap, int verticalGap) - Constructor

9 9BorderLayout import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BorderWindow extends JFrame { public BorderWindow() { public BorderWindow() { //--- use default content pane Container contentPane = getContentPane(); Container contentPane = getContentPane(); //--- Use the content pane's default BorderLayout. //--- contentPane.setLayout(new BorderLayout()); //unnecessary //--- contentPane.setLayout(new BorderLayout()); //unnecessary //--- under 1.5 can just commented out line as content pane //--- is implcitly obtained //--- add(new JButton("Button 1 (NORTH)"), //--- BorderLayout.NORTH); contentPane.add(new JButton("Button 1 (NORTH)"), contentPane.add(new JButton("Button 1 (NORTH)"), BorderLayout.NORTH); BorderLayout.NORTH); contentPane.add(new JButton("2 (CENTER)"), contentPane.add(new JButton("2 (CENTER)"), BorderLayout.CENTER); BorderLayout.CENTER); contentPane.add(new JButton("Button 3 (WEST)"), contentPane.add(new JButton("Button 3 (WEST)"), BorderLayout.WEST); BorderLayout.WEST); contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), BorderLayout.SOUTH); BorderLayout.SOUTH); contentPane.add(new JButton("Button 5 (EAST)"), contentPane.add(new JButton("Button 5 (EAST)"), BorderLayout.EAST); BorderLayout.EAST); setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String args[]) { public static void main(String args[]) { BorderWindow window = new BorderWindow(); BorderWindow window = new BorderWindow(); window.setTitle("BorderLayout"); window.setTitle("BorderLayout"); window.pack(); window.pack(); window.setVisible(true); window.setVisible(true); }}

10 10BorderLayout contentPane.add(new JButton("Button 1 (NORTH)"), BorderLayout.NORTH); BorderLayout.NORTH); contentPane.add(new JButton("2 (CENTER)"), contentPane.add(new JButton("2 (CENTER)"), BorderLayout.CENTER); BorderLayout.CENTER); contentPane.add(new JButton("Button 3 (WEST)"), contentPane.add(new JButton("Button 3 (WEST)"), BorderLayout.WEST); BorderLayout.WEST); contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), BorderLayout.SOUTH); BorderLayout.SOUTH); contentPane.add(new JButton("Button 5 (EAST)"), contentPane.add(new JButton("Button 5 (EAST)"), BorderLayout.EAST); BorderLayout.EAST); setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE); }

11 11BorderLayout public static void main(String args[]) { public static void main(String args[]) { BorderWindow window = new BorderWindow(); BorderWindow window = new BorderWindow(); window.setTitle("BorderLayout"); window.setTitle("BorderLayout"); window.pack(); window.pack(); window.setVisible(true); window.setVisible(true); }}

12 12 BoxLayout (1) Components on top / next to each other Components on top / next to each other Direction is your choiceDirection is your choice Tries to size components at preferred height for Y_AXIS or width for X_AXIS Tries to size components at preferred height for Y_AXIS or width for X_AXIS Width as largest component width Width as largest component width See above pictureSee above picture

13 13 BoxLayout (2) Space fillers Space fillers Rigid - fixed-size space between two componentsRigid - fixed-size space between two components Glue - taking up no space unless you pull apart the components that it's sticking to. Helps reposition extra space (default is at end)Glue - taking up no space unless you pull apart the components that it's sticking to. Helps reposition extra space (default is at end) Custom - Use this to specify a component with whatever minimum, preferred, and maximum sizes you wantCustom - Use this to specify a component with whatever minimum, preferred, and maximum sizes you want

14 14 BoxLayout (3) Component sizes Component sizes Respect Max, Min and Preferred Sizes of componentsRespect Max, Min and Preferred Sizes of components Alignment Alignment Comes into play when not all components are the same widthComes into play when not all components are the same width Can specify Left (0), Centre (0.5) or Right (1). Or Top Middle BottomCan specify Left (0), Centre (0.5) or Right (1). Or Top Middle Bottom If you are having layout problems, first treat as an Alignment issue, then examine sizes. If you are having layout problems, first treat as an Alignment issue, then examine sizes.

15 15BoxLayout import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BoxWindow extends JFrame { public BoxWindow() { public BoxWindow() { Container contentPane = getContentPane(); Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); BoxLayout.Y_AXIS)); addAButton("Button 1", contentPane); addAButton("Button 1", contentPane); addAButton("2", contentPane); addAButton("2", contentPane); addGlue(contentPane); addGlue(contentPane); addAButton("Button 3", contentPane); addCustomFiller(contentPane); addAButton("Long-Named Button 4", contentPane); addAButton("Long-Named Button 4", contentPane); addAButton("Button 5", contentPane); addAButton("Button 5", contentPane);setDefaultCloseOperation(EXIT_ON_CLOSE); } private void addAButton(String text, Container container) { private void addAButton(String text, Container container) { JButton button = new JButton(text); JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); container.add(button); } public void addGlue(Container container) { public void addGlue(Container container) { container.add(Box.createVerticalGlue()); container.add(Box.createVerticalGlue()); } public void addCustomFiller(Container container) { public void addCustomFiller(Container container) { Dimension minSize = new Dimension(300, 30); Dimension minSize = new Dimension(300, 30); Dimension prefSize = new Dimension(300, 30); Dimension maxSize = new Dimension(Short.MAX_VALUE, 30); container.add(new Box.Filler(minSize, prefSize, maxSize)); } public static void main(String args[]) { public static void main(String args[]) { BoxWindow window = new BoxWindow(); BoxWindow window = new BoxWindow(); window.setTitle("BoxLayout"); window.setTitle("BoxLayout"); window.pack(); window.pack(); window.setVisible(true); window.setVisible(true); }}

16 16BoxLayout import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BoxWindow extends JFrame { public BoxWindow() { public BoxWindow() { Container contentPane = getContentPane(); Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); BoxLayout.Y_AXIS)); addAButton("Button 1", contentPane); addAButton("Button 1", contentPane); addAButton("2", contentPane); addAButton("2", contentPane); addGlue(contentPane); addGlue(contentPane); addAButton("Button 3", contentPane); addCustomFiller(contentPane); addAButton("Long-Named Button 4", contentPane); addAButton("Long-Named Button 4", contentPane); addAButton("Button 5", contentPane); addAButton("Button 5", contentPane);setDefaultCloseOperation(EXIT_ON_CLOSE); }

17 17BoxLayout import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BoxWindow extends JFrame { public BoxWindow() { public BoxWindow() { private void addAButton(String text, Container container) { private void addAButton(String text, Container container) { JButton button = new JButton(text); JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); container.add(button); } public void addGlue(Container container) { public void addGlue(Container container) { container.add(Box.createVerticalGlue()); container.add(Box.createVerticalGlue()); }

18 18BoxLayout import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BoxWindow extends JFrame { public static void main(String args[]) { public static void main(String args[]) { BoxWindow window = new BoxWindow(); BoxWindow window = new BoxWindow(); window.setTitle("BoxLayout"); window.setTitle("BoxLayout"); window.pack(); window.pack(); window.setVisible(true); window.setVisible(true); }}

19 19FlowLayout Very simple - JPanel’s default Very simple - JPanel’s default Components in row(s) Components in row(s) At preferred size At preferred size Alignment Alignment FlowLayout.LEFTFlowLayout.LEFT FlowLayout.CENTREFlowLayout.CENTRE FlowLayout.RIGHTFlowLayout.RIGHT Gaps Gaps Default = 5Default = 5 Specifying - setter hGap vGap methods or via constructorSpecifying - setter hGap vGap methods or via constructor

20 20FlowLayout public class FlowWindow extends JFrame { public FlowWindow() { public FlowWindow() { Container contentPane = getContentPane(); Container contentPane = getContentPane(); //contentPane.setLayout(new FlowLayout()); //default = centre, 5, 5 //default = centre, 5, 5 contentPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 30, 5)); contentPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 30, 5)); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5")); contentPane.add(new JButton("Button 5"));setDefaultCloseOperation(EXIT_ON_CLOSE); }

21 21FlowLayout public static void main(String args[]) { FlowWindow window = new FlowWindow(); FlowWindow window = new FlowWindow(); window.setTitle("FlowLayout"); window.setTitle("FlowLayout"); window.pack(); window.pack(); window.setVisible(true); window.setVisible(true); }}

22 22GridLayout Grid of cells - all same size Grid of cells - all same size Components take all space in a cell Components take all space in a cell Gaps Gaps default = 5default = 5 use setter methods hGap and vGapuse setter methods hGap and vGap or via arguments to constructoror via arguments to constructor Re-sizing Re-sizing Cells resize to be as large as possible in given window / containerCells resize to be as large as possible in given window / container

23 23GridLayout public class GridWindow extends JFrame { public GridWindow() { public GridWindow() { Container contentPane = getContentPane(); Container contentPane = getContentPane(); //contentPane.setLayout(new GridLayout(0,2)); //contentPane.setLayout(new GridLayout(0,2)); contentPane.setLayout(new GridLayout(0,3,20,20)); contentPane.setLayout(new GridLayout(0,3,20,20)); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5")); contentPane.add(new JButton("Button 5")); setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE); }

24 24 GridBagLayout (1) Very flexible (and complex!) Very flexible (and complex!) Rows can have different heights Rows can have different heights Columns can have different lengths Columns can have different lengths Uses cells in a grid Uses cells in a grid GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); JPanel pane = new JPanel(); pane.setLayout(gridbag); //--- For each component to be added to this container: //---...Create the component... //---...Set instance variables in the GridBagConstraints instance... gridbag.setConstraints(theComponent, c); pane.add(theComponent);

25 25 GridBagLayout (2) Constraints Constraints set in an instance of a gridBagConstraints Objectset in an instance of a gridBagConstraints Object gridx and gridy - The row and column of the upper left of the componentgridx and gridy - The row and column of the upper left of the component Anchor - Where to display within cell when component is smaller than itAnchor - Where to display within cell when component is smaller than it fill - How to size component when cell is larger than components requested sizefill - How to size component when cell is larger than components requested size insets - External padding - min space between component and cell edgesinsets - External padding - min space between component and cell edges ipadx, ipady - Internal padding - What to add to min size of componentsipadx, ipady - Internal padding - What to add to min size of components weightx and weighty - How to distribute extra space (padding)weightx and weighty - How to distribute extra space (padding) gridwidth and gridheight - Number of columns or rows the component usesgridwidth and gridheight - Number of columns or rows the component uses More explanation here:More explanation here: http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbagConstraints.html http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbagConstraints.html http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbagConstraints.html Example explained very well here: Example explained very well here: http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbagExample.htmlhttp://java.sun.com/docs/books/tutorial/uiswing/layout/gridbagExample.html

26 26GridBagLayout public class GridBagWindow extends JFrame { public GridBagWindow() { public GridBagWindow() { JButton button; JButton button; Container contentPane = getContentPane(); Container contentPane = getContentPane(); GridBagLayout gridbag = new GridBagLayout(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); GridBagConstraints c = new GridBagConstraints(); contentPane.setLayout(gridbag); contentPane.setLayout(gridbag); //set all components to fill all available Horzontal space //set all components to fill all available Horzontal space c.fill = GridBagConstraints.HORIZONTAL; c.fill = GridBagConstraints.HORIZONTAL; button = new JButton("Button 1"); button = new JButton("Button 1"); c.weightx = 0.5; c.weightx = 0.5; c.gridx = 0; c.gridx = 0; c.gridy = 0; c.gridy = 0; gridbag.setConstraints(button, c); gridbag.setConstraints(button, c); contentPane.add(button); contentPane.add(button); button = new JButton("2"); button = new JButton("2"); c.weightx = 1.0; c.weightx = 1.0; c.gridx = 1; c.gridx = 1; c.gridy = 0; c.gridy = 0; gridbag.setConstraints(button, c); gridbag.setConstraints(button, c); contentPane.add(button); contentPane.add(button); button = new JButton("Button 3"); button = new JButton("Button 3"); c.weightx = 1.0; c.weightx = 1.0; c.gridx = 2; c.gridx = 2; c.gridy = 0; c.gridy = 0; gridbag.setConstraints(button, c); gridbag.setConstraints(button, c); contentPane.add(button); contentPane.add(button); button = new JButton("Long-Named Button 4"); button = new JButton("Long-Named Button 4"); c.ipady = 40; //make this component tall c.ipady = 40; //make this component tall c.weightx = 1.0; c.weightx = 1.0; c.gridwidth = 2; c.gridwidth = 2; c.gridx = 0; c.gridx = 0; c.gridy = 1; c.gridy = 1; gridbag.setConstraints(button, c); gridbag.setConstraints(button, c); contentPane.add(button); contentPane.add(button); button = new JButton("Button 5"); button = new JButton("Button 5"); c.ipady = 0; //reset to default c.ipady = 0; //reset to default c.weighty = 1.0; //request any extra vertical space c.weighty = 1.0; //request any extra vertical space c.anchor = GridBagConstraints.NORTH; //bottom of space c.anchor = GridBagConstraints.NORTH; //bottom of space c.insets = new Insets(10,0,0,0); //top padding c.insets = new Insets(10,0,0,0); //top padding c.gridx = 1; //aligned with button 2 c.gridx = 1; //aligned with button 2 c.gridwidth = 2; //2 columns wide c.gridwidth = 2; //2 columns wide c.gridy = 2; //third row c.gridy = 2; //third row gridbag.setConstraints(button, c); gridbag.setConstraints(button, c); contentPane.add(button); contentPane.add(button); setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE); }

27 27CardLayout Manages objects (usually JPanels) in sets Manages objects (usually JPanels) in sets Works much like tabbed pane Works much like tabbed pane Choose cards by Choose cards by Asking for card in order added to containerAsking for card in order added to container Going backwards or forwardsGoing backwards or forwards Specifying card by nameSpecifying card by name

28 28CardLayout public class CardWindow extends JFrame implements ItemListener { implements ItemListener { JPanel cards; JPanel cards; final static String BUTTONPANEL = "JPanel with JButtons"; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JTextField"; final static String TEXTPANEL = "JPanel with JTextField"; public CardWindow() { public CardWindow() { Container contentPane = getContentPane(); Container contentPane = getContentPane(); //Put the JComboBox in a JPanel to get a nicer look. //Put the JComboBox in a JPanel to get a nicer look. String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; JPanel cbp = new JPanel(); JPanel cbp = new JPanel(); JComboBox c = new JComboBox(comboBoxItems); JComboBox c = new JComboBox(comboBoxItems); c.setEditable(false); c.setEditable(false); c.addItemListener(this); c.addItemListener(this); cbp.add(c); cbp.add(c); //Use the default layout manager, BorderLayout //Use the default layout manager, BorderLayout contentPane.add(cbp, BorderLayout.NORTH); contentPane.add(cbp, BorderLayout.NORTH); cards = new JPanel(); cards = new JPanel(); cards.setLayout(new CardLayout()); cards.setLayout(new CardLayout()); JPanel p1 = new JPanel(); JPanel p1 = new JPanel(); p1.add(new JButton("Button 1")); p1.add(new JButton("Button 1")); p1.add(new JButton("Button 2")); p1.add(new JButton("Button 2")); p1.add(new JButton("Button 3")); p1.add(new JButton("Button 3")); JPanel p2 = new JPanel(); JPanel p2 = new JPanel(); p2.add(new JTextField("TextField", 20)); p2.add(new JTextField("TextField", 20)); cards.add(p1, BUTTONPANEL); cards.add(p1, BUTTONPANEL); cards.add(p2, TEXTPANEL); cards.add(p2, TEXTPANEL); contentPane.add(cards, BorderLayout.CENTER); contentPane.add(cards, BorderLayout.CENTER); setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void itemStateChanged(ItemEvent evt) { public void itemStateChanged(ItemEvent evt) { CardLayout cl = (CardLayout)(cards.getLayout()); CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, (String)evt.getItem()); cl.show(cards, (String)evt.getItem()); }

29 29 Choosing layout managers (1) In order to display a component in as much space as it can get, consider: In order to display a component in as much space as it can get, consider: BorderLayoutBorderLayout Component in centre Component in centre GridBagLayoutGridBagLayout fill=GridBagConstraints.BOTH fill=GridBagConstraints.BOTH BoxLayoutBoxLayout Component specifies very large preferred/maximum sizes Component specifies very large preferred/maximum sizes

30 30 Choosing layout managers (2) To display a few components in a compact row: To display a few components in a compact row: JPanel’s default FlowLayoutJPanel’s default FlowLayout BoxLayoutBoxLayout Display a few components of the same size in rows and columns Display a few components of the same size in rows and columns GridLayoutGridLayout

31 31 Choosing layout managers (3) Display a few components in a row or column, with different spacing between them and custom component sizes Display a few components in a row or column, with different spacing between them and custom component sizes BoxLayoutBoxLayout Display a complex layout that has many components Display a complex layout that has many components GridBagLayoutGridBagLayout Using JPanel grouping and hierarchiesUsing JPanel grouping and hierarchies

32 32 Layout managers - other layout features Absolute positioning of components Absolute positioning of components WhenWhen HowHow Customising layout managers Customising layout managers WhenWhen HowHow

33 33 Absolute positioning (1) Don’t do it; unless… Don’t do it; unless… component size isn’t affected by container size or font & look’n’feel changescomponent size isn’t affected by container size or font & look’n’feel changes e.g. desktop panes containing internal frames e.g. desktop panes containing internal frames custom container performs size & position calculations particular to containercustom container performs size & position calculations particular to container e.g. split panes e.g. split panes

34 34 Absolute positioning (2) Key points from NoneWindow.java Key points from NoneWindow.java Instruct window to use no Layout:Instruct window to use no Layout:.contentPane.setLayout(null);.contentPane.setLayout(null); Set components size and position withSet components size and position with XYZ.setBounds(x, y, width, height); XYZ.setBounds(x, y, width, height); Set window size with:Set window size with: window.setSize(x, y); window.setSize(x, y);

35 35 Absolute positioning public class NoneWindow extends JFrame { private JButton b1, b2, b3; private JButton b1, b2, b3; public NoneWindow() { public NoneWindow() { Container contentPane = getContentPane(); Container contentPane = getContentPane(); contentPane.setLayout(null); contentPane.setLayout(null); b1 = new JButton("one"); b1 = new JButton("one"); contentPane.add(b1); contentPane.add(b1); b2 = new JButton("two"); b2 = new JButton("two"); contentPane.add(b2); contentPane.add(b2); b3 = new JButton("three"); b3 = new JButton("three"); contentPane.add(b3); contentPane.add(b3); b1.setBounds(25, 5, 75, 20); b1.setBounds(25, 5, 75, 20); b2.setBounds(55, 35, 75, 20); b2.setBounds(55, 35, 75, 20); b3.setBounds(300, 200, 75, 30); b3.setBounds(300, 200, 75, 30); setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE); }

36 36 Custom layout managers (1) Ensure no existing manager does the job Ensure no existing manager does the job GridBagLayout / BoxLayoutGridBagLayout / BoxLayout Layout manager downloadsLayout manager downloads If your trying to do it, chances are someone else has done it already… If your trying to do it, chances are someone else has done it already… DECLARE use of external code in courseworkDECLARE use of external code in coursework

37 37 Custom layout managers (2) Create class which implements Layout Manager interface Create class which implements Layout Manager interface e.g. public class myManager implements LayoutManagere.g. public class myManager implements LayoutManager Must have 5 methods required by interface Must have 5 methods required by interface void addLayoutComponent(String, Component)void addLayoutComponent(String, Component) void removeLayoutComponent(Component)void removeLayoutComponent(Component) Dimension preferredLayoutSize(Container)Dimension preferredLayoutSize(Container) Dimension minimumLayoutSize(Container)Dimension minimumLayoutSize(Container) void layoutContainer(Container)void layoutContainer(Container) See below URL for more documentation See below URL for more documentation http://java.sun.com/docs/books/tutorial/uiswing/layout/custom.htmlhttp://java.sun.com/docs/books/tutorial/uiswing/layout/custom.html

38 38 Summary Creating a layout manager Creating a layout manager Consulting managers Consulting managers Types of managers Types of managers BorderLayout GridLayoutBorderLayout GridLayout BoxLayoutGridBagLayoutBoxLayoutGridBagLayout FlowLayoutCardLayoutFlowLayoutCardLayout Choosing managers Choosing managers Absolute positioning Absolute positioning Custom layout managers Custom layout managers Next : Abstract Model Widgets Next : Abstract Model Widgets


Download ppt "1 Java Swing Layout Management. 2 Laying out components Manage realized components Manage realized components Determine size and position Determine size."

Similar presentations


Ads by Google