不知何故水平滚动条没有出现在JTable中

时间:2014-06-02 10:33:52

标签: java swing jtable jscrollpane tablemodel

请查看以下代码段

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

1 个答案:

答案 0 :(得分:1)

mTable.setRowHeight(35);课程

FrameFoo之后添加以下代码
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);
        }