作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请查看以下代码片段,
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);
}
}
现在,当我向表中添加数据时,它会在需要时显示垂直滚动条,但它不会显示水平滚动条,而是裁剪最后一列中的字符串。我不明白这里出了什么问题..
这就是它的样子,
最佳答案
在 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/
这个问题在这里已经有了答案: How to get all enum values in Java? (8 个回答) 关闭5年前。 我想创建一个 JComboBox 来处理选择给它的任何 Enum。为
我是一名优秀的程序员,十分优秀!