Compartilhar código

2 respostas
wesleysanches

Olá a todos.
Acabei de fazer um programa sobre o uso de JList bem interessante, mas tbm bem básico. Ele está todinho na unha, fiz ele na mão e espero q ajude os usuários ai do GUJ.
Segue ele abaixo:

/******************************************************************************
    Descrição: Programa simples em java para demonstrar algumas utilizações
               básicas com o JList. E pequenas ações com JButtons e JLabels

    Versão: 1.0.0
    Autor: Wesley Sanches
******************************************************************************/
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class UsandoJlist
{
    //-------------------------------------------------------------------------
    // VARIAVEIS E COMPONENTES
    //-------------------------------------------------------------------------
    private JLabel lblTitulo = new JLabel();
    private DefaultListModel listModel = new DefaultListModel();
    private JList lstLista = new JList(listModel);
    private JButton btnNovo = new JButton("Adicionar");
    private JButton btnAlterar = new JButton("Alterar");
    private JButton btnExcluir = new JButton("Excluir");
    private JButton btnFinalizar = new JButton("Finalizar");

    //-------------------------------------------------------------------------
    // CONSTRUCTOR DA CLASSE
    //-------------------------------------------------------------------------
    public UsandoJlist()
    {
        JFrame Janela = new JFrame("Usando o Jlist - Básico");
        Janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Janela.setSize(380, 240);
        Janela.setLocationRelativeTo(null);
        Janela.getContentPane().setLayout(null);
        Janela.setResizable(false);
        Janela.setVisible(true);

        // lblTitulo - PROPRIEDADES
        Janela.getContentPane().add(lblTitulo);
        lblTitulo.setBounds(10, 5, 210, 30);
        lblTitulo.setText("Usando o JList - Básico");
        lblTitulo.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 18));
        lblTitulo.setForeground(java.awt.Color.BLUE);

        // lstLista - PROPRIEDADES
        Janela.getContentPane().add(lstLista);
        lstLista.setBounds(5, 45, 360, 120);
        lstLista.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK));

        // btnNovo - PROPRIEDADES
        Janela.getContentPane().add(btnNovo);
        btnNovo.setBounds(5, 168, 90, 25);
        btnNovo.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                String Item = "";
                Item = JOptionPane.showInputDialog(null, "Digite os dados do item a ser inserido:", "Novo Item", JOptionPane.QUESTION_MESSAGE);
                if (Item == "") { return; }
                if (Item == null) { return; }
                listModel.addElement(Item);
            }
        });

        // btnAlterar - PROPRIEDADES
        Janela.getContentPane().add(btnAlterar);
        btnAlterar.setBounds(98, 168, 75, 25);
        btnAlterar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                // Verifica se a lista tem valores
                if (lstLista.getSelectedIndex() == -1)
                { JOptionPane.showMessageDialog(null, "Não há itens a serem alterados, ou você não selecionou nenhum.", "Erro !!", JOptionPane.ERROR_MESSAGE); return; }

                String Item = "";
                Item = JOptionPane.showInputDialog(null, "Atualize os dados:", "Alterar Item", JOptionPane.QUESTION_MESSAGE);
                listModel.remove(lstLista.getSelectedIndex());
                listModel.addElement(Item);
            }
        });

        // btnExcluir - PROPRIEDADES
        Janela.getContentPane().add(btnExcluir);
        btnExcluir.setBounds(176, 168, 75, 25);
        btnExcluir.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                // Verifica se a lista tem valores
                if (lstLista.getSelectedIndex() == -1)
                { JOptionPane.showMessageDialog(null, "Não há itens a serem excluídos, ou você não selecionou nenhum.", "Erro !!", JOptionPane.ERROR_MESSAGE); return; }

                // Remove os valores
                if (JOptionPane.showConfirmDialog(null, "Tem certeza que deseja excluir o seguinte item:\n\n" + lstLista.getSelectedValue(), "Confirmação !!", JOptionPane.YES_NO_OPTION) == 0)
                { listModel.remove(lstLista.getSelectedIndex()); JOptionPane.showMessageDialog(null, "Item removido com sucesso !!", "Sucesso !!", JOptionPane.INFORMATION_MESSAGE); }
            }
        });

        // btnFinalizar - PROPRIEDADES
        Janela.getContentPane().add(btnFinalizar);
        btnFinalizar.setBounds(280, 168, 85, 25);
        btnFinalizar.addActionListener(new java.awt.event.ActionListener()
        {
           public void actionPerformed(java.awt.event.ActionEvent evt)
           {
               if (JOptionPane.showConfirmDialog(null, "Tem certeza que deseja sair ??", "Confirmação !!", JOptionPane.YES_NO_OPTION) == 0)
               { System.exit(0); }
           }
        });
    }

    //-------------------------------------------------------------------------
    // METODO MAIN - INICIO DO PROGRAMA
    //-------------------------------------------------------------------------
    public static void main(String[] args)
    { new UsandoJlist(); }
}

2 Respostas

edufera10

Boa iniciativa wesleysanches com certeza muitos gujianos irão se beneficiar

wesleysanches

Olá. Obrigado.
Acho que o pessoal vai gostar sim. É bem simples no começo quando comecei a mexer com JAVA tive muita dificuldade.

Acredito q essse exemplo ajdue muita gente.
Abraços !!

Criado 20 de agosto de 2009
Ultima resposta 21 de ago. de 2009
Respostas 2
Participantes 2