Arrange accented words in a list

1

I wonder if you can organize accented words within JComboBox . So far I have managed to organize the words without an accent, but the accented ones do not.

Follow 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
    
asked by anonymous 11.10.2016 / 02:24

1 answer

1

One way is to add all names to a list and use a 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 at ideone .

Then, to JCombobox popular, just add using a loop:

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

There are other ways to do this too, such as converting to Array and passing directly on startup from the combo or even creating a ComboModel custom , but you choose which option best suits your code.

    
11.10.2016 / 03:00