Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

Embed Size (px)

Citation preview

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    1/35

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    2/35

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    3/35

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    4/35

    javax.swing

    javax.accessibility

    JOptionPane

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    5/35

    JFileChooser

    showOpenDialog

    int

    File getSelectedFile

    JOptionPane.showMessageDialog( null , "Minha mensagem!" );

    JFileChooser fileChooser = new JFileChooser();

    fileChooser.showOpenDialog( null );

    JFileChooser fileChooser = new JFileChooser();int retorno = fileChooser.showOpenDialog( null );

    if (retorno == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // faz alguma coisa com arquivo} else {

    // dialogo cancelado}

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    6/35

    JFileChooser JOptionPane

    EscolhedorDeXML

    br.com.caelum.argentum.ui escolhe

    public void escolhe() { try { JFileChooser chooser = new JFileChooser(); int retorno = chooser.showOpenDialog( null );

    if (retorno == JFileChooser.APPROVE_OPTION) { FileReader reader = new

    FileReader(chooser.getSelectedFile());

    1

    2

    3

    4

    5

    6

    7

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    7/35

    main

    caelum/16 Desktop

    LeitorXML().carrega(reader);

    Negocio primeiroNegocio = negocios.get(0); String mensagem = "Primeiro negócio: " +

    primeiroNegocio.getPreco();

    JOptionPane.showMessageDialog( null , mensagem); } } catch (FileNotFoundException e) { e.printStackTrace(); } }

    9

    10

    11

    1213

    14

    15

    16

    17

    public static void main(String[] args) { new EscolhedorDeXML().escolhe();}

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    8/35

    JFileChooser

    showOpenDialog

    JFrames JPanels JButtons

    public void escolhe() { try { JFileChooser chooser = new JFileChooser( "/caelum/cursos/16" ); int retorno = chooser.showOpenDialog( null ); //...

    chooser.setFileFilter( new FileNameExtensionFilter( "ApenasXML" , "xml" ));

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    9/35

    JPanel

    JFrame

    JButton botao = new JButton( "Carregar XML" );

    JButton botaoCarregar = new JButton( "Carregar XML" );JButton botaoSair = new JButton( "Sair" );

    JPanel painel = new JPanel();painel.add(botaoCarregar);painel.add(botaoSair);

    JFrame janela = new JFrame( "Argentum" );janela.add(panel);janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);janela.pack();janela.setVisible( true );

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    5 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    10/35

    setVisible boolean

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    11/35

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    12/35

    ActionListener

    actionPerformed

    ActionListener

    addActionListener

    ActionListener

    ActionListener

    public void actionPerformed(ActionEvent e) { // ... tratamento do evento aqui...}

    ActionListener nossoListener = ????;botao.addActionListener(nossoListener);

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    13/35

    actionPerformed new

    ActionListeners

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    14/35

    Externa.Interna

    private

    public class SairListener

    implements ActionListener{

    public class Externa { public class Interna {

    }

    }

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    15/35

    ActionListener

    new

    sairListener

    new

    ActionListener()

    JButton botaoSair = new JButton( "Sair" );

    ActionListener sairListener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); }};

    botaoSair.addActionListener(sairListener);

    JButton botaoSair = new JButton( "Sair" );botaoSair.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); }});

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    16/35

    ActionListener

    actionPerformed

    System.exit(0)

    ActionListener

    ArgentumUI br.com.caelum.argentum.ui

    montaTelamain

    public class ArgentumUI {

    public static void main(String[] args) { new ArgentumUI().montaTela(); }

    private void montaTela() { // TODO Auto-generated method stub

    } }

    1

    2

    3

    4

    5

    6 7

    8

    9

    10

    11

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    17/35

    montaTela()

    Argentum

    preparaJanela

    mostraJanela

    pack setSize

    setVisible

    mostraJanela

    janela

    public void montaTela() { preparaJanela(); preparaPainelPrincipal(); preparaBotaoCarregar(); preparaBotaoSair(); mostraJanela();}

    public class ArgentumUI {

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    18/35

    painelPrincipal

    ActionListeners

    new

    painelPrincipal

    preparaPainelPrincipal

    // main e montaTela

    private void preparaJanela() { janela = new JFrame( "Argentum" );

    janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

    // outros metodos prepara...

    private void mostraJanela() { janela.pack(); janela.setSize(540, 540); janela.setVisible( true ); }}

    private void preparaPainelPrincipal() { painelPrincipal = new JPanel(); janela.add(painelPrincipal);}

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    19/35

    private void preparaBotaoCarregar() { JButton botaoCarregar = new JButton( "Carregar XML" ); botaoCarregar.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new EscolhedorDeXML().escolhe(); } }); painelPrincipal.add(botaoCarregar);}

    private void preparaBotaoSair() { JButton botaoSair = new JButton( "Sair" ); botaoSair.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } });

    painelPrincipal.add(botaoSair);}

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    20/35

    JButton

    UIManager

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    21/35

    javax.swing.table

    JTable

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    22/35

    TableModel

    TableColumn

    JTable

    JTable

    JScrollPane

    painelPrincipal

    Object[][]

    AbstractTableModel

    JTable table = new JTable();

    // por padrão, vem sem bordas, então colocamos:table.setBorder( new LineBorder(Color.black));table.setGridColor(Color.black);table.setShowGrid( true );

    JScrollPane scroll = new JScrollPane();scroll.getViewport().setBorder( null );scroll.getViewport().add(table);scroll.setSize(450, 450);

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    23/35

    getColumnCount

    getRowCount

    getValueAt(row, column)

    NegociosTableModel

    List

    public class NegociosTableModel extends AbstractTableModel {

    private final List negocios;

    public NegociosTableModel(List negocios) {

    this .negocios = negocios; }

    @Override public int getColumnCount() { return 3; }

    @Override public int getRowCount() { return negocios.size(); }

    @Override public Object getValueAt( int rowIndex, int

    columnIndex) {

    1

    2

    3

    4

    5

    6 7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    24/35

    TableModel

    escolhe

    EscolhedorDeXML List

    switch (columnIndex) {

    case 0: return n.getPreco(); case 1:

    return n.getQuantidade(); case 2: return n.getData(); } return null ; }

    22

    23

    24

    25

    26

    2728

    29

    30

    31

    32

    public class EscolhedorDeXML {

    public List escolhe() { try { JFileChooser chooser = new JFileChooser( "/caelum/cursos/16" ); chooser.setFileFilter( new FileNameExtensionFilter( "Apenas XML" , "xml" )); int retorno = chooser.showOpenDialog( null );

    if (retorno == JFileChooser.APPROVE_OPTION) { FileReader reader = new FileReader(chooser.getSelectedFile());

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    25/35

    NegociosTableModel

    final table

    final

    } } catch (FileNotFoundException e) { e.printStackTrace(); } return Collections.emptyList();

    }}

    botaoCarregar.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { List negocios = new EscolheXML().escolher(); NegociosTableModel ntm = new NegociosTableModel(negocios); table.setModel(ntm); }});

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    26/35

    escolhe

    Collections

    emptyList()null

    ArgentumUI montaTela()

    public List escolhe() { try { JFileChooser chooser = new JFileChooser( "/caelum

    /cursos/16" ); int retorno = chooser.showOpenDialog( null );

    if (retorno == JFileChooser.APPROVE_OPTION) { FileReader reader =

    new FileReader(chooser.getSelectedFile());

    return new LeitorXML().carrega(reader); } } catch (FileNotFoundException e) { e.printStackTrace(); } return Collections.emptyList(); }

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    27/35

    ctrl + 1

    ctrl + 1

    NegociosTableModel

    AbstractTableModel

    ctrl + N

    br.com.caelum.argentum.ui AbstractTableModel

    Negocio

    private void montaTela() { preparaJanela(); preparaPainelPrincipal(); preparaTabela(); // linha adicionada! preparaBotaoCarregar(); preparaBotaoSair(); mostraJanela();}

    private void preparaTabela() { tabela = new JTable();

    JScrollPane scroll = new JScrollPane();scroll.getViewport().add(tabela);

    painelPrincipal.add(scroll); }

    12

    3

    4

    5

    6

    7

    8

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    28/35

    NegociosTableModel

    public class NegociosTableModel extends AbstractTableModel {

    private final List lista;

    // ctrl + 3 constructor

    @Override

    public int getRowCount() { return lista.size(); }

    @Override public int getColumnCount() { return 3; }

    @Override public Object getValueAt( int linha, int coluna) { Negocio negocio = lista.get(linha); switch (coluna) { case 0: return negocio.getPreco(); case 1: return negocio.getQuantidade(); case 2: return negocio.getData(); } return "" ;

    } }

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    1213

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    2930

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    29/35

    getValueAt

    SimpleDateFormat

    botaoCarregar.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { List lista = new EscolhedorDeXML().escolhe(); NegociosTableModel ntm = new NegociosTableModel(lista);

    tabela.setModel(ntm); }});

    @Override public Object getValueAt( int linha, int coluna) { Negocio negocio = lista.get(linha); switch (coluna) { case 0: return negocio.getPreco(); case 1: return negocio.getQuantidade();

    case 2: SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" ); return sdf.format(negocio.getData().getTime()); } return "" ;}

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    30/35

    NegociosTableModel

    getColumnName

    @Override public String getColumnName( int column) { switch (column) { case 0:

    1

    2

    3

    4

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    31/35

    JLabel ArgentumUI

    montaTela

    getValueAt

    NegociosTableModel

    case 1: return "Quantidade" ; case 2: return "Data" ; }

    return "" ; }

    6

    7

    8

    9

    10

    1112

    private void preparaTitulo() { JLabel titulo = new JLabel( "Lista de Negócios" ,SwingConstants.CENTER); titulo.setFont( new Font( "Verdana" , Font.BOLD, 25)); painelPrincipal.add(titulo);}

    @Override public Object getValueAt( int linha, int coluna) { Negocio negocio = lista.get(linha); switch (coluna) { case 0:

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    32/35

    Locale getCurrencyInstance

    Locale

    setLocale

    main ArgentumUI

    NumberFormat formatadorMoeda =NumberFormat.getCurrencyInstance(brasil);

    return formatadorMoeda.format(negocio.getPreco()); case 1: return negocio.getQuantidade();

    case 2: SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" ); return sdf.format(negocio.getData().getTime()); } return "" ;}

    public static void main(String[] args) { Locale.setDefault( new Locale( "pt" , "BR" )); new ArgentumUI().montaTela();}

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    33/35

    ArgentumUI

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    34/35

    Você encontra a Caelum também em:

    Blog Caelum CursosOnline

    Facebook Newsletter

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...

    35 29/05/201

  • 8/17/2019 Interfaces Gráficas Com Swing - Laboratório Java Com Testes, XML e Design Patterns

    35/35

    Casa doCódigo Twitter

    aces gráficas com Swing - Laboratório Java com Testes, XML e De... http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/inter...