0

Please have a look at following code snippet,

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;
}

and this;

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);
    }
}

Now, when I am adding data to table, it is showing vertical scrollbar when there is a need, but it isn't showing horizontal one, instead it is cropping strings in last column. I am not getting what is going wrong here..

and this is what it looks like,

enter image description here

Rupesh
  • 3,415
  • 2
  • 26
  • 30
  • 2
    maybe is needed to override JScrollBars policy, maybe something to block (e.g. PreferredSize & used LayoutManager), for better help sooner post an SSCCE/MCVE, short, runnable, compilable with hardcoded value for JTable/XxxTableModel – mKorbel Jun 02 '14 at 10:36
  • 2
    Don't `setPreferredSize`; do override `getPreferredScrollableViewportSize`. – trashgod Jun 02 '14 at 10:41
  • Yeah, that duplicate is the answer I reckon! – Dan Temple Jun 02 '14 at 10:41
  • the solution to question in first comment by @CanadianDavid is not working for me.. – Rupesh Jun 02 '14 at 11:05
  • Override `getPreferredSize()` method of the `JTable`. override `getPreferredSize()` instead of `setPreferredSize()` method. – Braj Jun 02 '14 at 11:26
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Jun 02 '14 at 11:29
  • @AndrewThompson I have updated the description of question and also provided the complete src to reproduce the issue, please have a look. – Rupesh Jun 02 '14 at 12:30

1 Answers1

1

Add the below code after mTable.setRowHeight(35); in your FrameFoo class

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);
        }
Arijit
  • 1,633
  • 2
  • 21
  • 35
  • that's working.. thanks. could you please give me some links regarding what's going on here? – Rupesh Jun 02 '14 at 13:54
  • 1
    @Rupesh: The code is resizing the columns based on the size of the largest data element in the column. – Gilbert Le Blanc Jun 02 '14 at 14:52
  • 2
    @Rupesh, `could you please give me some links` - Maybe [Table Column Adjuster](http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/) will help. – camickr Jun 02 '14 at 15:58