gpt4 book ai didi

java - 不知何故,JTable 中没有出现水平滚动条

转载 作者:行者123 更新时间:2023-12-01 12:54:38 27 4
gpt4 key购买 nike

请查看以下代码片段,

public class FrameFoo extends JFrame {
private TableAdapter mAdapter;

public FrameFoo() {
HashMap<Integer, String> m = new HashMap<Integer, String>();

m.put(1, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
m.put(2, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
m.put(3, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
m.put(4, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
m.put(5, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
m.put(6, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
m.put(7, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");

mAdapter = new TableAdapter(m);

initComponents();
}

private void initComponents() {

mScrollPane = JScrollPane();
mTable = new JTable();

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(400, 200));
setResizable(false);

mTable.setModel(mAdapter);
mTable.setRowHeight(35);
mScrollPane.setViewportView(mTable);

getContentPane().add(mScrollPane, BorderLayout.CENTER);

pack();
}

public static void main(String args[]) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}

EventQueue.invokeLater(new Runnable() {

public void run() {
new FrameFoo().setVisible(true);
}
});
}

private JScrollPane mScrollPane;
private JTable mTable;
}

还有这个;

public class TableAdapter extends AbstractTableModel {

private Map<Integer, String> m;

/*
* Assume no changes (add/remove) to m will be made after initialization.
*/
public TableAdapter(Map<Integer, String> m) {
this.m = m;
}

public void notifyDataSetChanged() {
fireTableDataChanged();
}

@Override
public int getRowCount() {
return m.size();
}

@Override
public int getColumnCount() {
return 2;
}

@Override
public String getValueAt(int rowIndex, int columnIndex) {

if (columnIndex < 0 || columnIndex > 1) {
throw new IllegalArgumentException();
}

return columnIndex == 0 ? rowIndex + 1 + "" : m.get(rowIndex + 1);
}
}

现在,当我向表中添加数据时,它会在需要时显示垂直滚动条,但它不会显示水平滚动条,而是裁剪最后一列中的字符串。我不明白这里出了什么问题..

这就是它的样子,

enter image description here

最佳答案

FrameFoo 类中的 mTable.setRowHeight(35); 之后添加以下代码

mTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

final TableColumnModel columnModel = mTable.getColumnModel();
for (int column = 0; column < mTable.getColumnCount(); column++) {
int width = 50; // Min width
for (int row = 0; row < mTable.getRowCount(); row++) {
TableCellRenderer renderer = mTable.getCellRenderer(row, column);
Component comp = mTable.prepareRenderer(renderer, row, column);
width = Math.max(comp.getPreferredSize().width, width);
}
columnModel.getColumn(column).setPreferredWidth(width);
}

关于java - 不知何故,JTable 中没有出现水平滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com