Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting an ArrayList in Java
    text
    copied!<p>So I'm having trouble figuring out how to update a TextArea with information that I submit from an generics arraylist. As of now the program creates a new Order:</p> <pre><code>Order d1 = new Order(); </code></pre> <p>Then the user selects some data and pushes an add button, and the order is added to a TextArea. The problem I have is that I have to add the order to the correct spot in the list and update it each time. I"m only sorting it by one item. I'm not really sure how to do that using the CompareTo method.</p> <pre><code> public void actionPerformed(ActionEvent event) { ArrayList&lt;Drink&gt; DrinkArray = new ArrayList&lt;Drink&gt;(); if (event.getSource() == addcoffeeButton) { String coffeesize = (String) sizecoffeelist.getSelectedItem(); double coffeeprice = Double.parseDouble(pricecoffeeTextfield.getText()); String coffeetype = (String) cuptypecoffeelist.getSelectedItem(); String coffeecaffeine = (String) caffeineList.getSelectedItem(); String coffeeroom = (String) roomforcreamList.getSelectedItem(); String coffeeadditional = additionalflavorList.getText(); if ((coffeeadditional.isEmpty())) coffeeadditional = "No Additional Flavor"; Drink d1 = new Coffee(coffeesize, coffeeprice, coffeetype, coffeecaffeine, coffeeroom, coffeeadditional); DrinkArray.add(d1); orderTextArea.append(d1); </code></pre> <p>So I would have to add the drink to the correct spot before adding it to the array and printing to the text area, but I'm not quite sure how to do that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload