Buscar

Exemplo JComboBox

Prévia do material em texto

Swing Swing –– Exemplo JComboBox
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exemplo7 extends JFrame 
implements ActionListener, ItemListener /*ItemListener reconhece qdo. ocorre 
a mudança da opção selecionada no componente combo* /a mudança da opção selecionada no componente combo* /
{
JLabel L1;
JTextField T1,T2,T3;
JComboBox combo; /*Declara combo como sendo um objeto da classe 
JComboBox.* /
JButton B1,B2,B3,B4,B5,B6,B7,B8;
public static void main(String args[])
{
JFrame Janela = new Exemplo7();
Janela.show();
}
Exemplo7() {
Swing Swing –– Exemplo JComboBox
Exemplo7() {
setTitle("Uso do JComboBox");
setSize(400,170);
getContentPane().setBackground(new Color(190,190,190));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Fecha a 
janela
L1 = new JLabel("Conteudo");
L1.setForeground(Color.blue);
L1.setFont(new Font("Arial", Font.BOLD, 15));
B1 = new JButton("Mostra Texto"); B1.addActionListener(this);
B2 = new JButton("Mostra Indice"); B2.addActionListener(this);
B3 = new JButton("Adiciona Item"); B3.addActionListener(this);
B4 = new JButton("Remove Item"); B4.addActionListener(this); 
B5 = new JButton("Remove Todos"); B5.addActionListener(this);
Swing Swing –– Exemplo JComboBox
B5 = new JButton("Remove Todos"); B5.addActionListener(this);
B6 = new JButton("Quant. Itens"); B6.addActionListener(this);
T1 = new JTextField(); T2 = new JTextField(); 
/*Contem a declaração um array de string chamado cores que será 
adicionado ao JComboBox. Este array deve ser formado por todos os 
elementos(opções) que o combo terá, separados por vírgula */
String[] cores = {"Branco","Vermelho","Azul","Verde"};
combo = new JComboBox(cores); //Inicialliza o objeto combo
combo.addItemListener(this); //Registra o objeto combo
getContentPane().setLayout(new GridLayout(5,2));
getContentPane().add(L1);
getContentPane().add(combo);
getContentPane().add(B1);
getContentPane().add(B4); 
getContentPane().add(B2);
Swing Swing –– Exemplo JComboBox
getContentPane().add(B2);
getContentPane().add(B5); 
getContentPane().add(B3);
getContentPane().add(T1); 
getContentPane().add(B6);
getContentPane().add(T2);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==B1)//mostra texto
L1.setText("Texto: "+combo.getSelectedItem()); /*Obtem o texto do item selecionado
if (e.getSource()==B2)//mostra indice
L1.setText("Indice: " + combo.getSelectedIndex()); /*Obtém o indice do item 
selecionado*/
if (e.getSource()==B3)//adiciona item
Swing Swing –– Exemplo JComboBox
if (e.getSource()==B3)//adiciona item
if (T1.getText().length()!=0) { 
combo.addItem(T1.getText()); //Adiciona o texto com um novo item
T1.setText(""); }
if (e.getSource()==B4)//remove item
combo.removeItemAt(combo.getSelectedIndex());
if (e.getSource()==B5)//remove todos os itens
combo.removeAllItems();
if (e.getSource()==B6)//quantidade de itens
T2.setText(""+combo.getItemCount()); 
}
//O método itemStateChanged É executado toda vez que o usuário seleciona uma das 
opções do componente JComboBox. 
public void itemStateChanged(ItemEvent e)
{ T1.setText(""+combo.getSelectedItem()); }
}
Swing Swing –– Exemplo JComboBox

Continue navegando