import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
* Test if iPhone effect works also for google spreed api, so that developer can use that.
*
* @author Christian
*
*/
public class Sandbox extends JFrame{
JTable table = new JTable();
JScrollPane sp = null;
DefaultTableModel model = null;
int cnt = 0;
int y = -1;
long time1 = 0;
int ypos = 0;
Thread t = null;
static int velocity;
public Sandbox(){
this.setSize(800, 600);
this.setLayout(new BorderLayout());
this.addWindowListener(new WindowAdapter(){@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}});
model = new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column) {
return false;
}};
table.setRowSelectionAllowed(false);
String[] columnNames = {
"Testspalte 1", "Testspalte 2", "Testspalte 3", "Testspalte 4"
};
model.setColumnIdentifiers(columnNames);
table.setModel(model);
fillTable();
table.addMouseListener(new MouseAdapter(){@Override
public void mouseClicked(MouseEvent arg0) {
if(arg0.getClickCount()==2){
JOptionPane.showMessageDialog(null, "Row click!\n\nRownum: "+table.rowAtPoint( arg0.getPoint())+"\n\nValue of first cell: "+model.getValueAt(table.rowAtPoint( arg0.getPoint()),0),"Information", JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
public void mouseReleased(final MouseEvent e) {
int tmp = 0;
tmp = (ypos-e.getY());
if(tmp<0)
tmp = (e.getY()-ypos);
if(tmp>10){
/**
* Geschwindigkeit = ((Weg * Konstante (120)) / Zeit)
*/
velocity = (tmp*120)/((int)(System.currentTimeMillis()-time1));
t = new Thread(){
long l = System.currentTimeMillis();;
int direction = -1;//Up or down...
public void run(){
int steps = 10;
if((ypos-e.getY())<0){
direction = 0;//up
}else
direction = 1; //down
if(velocity>100)//if your finger (or mouse) was to quick, give em more steps to work with
steps = 18;
while(!Thread.currentThread().isInterrupted()){
try {
if((System.currentTimeMillis()-l)>velocity){
l = System.currentTimeMillis();
Thread.sleep(5);
}
velocity-=1;
if(steps>0 && velocity%25==0){
steps-=1;
}
scrollTable(cnt);
if(direction==0){
if(cnt>0)
cnt-=steps;
}else{
cnt+=steps;
}
if(steps == 0)
return;
} catch (InterruptedException e2) {
Thread.currentThread().interrupt();
};
}
}
};
t.setPriority(Thread.MIN_PRIORITY);
t.start();
}
}
@Override
public void mousePressed(MouseEvent arg0) {
if(t!=null)
t.interrupt();
y = -1;
ypos = arg0.getY();
time1 = System.currentTimeMillis();
}}
);
table.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
if(y==-1){
cnt++;
y = e.getY();
}else{
if(y < e.getY()){
if(cnt>0)
cnt-=5;
}else
cnt+=5;
}
scrollTable(cnt);
}});
sp = new JScrollPane(table);
sp.setDoubleBuffered(true);
table.setDoubleBuffered(true);
this.add(sp, BorderLayout.CENTER);
}
private void fillTable(){
for(int i=0;i<10000;i++){
model.addRow(new String[]{"Entry "+i,"Entry "+i, "Entry "+i, "Entry "+i});
}
}
private void scrollTable(int value){
try{
sp.getVerticalScrollBar().setValue(value);
}catch(Exception e){
//Bugfix
}
}
public static void main(String[] args){
new Sandbox().setVisible(true);
}
}