Organize accented words in a list

Asked

Viewed 141 times

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

1 answer

1

One way is to add all the names in a list and use one Collator, see an example:

List<String> listaNomes = new ArrayList<String>();

listaNomes.add("Vinicius");
listaNomes.add("Vinícius");
listaNomes.add("Âron");
listaNomes.add("Bola");
listaNomes.add("Carvalho");
listaNomes.add("Márcio");
listaNomes.add("Marcelo");

Collections.sort(listaNomes, Collator.getInstance());

for (String nome : listaNomes) {
   System.out.println(nome);
}

See working on ideone.

Then, to popular the JCombobox, just add using a lace:

for (String nome : listaNomes) {
   nomes.addItem(nome);
}

There are other ways to do that too, like converting into Array and passing directly on startup or even creating a Custom combomodel, but it is your choice which option best meets your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.