Passo 1: Criar uma classe Aluno.java como os atributos matricula, nome e curso e seus respectivos métodos gets e sets
public class Aluno { private String matricula; private String nome; private String curso; public String getMatricula(){ return matricula; } public void setMatricula(String matricula){ this.matricula = matricula; } public String getNome(){ return nome; } public void setNome(String nome){ this.nome = nome; } public String getCurso(){ return curso; } public void setCurso(String curso){ this.curso = curso; }
Passo 2: Ainda na classe Aluno.java crie o método getAll() que retorne uma lista dos alunos cadastrados.
public static List getAll() { ArrayList list = new ArrayList(); Aluno aluno = new Aluno(); aluno.setMatricula("1111111"); aluno.setNome("Aluno 1"); aluno.setCurso("curso 1"); list.add(aluno); Aluno aluno2 = new Aluno(); aluno2.setMatricula("2222222"); aluno2.setNome("Aluno 2"); aluno2.setCurso("curso 2"); list.add(aluno2); Aluno aluno3 = new Aluno(); aluno3.setMatricula("3333333"); aluno3.setNome("Aluno 3"); aluno3.setCurso("curso 3"); list.add(aluno3); return list; }
Passo 3: Sobrescreva do método toString() na classe Aluno.java . A String retornada por esse método será utilizada no display do JComboBox.
public String toString() { return this.getNome(); }
Passo 4: Crie a classe Tela.java que herda o classe JFrame. A sujestão de imlementação desse JComboBox é fazer com que ele não guarde apenas o uma String do nome do alunos ou a matricula, mas que ele guarda o objeto Aluno como um todo e que exiba no display apenas o nome.
import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import java.util.Vector; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JOptionPane; public class Tela extends JFrame { JComboBox combo = null; JButton button = null; public Tela() { List listaAlunos = Aluno.getAll(); /* Variável que será utilizada para carregar o JComboBox que os dados dos alunos. */ Vector alunos = new Vector(); alunos.addAll(listaAlunos); /* Carrega o JComboBox com o vector Alunos */ combo = new JComboBox(alunos); button = new JButton("Ok"); /* Botão adicionado para mostrar que depois que foi selecionado uma opção do JComboBox é possível pegar o numero de matricula através no obejto Aluno contido no JComboBox. */ button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Aluno aluno = (Aluno) combo.getSelectedItem(); System.out.println("Matricula: "+aluno.getMatricula() ); } }); /* Faz com que após o fechamento dessa tela as outras telas em aberto não fechem também. */ this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setSize(300, 70); this.setLocationRelativeTo(null); this.setLayout(new GridLayout(1,1)); this.getContentPane().add(combo); this.getContentPane().add(button); } public static void main(String[] args) { Tela frame = new Tela(); frame.setVisible(true); } }
0 comentários:
Postar um comentário