Galera…estou com um algoritmo que cria 2 ComboBox dinamicos…ou seja, ao escolher uma opção no 1º combo, as opçoes do 2º combo se alteram. DUVIDA …(Gostaria de incluir um 3º combobox que se alterasse de acordo com a escolha do 2º combobox).
Essas escolhas precisam ser gravadas em um arquivo (separadas por ponto e virgula).
Estou QUEIMANDO FOSFATO e desenvolvendo aos poucos esse programa.
Agradeço se alguem puder contribuir nessa tarefa pois ainda estou engatinhando no JAVA.
Segue abaixo o algoritmo que consegui fazer até agora.
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class ComboDinamico extends JFrame {
private JComboBox primeiroComboBox;
private JComboBox segundoComboBox;
private JPanel elPanel;
public static void main(String args[]) {
new ComboDinamico().show();
}
public ComboDinamico() {
initComponents();
initializeComponents();
}
private void initComponents() {
primeiroComboBox = new JComboBox();
segundoComboBox = new JComboBox();
elPanel = new JPanel();
elPanel.setLayout(new FlowLayout());
elPanel.setBackground(Color.blue);
elPanel.add(primeiroComboBox);
elPanel.add(segundoComboBox);
getContentPane().add(elPanel);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(d.width / 2 - 320, d.height / 2 - 240);
setSize(new java.awt.Dimension(700, 100));
setResizable(false);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
}
private void initializeComponents() {
addPrimeiroChoices();
segundoComboBox.addItem("- Selecione a Categoria -");
}
private void addSegundoChoices(int country) {
segundoComboBox.removeAllItems();
segundoComboBox.addItem("- Escolha a segunda opcao -");
String estados[][] = init_States();
for (int i = 0; i <= estados[country].length - 1; i++) {
segundoComboBox.addItem(estados[country][i]);
}
}
//Adicionando a lista de opcao em countryComboBox
private void addPrimeiroChoices() {
//adicionando na lista
primeiroComboBox.addItem("- Selecione a Categoria -");
String opcao[] = init_countries();
for (int i = 0; i < opcao.length ; i++) {
primeiroComboBox.addItem(opcao[i]);
}
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
addSegundoChoices(primeiroComboBox.getSelectedIndex());
}
};
primeiroComboBox.addActionListener(lst);
}
public String[] init_countries() {
String[] sPrimeiroString = { "FONTE", "TEXTO","BORDA"};
return sPrimeiroString;
}
public String[][] init_States() {
String sStateArray[][] = new String[4][];
sStateArray[0] = new String[] { "" };
sStateArray[1] = new String[] { "COR-FONTE", "TIPO", "TAMANHO",
"ESTILO-FONTE"};
sStateArray[2] = new String[] { "COR", "ESPACAMENTO", "ALINHAMENTO"};
sStateArray[3] = new String[] { "ESPESSURA", "ESTILO", "COR"};
return sStateArray;
}
}