Link to home
Create AccountLog in
Avatar of perdoname_
perdoname_

asked on

Sort jlist ascending

Hello Experts,

I need your assistance how can i use the class StringSorter as to sort correctly the jlist.

Thanks in advance !!
public class ListModel extends JFrame implements ActionListener {
 
    JList list;
 
    DefaultListModel model;
 
    private JMenu fileMenu = new JMenu("File");
    private JMenuBar menuBar = new JMenuBar();
    private JMenuItem saveItem = new JMenuItem("Save");
    private JMenuItem openItem = new JMenuItem("Load");
    private JMenuItem exitItem = new JMenuItem("Exit");
    private String filename = null;
 
    int counter = 100;
 
    public ListModel() {
        super("List Model");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.add(exitItem);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        exitItem.addActionListener(this);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
        setLayout(new BorderLayout());
        model = new DefaultListModel();
        final JList list = new JList(model);
        JScrollPane pane = new JScrollPane(list);
        final JButton addButton = new JButton("Add Element");
        JButton removeButton = new JButton("Remove Element");
 
 
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String element = JOptionPane.showInputDialog(this);
                if (element == null) {
                    return;
                } else {
                    model.addElement(element);
                }
 
            }
        });
     
 
      
        removeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	int selectedIndices[] = list.getSelectedIndices();
                for (int i = 0; i < selectedIndices.length; i++)
                {
              	  model.removeElementAt(selectedIndices[i] - i);
                }
            }
        });
       
        JPanel pnlButtons = new JPanel() ;
        pnlButtons.add( addButton ) ;
        pnlButtons.add( removeButton ) ;
 
        add( new RadioButton1(), BorderLayout.NORTH );
        add( pane, BorderLayout.CENTER );
        add( pnlButtons, BorderLayout.SOUTH );
        pack() ;
        setVisible(true);
    }
 
    
 
	public void actionPerformed(ActionEvent e) {
        if (e.getSource() == openItem)
            try {
                loadFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        else if (e.getSource() == exitItem)
            System.exit(0);
    }
  
    
    private DefaultListModel loadFile() throws IOException {
        JFileChooser fc = new JFileChooser();
        String name = null;
        if (fc.showOpenDialog(null) != JFileChooser.CANCEL_OPTION)
            name = fc.getSelectedFile().getAbsolutePath();
        else
            return model;
        FileInputStream stream = new FileInputStream(name);
        InputStreamReader reader = new InputStreamReader(stream);
        BufferedReader buffer = new BufferedReader(reader);
        while (true)
        {
            String line = buffer.readLine();
            if (line == null)
            {
            	break;
            }
            model.addElement(line);
        }
		return model;
    }
 
    public static void main(String s[]) {
        new ListModel();
    }
 
    public ListModel getModel() {
        return null;
    }
 
    public String getElementAt(int i) {
        return null;
    }
  }
}//end of class
 
 
 
class StringSorter {
	
	 void sortList(JList list)
 {
   ListModel model = list.getModel();
   int numItems = model.getSize();
   String[] a = new String[numItems];
   for (int i=0;i<numItems;i++)
   {
     a[i] = (String)model.getElementAt(i);
   }
   sortArray(a);
   list.setListData(a);
   list.revalidate();
 }
 void sortArray(String[] strArray)
 {
   if (strArray.length == 1)    // no need to sort one item
     return;
   Collator collator = Collator.getInstance();
   String strTemp;
   for (int i=0;i<strArray.length;i++)
   {
     for (int j=i+1;j<strArray.length;j++)
     {
       if (collator.compare(strArray[i], strArray[j]) > 0)
       {
         strTemp = strArray[i];
         strArray[i] = strArray[j];
         strArray[j] = strTemp;
       }
     }
   }
 }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of favoretti
favoretti
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account