1
I wonder if it has how to organize accented words within a JComboBox
. So far I have managed to organize the words without accent, but the sharp ones, no.
Follows the code.
public class OrdenarCombo extends JFrame {
JComboBox<String> nomes = new JComboBox<>();
JTextField entra = new JTextField();
public OrdenarCombo() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Organizando o combo");
JButton addButton = new JButton("Add");
Container contentPane = this.getContentPane();
contentPane.add(nomes, BorderLayout.CENTER);
contentPane.add(entra, BorderLayout.NORTH);
contentPane.add(addButton, BorderLayout.SOUTH);
// ============ Lê o arquivo - inicia aqui ======================
try {
File myFile = new File("F:/Relacao.txt");
FileReader fileReader= new FileReader(myFile);
BufferedReader reader = new BufferedReader(fileReader);
// cria uma variável para armazenar cada linha quando for lida
String line;
// enquanto houver linhas para serem lidas, lê e exibe
while ((line = reader.readLine()) != null){
nomes.addItem(line);
}
reader.close();
}
catch (Exception e) {
e.printStackTrace();
}
// ==================== fecha aqui ================================
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String names;
names = entra.getText().trim();
if (names.equals("")) {
JOptionPane.showMessageDialog(null, "Digite algo");
return;
}
else if (nomes.getItemCount() > 0 ) {
for (int i = 0; i < nomes.getItemCount(); i++) {
if (names.compareTo((String)nomes.getItemAt(i)) < 0 ) {
nomes.insertItemAt(names, i);
return;
}
}
}
nomes.addItem(names);
} // fim de actionPerformed
} // fim de actionListener
); // fim do método addActionListener
} // fim do construtor OrdenarCombo
public static void main(String[] args) {
OrdenarCombo bf = new OrdenarCombo();
bf.pack();
bf.setVisible(true);
} // fim de main
} // fim da classe OrdenarCombro