[ Back | Previous | Next ]

How let Jtable scroll on the column rather than pixels?

Package:
javax.swing.*
Product:
Swing
Release:
1.0.3
Related Links:
ComboBox
JDialog
JFileChooser
JFrame
JOptionPane
JProgressBar
JScrollPane
JTable
General
JTree
JWindow
KeyStroke
LayeredPane
UIDefaults
Comment:
package nl.rotterdam.port.swing;

import java.awt.event.*;
import java.awt.*;
//
import com.sun.java.swing.*;

/**
 * This type was created by A. Kaan
 * GHR (c) 2000
 */
public class JUnitTable extends com.sun.java.swing.JTable {
	private int maxUnitIncrement = 0;
/**
 * JUnitTable constructor comment.
 */
public JUnitTable() {
	super();
}
/**
 * JUnitTable constructor comment.
 * @param rowData java.lang.Object[][]
 * @param columnNames java.lang.Object[]
 */
public JUnitTable(java.lang.Object[][] rowData, java.lang.Object[] columnNames) {
	super(rowData, columnNames);
}
/**
 * JUnitTable constructor comment.
 * @param numRows int
 * @param numColumns int
 */
public JUnitTable(int numRows, int numColumns) {
	super(numRows, numColumns);
}
/**
 * JUnitTable constructor comment.
 * @param dm com.sun.java.swing.table.TableModel
 */
public JUnitTable(com.sun.java.swing.table.TableModel dm) {
	super(dm);
}
/**
 * JUnitTable constructor comment.
 * @param dm com.sun.java.swing.table.TableModel
 * @param cm com.sun.java.swing.table.TableColumnModel
 */
public JUnitTable(com.sun.java.swing.table.TableModel dm, com.sun.java.swing.table.TableColumnModel cm) {
	super(dm, cm);
}
/**
 * JUnitTable constructor comment.
 * @param dm com.sun.java.swing.table.TableModel
 * @param cm com.sun.java.swing.table.TableColumnModel
 * @param sm com.sun.java.swing.ListSelectionModel
 */
public JUnitTable(com.sun.java.swing.table.TableModel dm, com.sun.java.swing.table.TableColumnModel cm, com.sun.java.swing.ListSelectionModel sm) {
	super(dm, cm, sm);
}
/**
 * JUnitTable constructor comment.
 * @param rowData java.util.Vector
 * @param columnNames java.util.Vector
 */
public JUnitTable(java.util.Vector rowData, java.util.Vector columnNames) {
	super(rowData, columnNames);
}
/**
 * This method was created in VisualAge.
 * @param str java.lang.String
 */
private static void dbg(String str) {
	System.out.println("JUnitTable: " + str );
}
/**
 * This method was created in VisualAge.
 * @return int
 */
public int getMaxUnitIncrement() {
	if (getModel() == null)
		return 0;
	int cols = getModel().getColumnCount();
	if ( cols > 0) {
		cols--;
	}
	double unit = getPreferredSize().width / cols;
	dbg("unit: " + unit);
	return (int) unit;
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
	if (orientation == SwingConstants.HORIZONTAL)
		return visibleRect.width - getMaxUnitIncrement();
	else
		return visibleRect.height - getMaxUnitIncrement();
}
	public boolean getScrollableTracksViewportHeight() {
		return false;
	}
	public boolean getScrollableTracksViewportWidth() {
		return false;
	}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
	//Get the current position.
	int currentPosition = 0;
	if (orientation == SwingConstants.HORIZONTAL)
		currentPosition = visibleRect.x;
	else
		currentPosition = visibleRect.y;

	//Return the number of pixels between currentPosition
	//and the nearest tick mark in the indicated direction.
	if (direction < 0) {
		int newPosition = currentPosition - (currentPosition / getMaxUnitIncrement()) * getMaxUnitIncrement();
		return (newPosition == 0) ? getMaxUnitIncrement() : newPosition;
	} else {
		return ((currentPosition / getMaxUnitIncrement()) + 1) * getMaxUnitIncrement() - currentPosition;
	}
}
	public void setMaxUnitIncrement(int pixels) {
		maxUnitIncrement = pixels;
	}
}
1