有没有像excel那样的电子表格swt控件吗?
解决方案 »
- HTTP 1.1 PIPELINE 问题
- String与byte[]
- 问一个写文件的问题
- 怎样将一个byte数组转换成中文字符串?
- hashmap,hashset,treeset,treemap那些需要实现comparator,那些需要实现hashcode()
- 新手选择,迷茫中,有经验的老大给出个主意,软件测试是一个好的选择吗?
- where can download jtest and crack
- 难题:这个配置文件的正则表达式该怎么写呢?
- 新手问题:我用java运行程序出错
- 不知道是java问题还是pb的问题
- J2SE,如何通过代理服务器请求一个URL?
- 请达人指教:如何用鼠标事件读出来的坐标取得JLabel上相应点上的元素
* Allows table columns to be resized not only using the header but from any
* rows. Based on the BasicTableHeaderUI.MouseInputHandler code.
*
* @author Santhosh Kumar T - [email protected]
*/
public class TableColumnResizer extends MouseInputAdapter{
public static Cursor resizeCursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
private int mouseXOffset;
private Cursor otherCursor = resizeCursor;
private JTable table;
public TableColumnResizer(JTable table){
this.table = table;
table.addMouseListener(this);
table.addMouseMotionListener(this);
}
private boolean canResize(TableColumn column){
return column != null
&& table.getTableHeader().getResizingAllowed()
&& column.getResizable();
}
private TableColumn getResizingColumn(Point p){
return getResizingColumn(p, table.columnAtPoint(p));
}
private TableColumn getResizingColumn(Point p, int column){
if(column == -1){
return null;
}
int row = table.rowAtPoint(p);
if(row==-1)
return null;
Rectangle r = table.getCellRect(row, column, true);
r.grow( -3, 0);
if(r.contains(p))
return null;
int midPoint = r.x + r.width / 2;
int columnIndex;
if(table.getTableHeader().getComponentOrientation().isLeftToRight())
columnIndex = (p.x < midPoint) ? column - 1 : column;
else
columnIndex = (p.x < midPoint) ? column : column - 1;
if(columnIndex == -1)
return null;
return table.getTableHeader().getColumnModel().getColumn(columnIndex);
}
public void mousePressed(MouseEvent e){
table.getTableHeader().setDraggedColumn(null);
table.getTableHeader().setResizingColumn(null);
table.getTableHeader().setDraggedDistance(0);
Point p = e.getPoint();
// First find which header cell was hit
int index = table.columnAtPoint(p);
if(index==-1)
return;
// The last 3 pixels + 3 pixels of next column are for resizing
TableColumn resizingColumn = getResizingColumn(p, index);
if(!canResize(resizingColumn))
return;
table.getTableHeader().setResizingColumn(resizingColumn);
if(table.getTableHeader().getComponentOrientation().isLeftToRight())
mouseXOffset = p.x - resizingColumn.getWidth();
else
mouseXOffset = p.x + resizingColumn.getWidth();
}
private void swapCursor(){
Cursor tmp = table.getCursor();
table.setCursor(otherCursor);
otherCursor = tmp;
}
public void mouseMoved(MouseEvent e){
if(canResize(getResizingColumn(e.getPoint()))
!= (table.getCursor() == resizeCursor)){
swapCursor();
}
}
public void mouseDragged(MouseEvent e){
int mouseX = e.getX();
TableColumn resizingColumn = table.getTableHeader().getResizingColumn();
boolean headerLeftToRight =
table.getTableHeader().getComponentOrientation().isLeftToRight();
if(resizingColumn != null){
int oldWidth = resizingColumn.getWidth();
int newWidth;
if(headerLeftToRight){
newWidth = mouseX - mouseXOffset;
} else{
newWidth = mouseXOffset - mouseX;
}
resizingColumn.setWidth(newWidth);
Container container;
if((table.getTableHeader().getParent() == null)
|| ((container = table.getTableHeader().getParent().getParent()) == null)
|| !(container instanceof JScrollPane)){
return;
}
if(!container.getComponentOrientation().isLeftToRight()
&& !headerLeftToRight){
if(table != null){
JViewport viewport = ((JScrollPane)container).getViewport();
int viewportWidth = viewport.getWidth();
int diff = newWidth - oldWidth;
int newHeaderWidth = table.getWidth() + diff;
/* Resize a table */
Dimension tableSize = table.getSize();
tableSize.width += diff;
table.setSize(tableSize);
/*
* If this table is in AUTO_RESIZE_OFF mode and has a horizontal
* scrollbar, we need to update a view's position.
*/
if((newHeaderWidth >= viewportWidth)
&& (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)){
Point p = viewport.getViewPosition();
p.x =
Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
viewport.setViewPosition(p);
/* Update the original X offset value. */
mouseXOffset += diff;
}
}
}
}
}
public void mouseReleased(MouseEvent e){
table.getTableHeader().setResizingColumn(null);
table.getTableHeader().setDraggedColumn(null);
}
}