Align the column in a List

From CodeCodex

Implementations[edit]

Java[edit]

The trick is to use a FIXED width character set like "Courier".
ml = new List();
my.setFont(new Font("Courier", Font.BOLD, 10));

When inserting a line, we simply pad spaces as needed. Here the first column is 20 characters wide, the second 10 and the last one the remaining.

insertItem(ml, "ARCHIVE", "STATUS", "PORT");
public void insertItem(List lbx, 
  String col1, String col2, String col3) {
   String spaces2 = "  ";
   String spaces10 = "          ";  
   String spaces20 = spaces10 + spaces10;
   lbx.addItem(col1 +
    spaces20.substring(0,20-col1.length()) +
    spaces2 +
    col2 +
    spaces10.substring(0,10-col2.length()) +
    col3);
}

In real life, the preferred way would be to extend the java.awt.List and override the addItem method.