Wednesday, March 2, 2011

Text Placement in a Java JList

This is just for future reference so I don't have to dig around for the answer.

private static final int LIST_LEFT_MARGIN = 5;

jList1.setCellRenderer(new DefaultListCellRenderer() {
  public Component getListCellRendererComponent(JList list, Object value,
    int index, boolean isSelected, boolean cellHasFocus) {
      super.getListCellRendererComponent(list, value,
                                         index, isSelected, cellHasFocus);
      this.setBorder(BorderFactory.createEmptyBorder(0, LIST_LEFT_MARGIN, 0, 0));
      return this;
    }
  }
}

No comments:

Post a Comment