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,