如何在String.format中均匀地空格键,这是JList中的DefaultListModel?

时间:2016-12-14 12:10:00

标签: java string jlist defaultlistmodel

我已经在这里问了,所以请在这里阅读。这个问题部分已解决。 How can I add \t spacing in a String that belongs to a DefaultListModel which belongs to a JLIst?

现在我有另一个问题:

请先查看此示例

// Declarations
String string1 = "Eggs";
String string2 = "Whole Chicken";
int quantity1 = 100;
int quantity2 = 25;

// HTML and PRE tags are added before and after string
StringBuilder builder1 = new Stringbuilder;
builder.append("<html><pre>");
builder.append(String.format("%s \t %d", string1, quantity1));
builder.append("</pre></html>");

StringBuilder builder2 = new Stringbuilder;
builder.append("<html><pre>");
builder.append(String.format("%s \t %d", string2, quantity2));
builder.append("</pre></html>");

// JList is defined, and the builder is added to the list.
JList<String> list = new JList<String>();
list.addElement(builder1.toString());
list.addElement(builder2.toString());

这是显示所有项目的方式

Eggs    100
Whole Chicken    25

我想要发生的是,所有数量变量都是对齐的,而不是基于前一个变量的间距。任何想法我怎么能做到这一点?

谢谢!

==========================

另一个建议是,如果可以在String.format()中定义字符串的最小长度吗?

3 个答案:

答案 0 :(得分:0)

您只需使用

即可
%20s
例如

- 将20个空格放入格式化的输出中。

所以诀窍基本上是首先扫描列表中的所有字符串,然后计算格式化模式中所需的合理数字。换句话说:您首先要确定要显示的字符串的最大长度;然后计算适当的缩进比例。

编辑:影响格式的方式有很多,请参阅此questionenter link description here,或者最好,研究指定所有格式模式的javadoc

答案 1 :(得分:0)

正如@GhostCat所说,使用格式字符串中的宽度将解决您的问题。

有关详细信息,请查看格式字符串的documentation

但是,您尝试做的事情让我想到,可能使用JTable而不是使用JList?

$(".button-0").append($("<div></div>").addClass("button-0-text").text($(this).parent().attr("data-text")));

答案 2 :(得分:0)

最简单的解决方案是使用等宽字体和字符串格式。

public class Main {

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(320, 100);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();

    DefaultListModel<String> defaultListModel = new DefaultListModel<>();
    defaultListModel.addElement(String.format("%-20s %d", "Eggs", 100));
    defaultListModel.addElement(String.format("%-20s %d", "Whole Chicken", 25));

    JList<String> jList = new JList<>(defaultListModel);
    Font defaultListFont = jList.getFont();
    jList.setFont(new Font("monospaced", defaultListFont.getStyle(), defaultListFont.getSize()));

    contentPane.add(jList);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

将导致

With monospaced font

不使用等宽字体的解决方案

如果您想根据ListModel调整列宽,我会收听ListModel更改,以便计算模型中最长的字符串并使用自定义渲染器。

public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.setSize(320, 150);
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

  Container contentPane = frame.getContentPane();

  DefaultListModel<String[]> defaultListModel = new DefaultListModel<>();
  defaultListModel.addElement(new String[]{"Eggs", "100"});
  defaultListModel.addElement(new String[]{"The longest Entry in the list", "12125"});
  defaultListModel.addElement(new String[]{"Whole Chicken", "25"});

  JList<String[]> jList = new JList<>(defaultListModel);

  ListModelAwareColumnWidthProvider listModelAwareColumnWidthProvider = new ListModelAwareColumnWidthProvider();
  listModelAwareColumnWidthProvider.setListModel(defaultListModel);

  ColumnCellRenderer columnCellRenderer = new ColumnCellRenderer(listModelAwareColumnWidthProvider);
  jList.setCellRenderer(columnCellRenderer);

  contentPane.add(jList);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
}

你必须实现类似的东西

支持列

的渲染器
class ColumnCellRenderer implements ListCellRenderer<String[]> {

  private ColumnWidthProvider columnWidthProvider;

  public ColumnCellRenderer(ColumnWidthProvider columnWidthProvider) {
    this.columnWidthProvider = columnWidthProvider;
  }


  @Override
  public Component getListCellRendererComponent(JList<? extends String[]> list, String[] value, int index, boolean isSelected, boolean cellHasFocus) {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    for (int i = 0; i < value.length; i++) {
      String valueElement = value[i];

      JLabel label = new JLabel(valueElement);

      align(label, i);
      panel.add(label);
    }

    applyColors(list, panel, isSelected);

    return panel;
  }

  private void applyColors(JList<?> list, JComponent component, boolean isSelected) {
    if (isSelected) {
      component.setForeground(list.getSelectionForeground());
      component.setBackground(list.getSelectionBackground());
    } else {
      component.setForeground(list.getForeground());
      component.setBackground(list.getBackground());
    }
  }

  private void align(JComponent component, int columnIndex) {
    Font font = component.getFont();
    FontMetrics fontMetrics = component.getFontMetrics(font);
    int fontHeight = fontMetrics.getHeight();

    int columnWidth = columnWidthProvider.getColumnWidth(fontMetrics, columnIndex);
    component.setPreferredSize(new Dimension(columnWidth, fontHeight));
  }

}

渲染器获取列宽

的界面
interface ColumnWidthProvider {
  public int getColumnWidth(FontMetrics fontMetrics, int columnIndex);
}

以及观察ListModel内容更改的ColumnWidthProvider的实现

class ListModelAwareColumnWidthProvider implements ColumnWidthProvider {

  private class ListModelChangeAdapter implements ListDataListener {
    @Override
    public void intervalAdded(ListDataEvent e) {
      initializeColumnWidths();
    }

    @Override
    public void intervalRemoved(ListDataEvent e) {
      initializeColumnWidths();
    }

    @Override
    public void contentsChanged(ListDataEvent e) {
      initializeColumnWidths();
    }
  }

  private int defaultColumnWidth = 100;

  private ListModelChangeAdapter listModelChangeAdapter = new ListModelChangeAdapter();
  private ListModel<String[]> listModel;
  private java.util.List<String> longestColumnStrings = new ArrayList<>();

  public void setListModel(ListModel<String[]> listModel) {
    if (this.listModel != null) {
      this.listModel.removeListDataListener(listModelChangeAdapter);
    }

    this.listModel = listModel;
    initializeColumnWidths();

    if (this.listModel != null) {
      this.listModel.addListDataListener(listModelChangeAdapter);
    }
  }

  private void initializeColumnWidths() {
    longestColumnStrings.clear();

    if (listModel != null) {
      int size = listModel.getSize();
      for (int i = 0; i < size; i++) {
        String[] elementAt = listModel.getElementAt(i);

        for (int columnIndex = 0; columnIndex < elementAt.length; columnIndex++) {
          String columnValue = elementAt[columnIndex];

          while (columnIndex >= longestColumnStrings.size()) {
            longestColumnStrings.add(null);
          }

          String lastLongestColumnString = longestColumnStrings.get(columnIndex);
          if (lastLongestColumnString == null) {
            longestColumnStrings.set(columnIndex, columnValue);
          } else if (columnValue.length() > lastLongestColumnString.length()) {
            longestColumnStrings.set(columnIndex, columnValue);
          }
        }
      }
    }
  }

  public void setDefaultColumnWidth(int defaultColumnWidth) {
    this.defaultColumnWidth = defaultColumnWidth;
  }

  @Override
  public int getColumnWidth(FontMetrics fontMetrics, int columnIndex) {
    if (columnIndex < longestColumnStrings.size()) {
      String longestColumnString = longestColumnStrings.get(columnIndex);
      return fontMetrics.stringWidth(longestColumnString);
    }
    return defaultColumnWidth;
  }
}

将导致

With renderer