Java First-Tier: Aplicações Construção de Interfaces com Swing.

Slides:



Advertisements
Apresentações semelhantes
Presenter’s Notes Some Background on the Barber Paradox
Advertisements

Exemplos de código WebForm1.aspx: Listas.aspx: TestaMarcadores.aspx:
APLICAÇÕES DE LINGUAGEM DE PROGRAMAÇÃO ORIENTADA A OBJETOS COMPONENTES GUI – PARTE I Prof. Thiago Pereira Rique
MATLAB – Aplicações a Engenharia Construção de Interfaces Gráficas
Programação em Java Prof. Maurício Braga
1 Interface Gráfica em Java Profa. Dra. Angélica Félix de Castro.
Eventos Marco Antonio, Arquiteto de Software – TJDF Novembro/2005
Criação de telas com Swing Arquiteto de Software – TJDF
APLICAÇÕES DE LINGUAGEM DE PROGRAMAÇÃO ORIENTADA A OBJETOS COMPONENTES GUI – PARTE III Prof. Thiago Pereira Rique
Programação orientada a objetos
Criação de interfaces gráficas
Capítulo 14 Componentes GUI: Parte 1
14.17 Tratamento de Eventos de Teclado
14.6  Campos de texto e uma introdução ao tratamento de eventos com classes aninhadas Um usuário interage com uma GUI para indicar as tarefas que o aplicativo.
Orientação a Objetos com Java
Linguagens de Programação Orientadas a Objetos
DIRETORIA ACADÊMICA NÚCLEO DE CIÊNCIAS HUMANAS E ENGENHARIAS DISCIPLINA: INGLÊS FUNDAMENTAL - NOITE PROFESSOR: JOSÉ GERMANO DOS SANTOS PERÍODO LETIVO
Módulo II Interface com usuário – GUI
Módulo III Padrões GOF-VI: MVC
Graphical User Interface (GUI) Prof(o): Alexandre Parra.
Linguagem de Programação II
Capítulo 15 Swing – Parte 1.
Capítulo 15 Swing – Parte 2.
Tutorial I: Criando a interface de uma aplicação em Java
Links Rollover image Tabelas
Fernanda Barroso Abril,2006
Aula de reposição Prof. Grace e Ângela
Componentes GUI GUI – Grafical User Interface – São construídas a partir de componentes GUI chamados de controles ou widgets (windows gadgets – engenhocas.
Programação Visual em Java
Java – Programação Gráfica
Linguagem de Programação III
Interface Grafica - Swing Professor Rafael Monteiro
Programação Prof.: Bruno Rafael de Oliveira Rodrigues.
Interface Gráfica (material da Profa. Luciana Nedel - www. inf. pucrs
SIMPLE PRESENT O Simple Present é o equivalente, na língua inglesa, ao Presente do Indicativo, na língua portuguesa. O Simple Present é usado para indicar.
Introdução a aplicativos Java
Estudo de Caso: um editor de documentos
Oficina Plone Criação de Formulários
10/ Daily Goal Sheet & Daily Checklist for Teachers Folha do Objetivo Diário & Checklist diário para os Professores By Por David Batty PSNC #7.
Pilhas Profa. Nádia Félix.
POO.
Jpanel e Diagramadores
Aulas 2 e 3 – Java – Prof. Marcelo Heitor # O método main e argumentos na linha de comando; # Fluxo padrão de entrada e saída; # A classe JOptionPane;
Stream – Fluxo I/O Manipulação de arquivos texto
POO - II Prof.: Jean Carlo Mendes
POO Interface Gráfica - Swing
Estrutura de Controle em JAVA
Introdução PLB-II (Java) – Prof. Alberto  Mais componentes da GUI.
GUI – Graphic User Interface Prof. M.Sc. Ronnison Reges Vidal.
Video Capítulo 8: Impasses. Silberschatz, Galvin and Gagne  Video Operating System Concepts Assuntos n Modelo de Sistemas n Carcterização de.
Programação Prof.: Bruno Rafael de Oliveira Rodrigues.
Skinning Flex 3 Gabriela Trindade Perry. Definitions Skinning Imagens, swf e classes CSS (estilos personalizados) Programaticamente Themes “Nativos” ou.
1 Visual Basic Maria Alice Grigas Varella Ferreira EP-USP Abril 2003.
Programação Gráfica em Java Introdução
UNIVERSIDADE FEDERAL DE SANTA CATARINA CTC - CENTRO TECNOLÓGICO INE - DEPARTAMENTO DE INFORMÁTICA E ESTATÍSTICA DISCIPLINA DE COMPUTAÇÃO COM OBJETOS DISTRIBUÍDOS.
O que são os alertas do Google?
WIKISPACE Tutorial. Índice TópicoSlide Creating an accountslide 3 Introductory informationslide 4 Edição básicaslides 5 – 7 Inserção de arquivos e imagensslides.
Programação Orientada a Objetos Prof. Rafael Silva.
Limit Equlibrium Method. Limit Equilibrium Method Failure mechanisms are often complex and cannot be modelled by single wedges with plane surfaces. Analysis.
© 2007 IBM Corporation Academic Initiative 07/05/07 Aula 2 – Parte 1: Java Basics Autores: Argemiro José de Juliano Marcos
Java Módulo II Projeto Final.
Programação Gráfica em Java Swing Aula Na aula passada vimos: Thread Animação Técnica de desenho eficiente (double-bufferring)
Principais Classes Swing AWT  As classes e interfaces localizadas nos pacotes:  java.awt  javax.swing  são recursos para o desenvolvimento de GUIs.
JFC e Swing Thiago Medeiros Barros 18/08/2011. JFC Java Foundation Classes é um conjunto de componentes e serviços que simplificam o desenvolvimento e.
Projeto Avançado de Software II 6 – Popular Movies APP Eduardo de Lucena Falcão.
Abril 2016 Gabriel Mormilho Faculdade de Economia, Administração e Contabilidade da Universidade de São Paulo Departamento de Administração EAD5853 Análise.
Learning english with comics …………….. Aprendendo inglês com quadrinhos.
TQS - Teste e Qualidade de Software (Software Testing and Quality) Geração Automática de Casos de Teste com a Ferramenta.
Wondershare software On the [View] menu, point to [Master], and then click [Slide Master] or [Notes Master].
Pesquisadores envolvidos Recomenda-se Arial 20 ou Times New Roman 21.
Transcrição da apresentação:

Java First-Tier: Aplicações Construção de Interfaces com Swing

2 Swing versus AWT l Conjunto maior de elementos l Maior consistência de uso entre plataformas l Pacote javax.swing l Pacote de extensão no Java 1.1 l Incorporado como pacote básico no Java 2

3 Swing: Características l Componentes leves l Diagramadores & Listeners do AWT l Look & Feel configurável l Arquitetura MVC

4 Arquitetura MVC l O Swing adota uma arquitetura conhecida como Model-View-Controller (MVC) –Modelo = dados –Visão = aparência –Controle = comportamento

5 Hierarquia de Classes Object Component Container Window Frame JFrame JComponent JLabel JAbstractButton JButton JToggleButton...

6 Classe JFrame l Modela uma janela do sistema nativo l Equivalente, no Swing, à classe Frame do AWT

7 Estrutura de um JFrame © The Java TM Tutorial

8 l JRootPane l A lightweight container used behind the scenes by JFrame, JDialog, JWindow, JApplet, and JInternalFrame. For task-oriented information on functionality provided by root panes see How to Use Root Panes, a section in The Java Tutorial.How to Use Root Panes l The following image shows the relationships between the classes that use root panes.

9

10 l The "heavyweight" components (those that delegate to a peer, or native component on the host system) are shown with a darker, heavier box. The four heavyweight JFC/Swing containers (JFrame, JDialog, JWindow, and JApplet) are shown in relation to the AWT classes they extend. These four components are the only heavyweight containers in the Swing library. The lightweight container JInternalPane is also shown. All five of these JFC/Swing containers implement the RootPaneContainer interface, and they all delegate their operations to a JRootPane (shown with a little "handle" on top). Note: The JComponent method getRootPane can be used to obtain the JRootPane that contains a given component. The diagram at right shows the structure of a JRootPane. A JRootpane is made up of a glassPane, an optional menuBar, and a contentPane. (The JLayeredPane manages the menuBar and the contentPane.) The glassPane sits over the top of everything, where it is in a position to intercept mouse movements. Since the glassPane (like the contentPane) can be an arbitrary component, it is also possible to set up the glassPane for drawing. Lines and images on the glassPane can then range over the frames underneath without being limited by their boundaries.

11 l Although the menuBar component is optional, the layeredPane, contentPane, and glassPane always exist. Attempting to set them to null generates an exception. l To add components to the JRootPane (other than the optional menu bar), you add the object to the contentPane of the JRootPane, like this: l rootPane.getContentPane().add(child);

12 l The same principle holds true for setting layout managers, removing components, listing children, etc. All these methods are invoked on the contentPane instead of on the JRootPane. Note: The default layout manager for the contentPane is a BorderLayout manager. However, the JRootPane uses a custom LayoutManager. So, when you want to change the layout manager for the components you added to a JRootPane, be sure to use code like this: rootPane.getContentPane().setLayout(new BoxLayout()); If a JMenuBar component is set on the JRootPane, it is positioned along the upper edge of the frame. The contentPane is adjusted in location and size to fill the remaining area. (The JMenuBar and the contentPane are added to the layeredPane component at the JLayeredPane.FRAME_CONTENT_LAYER layer.) The layeredPane is the parent of all children in the JRootPane -- both as the direct parent of the menu and the grandparent of all components added to the contentPane. It is an instance of JLayeredPane, which provides the ability to add components at several layers. This capability is very useful when working with menu popups, dialog boxes, and dragging -- situations in which you need to place a component on top of all other components in the pane. l The glassPane sits on top of all other components in the JRootPane. That provides a convenient place to draw above all other components, and makes it possible to intercept mouse events, which is useful both for dragging and for drawing. Developers can use setVisible on the glassPane to control when the glassPane displays over the other children. By default the glassPane is not visible.

13

14 l Main differences in use compared to Frame: l Components go in the "content pane", not directly in the frame. Changing other properties (layout manager, background color, etc.) also apply to the content pane. Access content pane via getContentPane, or if you want to replace the content pane with your container (e.g. a JPanel), use setContentPane. l JFrames close automatically when you click on the close button (unlike AWT Frames). However, closing the last JFrame does not result in your program exiting Java. So your "main" JFrame still needs a WindowListener. l You get Java (Metal) look by default, so you have to explicitly switch if you want native look.

15 Métodos de JFrame JRootPane getRootPane() Container getContentPane() void setJMenuBar(JMenuBar menubar) JMenuBar getJMenuBar() void setDefaultCloseOperation(int op) –DO_NOTHING_ON_CLOSE –HIDE_ON_CLOSE –DISPOSE_ON_CLOSE –EXIT_ON_CLOSE

16 Exemplo de JFrame JFrame f = new JFrame("Teste"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.show();

17 Classe JComponent l Superclasse de muitos elementos do Swing, disponibiliza métodos para controlar: –Tool tips –Bordas –Propriedades

18 Métodos de JComponent void setToolTipText(String text) String getToolTipText() void setBorder(Border border) Border getBorder() final void putClientProperty(Object key, Object value) final Object getClientProperty(Object key)

19 Classe JLabel l Essa classe modela um texto e/ou imagem não editável, isto é, sem interação com o usuário l É o equivalente, no Swing, ao Label do AWT, só que com mais recursos l Pode-se controlar tanto o alinhamento horizontal como o vertical, e o JLabel pode passar o foco para outro elemento

20 Métodos de JLabel void setText(String text) void setIcon(Icon icon) void setIconTextGap(int gap) void setHorizontalAlignment(int a) void setVerticalAlignment(int a) void setLabelFor(Component c) void setDisplayedMnemonic(int key) void setDisplayedMnemonic(char aChar)

21 Exemplo de JLabel JFrame f = new JFrame("Teste"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel l = new JLabel("Isso é um JLabel"); l.setIcon(new ImageIcon("javalogo.gif")); Container cp = f.getContentPane(); cp.add(l); f.pack(); f.show();

22 Classe JButton l Modela um push-button l Sendo uma sub-classe de AbstractButton, herda os métodos getLabel e setLabel que permitem consultar e alterar seu texto

23 Métodos de JButton JButton(String text) JButton(String text, Icon icon) void setLabel(String label) String getLabel()

24 Exemplo de JButton JFrame f = new JFrame("Teste"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b1 = new JButton("Botão 1"); JButton b2 = new JButton("Botão 2"); Container cp = f.getContentPane(); cp.setLayout(new GridLayout(1,0)); cp.add(b1); cp.add(b2); f.pack(); f.show();

25 Classe JRadioButton l Modela um botão de escolha que pode ser marcado e desmarcado l Objetos do tipo JRadioButton são organizados em grupos l Apenas um único botão de um grupo pode estar marcado em um dado momento

26 Métodos de JRadioButton JRadioButton(String label) JRadioButton(String label, boolean state) boolean isSelected() void setSelected(boolean state)

27 Classe ButtonGroup l Cria um “escopo” de exclusão para um grupo de botões l Basta criar um ButtonGroup e adicionar a ele os JRadioButtons que compõem o grupo

28 Métodos de ButtonGroup void add(AbstractButton b) ButtonModel getSelection() boolean isSelected(ButtonModel m) void setSelected(ButtonModel m, boolean state)

29 Classe JCheckBox l Modela um botão de escolha que pode ser marcado e desmarcado

30 Métodos de JCheckBox public JCheckBox(String label) public JCheckBox(String label, boolean state) public boolean isSelected() public void setSelected(boolean state)

31 Classe JTextComponent l Classe abstrata que modela o que há de comum entre diferentes elementos de edição de texto public void setText(String t) public String getText() public void setEditable(boolean b)

32 Elementos de Edição de Texto JTextArea JEditorPane JTextPane © The Java TM Tutorial

33 Classe JTextField l Cria um campo de edição de texto de uma linha JTextField() JTextField(String text) JTextField(int columns) JTextField(String text, int columns) void setColumns(int columns)

34 Classe JPasswordField l Estende JTextField l Caracteres digitados não são exibidos JPasswordField() JPasswordField(int columns) JPasswordField(String text, int columns) char[] getPassword() void setEchoChar(char c)

35 Classe JTextArea l Cria um campo de edição de texto com múltiplas linhas JTextArea(int rows, int columns) JTextArea(String text, int rows, int columns) void append(String t) void insert(String t, int pos) void setLineWrap(boolean wrap) void setWrapStyleWord(boolean word)

36 Classe JTextPane l Componente de texto que suporta atributos representados graficamente (styled text) l Permite o uso de diversas “fontes” no texto l Permite a inclusão de imagens e de outros componentes

37 Classe JPanel l Modela um container sem decoração, normalmente utilizado para estruturar a interface l Equivalente, no Swing, à classe Panel do AWT

38 Métodos de JPanel JPanel() JPanel(LayoutManager mgr) void setLayout(LayoutManager mgr) Component add(Component comp) void add(Component c, Object constraints)

39 Exemplo de JPanel JFrame f = new JFrame("Teste"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b1 = new JButton("Botão 1"); JButton b2 = new JButton("Botão 2"); JPanel p = new JPanel(); p.add(b1); p.add(b2); f.getContentPane().add(p); f.pack(); f.show();

40 Exemplos de Bordas © The Java TM Tutorial

41 Mais exemplos... © The Java TM Tutorial

42 BorderFactory l Contém métodos para a criação de diferentes tipos de bordas static Border createLineBorder(Color color) static Border createEtchedBorder() static TitledBorder createTitledBorder(String t) static TitledBorder createTitledBorder(Border b, String t)

43 Painéis, Bordas e Botões JCheckBox JRadioButton

44 Classe JList l Modela um elemento que apresenta uma lista de opções ao usuário l Permite a seleção simples (um único elemento), ou múltipla (vários elementos)

45 Métodos de JList JList(Object[] listData) void setSelectionMode(int selectionMode) int getSelectedIndex() int[] getSelectedIndices() Object getSelectedValue() Object[] getSelectedValues()

46 Exemplo de JList JFrame f = new JFrame("Teste"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String[] nomes = {"a", "b", "c", "d", "e", "f"}; JList l = new JList(nomes); Container cp = f.getContentPane(); cp.add(l); f.pack(); f.show();

47 Barras de Rolagem l No Swing, ao invés de repetir o código que implementa o uso de barras de rolagem (scrollbars) em diferentes elementos, foi criado um elemento de interface cujo único objetivo é fornecer essa funcionalidade l A classe JScrollPane modela esse elemento e a interface Scrollable define o que cada elemento deve ter para poder ser tratado por um JScrollPane

48 Elementos Scrollable l As classes JList, JTextComponent, JTree e JTable implementam a interface Scrollable e, portanto, suas instâncias podem ser usadas com JScrollPane

49 Exemplo: JScrollPane com JList JFrame f = new JFrame("Teste"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String[] nomes = {"a", "b", "c", "d", "e", "f"}; JList l = new JList(nomes); JScrollPane sp = new JScrollPane(l); Container cp = f.getContentPane(); cp.add(sp); f.pack(); f.show();

50 Explorando a Arquitetura MVC l Como os dados (o modelo) não fazem parte integrante do elemento de interface que os exibe, podemos gerenciá-los em separado l Por exemplo, é possível exibir um mesmo conjunto de dados em mais de um elemento de interface, simultaneamente l Também é possível fazer com que o elemento de interface use os dados originais, sem copiá-los

51 Exemplo de Uso l Suponha que você tem uma lista de nomes muito grande e deseja exibí-la em uma JList l Usando a forma que vimos, esses nomes seriam copiados para dentro da lista l Para evitar essa replicação, podemos utilizar um modelo próprio, que permitirá à JList acessar diretamente a lista de nomes

52 Interface ListModel l Define o modelo usado pela classe JList l Abrange dois aspectos: 1.o acesso aos dados 2.o controle da modificação dos dados

53 Métodos de ListModel int getSize() Object getElementAt(int index) void addListDataListener( ListDataListener l) void removeListDataListener( ListDataListener l)

54 De Volta ao Exemplo l Imagine que os nomes estão armazenados em um array de String l Assumindo que a lista de nomes não é modificada, podemos ignorar o listener l Basta, então, definir uma classe que implemente ListModel e utilize o array como fonte dos dados

55 Criando um Modelo class ListaDeNomes implements ListModel { private String[] nomes; ListaDeNomes(String[] nomes) { this.nomes = nomes; } public int getSize() { return nomes.length; } public Object getElementAt(int index) { return nomes[index]; } public void addListDataListener(ListDataListener l) {} public void removeListDataListener(ListDataListener l) {} }

56 Usando o Modelo JFrame f = new JFrame("Teste"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String[] nomes = {"a", "b", "c", "d", "e", "f"}; JList l = new JList(new ListaDeNomes(nomes)); Container cp = f.getContentPane(); cp.add(new JScrollPane(l)); f.pack(); f.show();

57 Classe JOptionPane l Métodos estáticos para a criação desses diálogos simples l Estrutura básica: ícone mensagem entrada de dados botões de opção

58 MessageDialog l Exibe uma mensagem e aguarda OK do usuário INFORMATION QUESTION WARNING ERROR PLAIN

59 Método showMessageDialog void showMessageDialog(Component parentComponent, Object message); void showMessageDialog(Component parentComponent, Object message, String title, int messageType); void showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon);

60 Exemplo de MessageDialog JOptionPane.showMessageDialog(janela, "Cuidado com o apagão!", "ATENÇÃO", JOptionPane.WARNING_MESSAGE);

61 ConfirmDialog l Exibe uma mensagem e obtém uma confirmação (YES/NO, OK/CANCEL) l Conjuntos de botões de opção (optionType): –JOptionPane.DEFAULT_OPTION –JOptionPane.YES_NO_OPTION –JOptionPane.YES_NO_CANCEL_OPTION –JOptionPane.OK_CANCEL_OPTION

62 Método showConfirmDialog int showConfirmDialog(Component parentComponent, Object message); int showConfirmDialog(Component parentComponent, Object message, String title, int optionType); int showConfirmDialog(Component parentComponent, Object message, String title, int optionType int messageType, Icon icon);

63 Exemplo de ConfirmDialog int resp = JOptionPane.showConfirmDialog(janela, "Prossegue com a tarefa?", "Escolha uma opção", JOptionPane.OK_CANCEL_OPTION); OK_OPTIONCANCEL_OPTION CLOSED_OPTION

64 InputDialog l Exibe uma mensagem e obtém um valor de entrada do usuário –campo de texto editável –combo box

65 Método showInputDialog String showInputDialog(Component parentComponent, Object message); String showInputDialog(Component parentComponent, Object message, String title, int messageType); Object showInputDialog(Component parentComponent, Object message, String title, int messageType Icon icon, Object[] selectionValues, Object defaultSelection);

66 Exemplo de InputDialog String nome = JOptionPane.showInputDialog(janela, "Qual é o seu nome?", "Identifique-se", JOptionPane.QUESTION_MESSAGE);

67 OptionDialog l Exibe uma mensagem (ou objeto) e obtém uma opção escolhida pelo usuário l O número de botões e seus textos são configuráveis l A opção default é configurável

68 Método showOptionDialog int showOptionDialog(Component parentComponent, Object message, String title, int optionType int messageType, Icon icon, Object[] options, Object initialValue);

69 Exemplo de OptionDialog Object[] opções = {"Sim", "Não", "Mais Tarde", "Amanhã", "Sei lá!"}; int resp = JOptionPane.showOptionDialog(janela, "Prossegue com a tarefa?", "Escolha uma opção", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, opções, opções[0]);

70 Exemplo de OptionDialog

71 Classe JFileChooser l É comum uma aplicação abrir e salvar arquivos l A classe JFileChooser implementa uma caixa de diálogo que permite que o usuário navegue pelo sistema de arquivos –semelhante às usadas por aplicações “nativas”

72 Diálogo de Seleção de Arquivos

73 Métodos de JFileChooser void setCurrentDirectory(File dir) void setSelectedFile(File file) void setMultiSelectionEnabled(boolean b) void setFileFilter(FileFilter filter) int showOpenDialog(Component parent) int showSaveDialog(Component parent) int showDialog(Component parent, String approveButtonText) File getSelectedFile() File[] getSelectedFiles()

74 Exemplo de JFileChooser JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File("c:\\jdk1.3")); int res = chooser.showOpenDialog(janela); if (res == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); System.out.println(file.getName()); }

75 Gerenciadores de Layout l A layout manager is an object that implements the LayoutManager interface* and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container.*

76 Gerenciadores de Layout l Setting the Layout Manager l As a rule, the only containers whose layout managers you need to worry about are JPanels and content panes. Each JPanel object is initialized to use a FlowLayout, unless you specify differently when creating the JPanel. Content panes use BorderLayout by default. If you don't like the default layout manager that a panel or content pane uses, you're free to change it to a different one. JPanels content panes l You can set a panel's layout manager using the JPanel constructor. For example: l JPanel panel = new JPanel(new BorderLayout()); After a container has been created, you can set its layout manager using the setLayout method. For example: Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); l Although we recommend that you use layout managers, you can perform layout without them. By setting a container's layout property to null, you make the container use no layout manager. With this strategy, called absolute positioning, you must specify the size and position of every component within that container. One drawback of absolute positioning is that it doesn't adjust well when the top-level container is resized. It also doesn't adjust well to differences between users and systems, such as different font sizes and locales.locales

77 l Adding Components to a Container l When you add components to a panel or content pane, the arguments you specify to the add method depend on the layout manager that the panel or content pane is using. For example, BorderLayout requires that you specify the area to which the component should be added, using code like this: pane.add(aComponent, BorderLayout.PAGE_START); The how-to section for each layout manager has details on what, if any, arguments you need to specify to the add method. Some layout managers, such as GridBagLayout and SpringLayout, require elaborate setup procedures. Many layout managers, however, simply place components based on the order they were added to their container. l Swing containers other than JPanel and content panes generally provide API that you should use instead of the add method. For example, instead of adding a component directly to a scroll pane (or, actually, to its viewport), you either specify the component in the JScrollPane constructor or use setViewportView. Because of specialized API like this, you don't need to know which layout manager (if any) many Swing containers use. (For the curious: scroll panes happen to use a layout manager named ScrollPaneLayout.)scroll pane l For information about how to add components to a specific container, see the how-to page for the container. You can find the component how-to pages using A Visual Index to the Swing Components.A Visual Index to the Swing Components

78 l Providing Size and Alignment Hints l Sometimes you need to customize the size hints that a component provides to its container's layout manager, so that the component will be laid out well. You can do this by specifying one or more of the minimum, preferred, and maximum sizes of the component. You can invoke the component's methods for setting size hints — setMinimumSize, setPreferredSize, and setMaximumSize. Or you can create a subclass of the component that overrides the appropriate getter methods — getMinimumSize, getPreferredSize, and getMaximumSize. Here is an example of making a component's maximum size unlimited: component.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); Many layout managers don't pay attention to a component's requested maximum size. However, BoxLayout and SpringLayout do. l Besides providing size hints, you can also provide alignment hints. For example, you can specify that the top edges of two components should be aligned. You set alignment hints either by invoking the component's setAlignmentX and setAlignmentY methods, or by overriding the component's getAlignmentX and getAlignmentY methods. Although most layout managers ignore alignment hints, BoxLayout honors them. You can find examples of setting the alignment in How to Use BoxLayout.How to Use BoxLayout