0

I have 12 labels and 12 car objects in an ArrayList. The for loop sets the for each label and its corresponding car object. However the text in the labels does not wrap and just carries on.

What do I have to put in the loop to make the text wrap?

In theory the text is supposed to look like this:

2012 Toyota Corolla

70000 Miles

$12,000.00

But instead in the labels all I get in a single line is:

2012 Toyota Corolla 700...


Code

    InventoryFileReader reader = new InventoryFileReader();
    ArrayList<Car> cars = reader.getAllCars();

    //will format getPrice to currency.
    Locale locale = new Locale("en", "US");
    NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);

    JLabel[] labels = new JLabel[]{jLabel1, jLabel2, jLabel3, jLabel4,
                                    jLabel5, jLabel6, jLabel7, jLabel8,
                                    jLabel9, jLabel10, jLabel11, jLabel12};

    for(int i = 0; labels.length > i; i ++){

        labels[i].setText(cars.get(i).getYear() + " " + cars.get(i).getMake() + 
                " " + cars.get(i).getModel() + " " + "\n" + cars.get(i).getMiles() 
                + " miles" + '\n' + " " +  formatter.format(cars.get(i).getPrice()));
    }

The getYear(), getMiles(), and getPrice(), actually return integer values.

  • *"What do I have to put in the loop to make the text wrap?"* - wrap the text in HTML – MadProgrammer May 21 '18 at 00:21
  • Possible duplicate [JLabel - Show longer text as multiple lines?](https://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines/14738193#14738193) and/or [How to format JLabel/JTextField so text wraps](https://stackoverflow.com/questions/26747610/how-to-format-jlabel-jtextfield-so-text-wraps/26747718#26747718) – MadProgrammer May 21 '18 at 00:22
  • Create a JPanel with 3 labels displayed vertically. Assign each line of text to the appropriate label. – camickr May 21 '18 at 00:26
  • Another option is to use a non-editable `JTextArea` – MadProgrammer May 21 '18 at 00:38

0 Answers0