Here is the code to reproduce the problem.
To see the problem do a resize by mouse dragging enlargeing the row height, then click on the button to resize the row, and finally click on a cell of the table.
you will see the table that autoresize all the row to the last height decided by mouse dragging.
- Code: Select all
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.net.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import com.jidesoft.grid.*;
import com.jidesoft.swing.JideSwingUtilities;
public class JideTableThumbnail extends JideTable{
protected CellResizeListener listener;
protected boolean variousRowHeight;
public JideTableThumbnail() {
listener = new CellResizeListener(this);
ThumbnailTableModel tm = new ThumbnailTableModel(listener);
this.setModel(tm);
this.setColumnModel(tm);
getTableHeader().setVisible(true);
setNonContiguousCellSelection(true);
setAutoCreateColumnsFromModel(false);
setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
setColumnResizable(true);
setRowResizable(true);
setRowHeight(50);
setVariousRowHeights(false);
}
public CellResizeListener getListener() {
return listener;
}
public static void main(String[] args) {
JFrame f = new JFrame("test");
final JideTableThumbnail t = new JideTableThumbnail();
JScrollPane sp = new JScrollPane(t);
sp.addComponentListener(t.getListener());
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(sp);
JButton b = new JButton("setRowHeight = 33");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
t.setRowHeight(33);
}});
f.getContentPane().add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
System.out.println(f.getSize());
JideSwingUtilities.globalCenterWindow(f);
f.setVisible(true);
}
public Vector<TableColumn> getColumns() {
ThumbnailTableModel ttm = getThumbnailTableModel();
if(ttm != null)
return ttm.getColumnsVector();
return null;
}
public void updateColumnCount(int newColumnWidth) {
int tableWidth = getWidth();
if (getParent() instanceof JViewport) {
JViewport v = (JViewport) getParent();
tableWidth = v.getWidth();
}
int columnCount = tableWidth/newColumnWidth;
if(columnCount == this.getColumnModel().getColumnCount())
return;
if(columnCount == 0)
return;
if(columnCount > this.getColumnModel().getColumnCount()){
for (int i = this.getColumnModel().getColumnCount(); i < columnCount; i++) {
this.getColumnModel().addColumn(((ThumbnailTableModel)this.getColumnModel()).buildColumn(i));
}
}else if(columnCount < this.getColumnModel().getColumnCount()){
for (int i = this.getColumnModel().getColumnCount() -1; i >= columnCount; i--) {
this.getColumnModel().removeColumn(this.getColumnModel().getColumn(i));
}
}
ThumbnailTableModel ttm = getThumbnailTableModel();
if(ttm != null){
ttm.fireTableChanged(new TableModelEvent(ttm, TableModelEvent.HEADER_ROW));
ttm.fireTableChanged(new TableModelEvent(ttm));
}
}
public ThumbnailTableModel getThumbnailTableModel(){
ThumbnailTableModel ttm = null;
TableModel tm = getModel();
if (tm instanceof SortableTableModel) {
tm = ((SortableTableModel) tm).getActualModel();
}
if (tm instanceof ThumbnailTableModel) {
ttm = (ThumbnailTableModel) tm;
}return ttm;
}
class ThumbnailTableModel extends DefaultTableColumnModel implements TableModel{
protected Vector<String> rows;
protected Vector<TableModelListener> listeners;
protected CellResizeListener resizeListener;
public ThumbnailTableModel(CellResizeListener resizeListener) {
this.resizeListener = resizeListener;
tableColumns.clear();
tableColumns.add(buildColumn(0));
tableColumns.add(buildColumn(1));
tableColumns.add(buildColumn(2));
tableColumns.add(buildColumn(3));
rows = new Vector<String>(12);
rows.add("http://www.jidesoft.com/imgs/pht_s2.jpg");
rows.add("http://www.jidesoft.com/imgs/125x125-JDJ.jpg");
rows.add("http://www.jidesoft.com/imgs/pht_1.jpg");
rows.add("http://www.jidesoft.com/imgs/pht_s6.jpg");
rows.add("http://www.libero.it/img41/col3/top_box1/03/325/2007/5/banner_10.jpg");
rows.add("http://www.libero.it/c/img69/hp/07/7/2007/6/cannavaro148.jpg");
rows.add("http://www.libero.it/x/img48/02/2716/2007/6/motori_s.jpg");
rows.add("http://www.libero.it/c/img69/hp/06/60/2007/6/dalemasei88.jpg");
rows.add("http://thumbs.ebaystatic.com/pict/2901307386548080_1.jpg");
rows.add("http://thumbs.ebaystatic.com/pict/2501365998908080_1.jpg");
rows.add("http://tbn0.google.com/images?q=tbn:mVuW5icfsm2ZzM:http://www.flensburg-online.de/comics/pluto.gif");
rows.add("http://www.flensburg-online.de/comics/pluto2.gif");
}
public Vector<TableColumn> getColumnsVector(){
return this.tableColumns;
}
private TableColumn buildColumn(int index) {
TableColumn c = new TableColumn();
int preferredWidth = (tableColumns!= null && tableColumns.size() > 0)?tableColumns.get(0).getPreferredWidth():160;
c.setPreferredWidth(preferredWidth);
c.setModelIndex(index);
c.addPropertyChangeListener(resizeListener);
c.setCellRenderer(new MyTableCellRenderer());
return c;
}
@Override
public int getColumnCount() {
return tableColumns.size();
}
public void addTableModelListener(TableModelListener l) {
if(listeners == null)
listeners = new Vector<TableModelListener>();
listeners.add(l);
}
public void removeTableModelListener(TableModelListener l) {
if(listeners != null)
listeners.remove(l);
}
public Class<?> getColumnClass(int columnIndex) {
return Object.class;
}
public String getColumnName(int columnIndex) {
return (columnIndex>0 && columnIndex < tableColumns.size())?tableColumns.get(columnIndex).toString():null;
}
public int getRowCount() {
return rows.size()/getColumnCount()+(rows.size()%getColumnCount()==0?0:1);
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(rowIndex*getColumnCount()+columnIndex < rows.size())
return rows.get(rowIndex*getColumnCount()+columnIndex);
return null;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {}
public void fireTableChanged(TableModelEvent e) {
for (int i = listeners.size() - 1; i >= 0; i--) {
listeners.get(i).tableChanged(e);
}
}
}
class CellResizeListener extends ComponentAdapter implements PropertyChangeListener{
protected JideTableThumbnail thumbTable;
public CellResizeListener(JideTableThumbnail thumbTable) {
this.thumbTable = thumbTable;
}
public void propertyChange(PropertyChangeEvent evt) {
Vector<TableColumn> columns = this.thumbTable.getColumns();
if(columns == null)
return;
int updatedWidth = -1;
if(evt.getPropertyName().equals("width") ){
updatedWidth = (Integer)evt.getNewValue();
for (int i = 0; i < columns.size(); i++) {
TableColumn col = columns.get(i);
if(col.getWidth() != updatedWidth){
col.setWidth(updatedWidth);
}
}
}else if(evt.getPropertyName().equals("preferredWidth")){
updatedWidth = (Integer)evt.getNewValue();
for (int i = 0; i < columns.size(); i++) {
TableColumn col = columns.get(i);
if(col.getPreferredWidth() != updatedWidth){
col.setPreferredWidth(updatedWidth);
}
}
}
ThumbnailTableModel ttm = thumbTable.getThumbnailTableModel();
if(updatedWidth > 0)
thumbTable.updateColumnCount(updatedWidth);
}
public void componentResized(ComponentEvent e) {
Vector<TableColumn> columns = this.thumbTable.getColumns();
if(columns == null || columns.size()<1)
return;
updateColumnCount(columns.get(0).getWidth());
}
}
class MyTableCellRenderer extends DefaultTableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component argr = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
try {
if(value != null)
setIcon(new ImageIcon(new URL(value.toString())));
else
setIcon(null);
} catch (MalformedURLException e) {
e.printStackTrace();
}
setText("");
return argr;
}
}
}