的最簡單的解決方案是使用等寬字體和字符串格式。
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);
}
}
這將導致
一個解決方案,而無需使用等寬字體
如果要對齊取決於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);
}
渲染器和ColumnWidthProvider的實現觀察ListModel
以con 10噸改變
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;
}
}
這將導致
直接從教程中獲取的示例。只需要更多的工作並創建一個自定義的'TableModel',你就可以做更多的事情。 – bracco23