需求:实现一个JTabbed, 当下拉到Tabbed的底部时,自动加载下一次的数据。

下面是具体代码:

 import java.awt.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import java.awt.event.*; public class JTabbedPaneExample extends JFrame implements AdjustmentListener
{
private GridBagLayout gridBagLayout = new GridBagLayout();
private JSplitPane splitPane = new JSplitPane();
private JPanel timePanel = new JPanel(); private GridBagLayout gridBagLayoutForTime = new GridBagLayout();
private JButton btnSearch = new JButton();
private HistoryStatisticsTableModel historyStatisticsTableModel = new HistoryStatisticsTableModel();
private JTable searchTable = new JTable(historyStatisticsTableModel);
private JScrollPane searchScrollPane = new JScrollPane(searchTable);
private JScrollBar scrollBarForVertical = new JScrollBar(JScrollBar.VERTICAL, 10, 10, 0, 100);
private JScrollBar scrollBarForHorizontal = new JScrollBar(JScrollBar.HORIZONTAL, 10, 10, 0, 100);
private int intPageNum = 1; public JTabbedPaneExample()
{
jbInit();
} private void jbInit()
{
this.setLayout(gridBagLayout);
searchTable.setEnabled(false);
splitPane.setLastDividerLocation(-1);
splitPane.setDividerLocation(150);
this.add(splitPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); timePanel.setLayout(gridBagLayoutForTime);
splitPane.add(timePanel, JSplitPane.RIGHT); btnSearch.setText("Search");
btnSearch.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
searchBtn_actionPerformed(e);
}
}); timePanel.add(btnSearch, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 50, 0, 0), 0, 0));
timePanel.add(searchScrollPane, new GridBagConstraints(0, 3, 5, 1, 1.0, 1.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 50, 50, 50), 0, 0));
scrollBarForVertical.setUnitIncrement(1);
scrollBarForVertical.setBlockIncrement(10);
scrollBarForVertical.setMaximum(90);
scrollBarForVertical.addAdjustmentListener(this);
searchScrollPane.setVerticalScrollBar(scrollBarForVertical);
searchScrollPane.setHorizontalScrollBar(scrollBarForHorizontal);
} public void adjustmentValueChanged(AdjustmentEvent e)
{
boolean isChange = false;
System.out.println("XXXXX bar Max : " + scrollBarForVertical.getMaximum());
System.out.println("XXXX bar Max Size : " + scrollBarForVertical.getMaximumSize());
System.out.println("XXXX bar pre size : " + scrollBarForVertical.getPreferredSize());
System.out.println("XXXX pane Max size : " + searchScrollPane.getMaximumSize());
System.out.println("XXXX pane size : " + searchScrollPane.getSize());
System.out.println("XXXX pane getHeight size : " + searchScrollPane.getHeight());
System.out.println("XXXX pane pre size : " + searchScrollPane.getPreferredSize());
System.out.println("XXXX intPageNum : " + intPageNum); if ((JScrollBar) e.getSource() == scrollBarForVertical)
{
long currentValue = e.getValue();
long diff = scrollBarForVertical.getMaximum() - searchScrollPane.getHeight();
System.out.println("XXXX currentValue = " + currentValue);
System.out.println("XXXX diff = " + diff);
if(currentValue >= diff && 0 != currentValue) //下拉到table底部,自动加载下一次数据功能
{
//System.out.println("XXXX scrollBarForVertical currentSize : " + currentSize);
System.out.println("XXXX Yes");
test();
intPageNum = intPageNum + 1;
isChange = true;
} if(isChange)
{
// scrollBarForVertical.setValue(0);
}
}
} public static void main(String[] args)
{
JTabbedPaneExample test = new JTabbedPaneExample();
test.setVisible(true);
} private void test()
{
String[] aStrCategoryName = new String[500];
String[] aStringCounterName = new String[500];
String[] aStrStartTime = new String[500];
String[] aStopTime = new String[500];
String[] aStrValue = new String[500];
int i = 0;
for(; i < 499; i++)
{
aStrCategoryName[i] = i + "";
aStringCounterName[i] = i + "";
aStrStartTime[i] = i + "";
aStopTime[i] = i + "";
aStrValue[i] = i + "";
} //historyStatisticsTableModel.createTable(aStrCategoryName, aStringCounterName, aStrStartTime, aStopTime, aStrValue);
historyStatisticsTableModel.createTable(aStrCategoryName, aStringCounterName, aStrStartTime, aStopTime, aStrValue);
//historyStatisticsTableModel.addRow(); } private void searchBtn_actionPerformed(ActionEvent e)
{
test();
} public class Parameter
{
public Parameter(String strCategoryName, String strCounterName, String strStartTime, String strStopTime, String strValue)
{
this.strCategoryName = strCategoryName;
this.strCounterName = strCounterName;
this.strStartTime = strStartTime;
this.strStopTime = strStopTime;
this.strValue = strValue;
} public String strCategoryName;
public String strCounterName;
public String strStartTime;
public String strStopTime;
public String strValue;
} private class HistoryStatisticsTableModel extends DefaultTableModel
{
private String[] COLUMN_NAMES = {"Category Name", "Counter Name", "Start Time", "Stop Time", "Value"};
public Parameter[] myparameters = new Parameter[0]; public HistoryStatisticsTableModel()
{
super();
super.setColumnIdentifiers(COLUMN_NAMES); super.setRowCount(0);
} public int getColumnCount()
{
return COLUMN_NAMES.length;
} /*
public int getRowCount()
{
return myparameters.length;
}
*/ public void setRowCount(int rowCount)
{
} public String getColumnName(int col)
{
return COLUMN_NAMES[col];
} public void addRow(String strCategoryName, String strCounterName, String strStartTime, String strStopTime, String strValue)
{
Object[] rowData = new Object[COLUMN_NAMES.length];
for (int i = 0; i < COLUMN_NAMES.length; i++)
{
switch(i)
{
//Category Name
case 0:
rowData[i] = strCategoryName;
break;
//Counter Name
case 1:
rowData[i] = strCounterName;
break;
//Start Time
case 2:
rowData[i] = strStartTime;
break;
//Stop Time
case 3:
rowData[i] = strStopTime;
break;
//Value
case 4:
rowData[i] = strValue;
break;
default:
rowData[i] = "";
}
} super.addRow(rowData);
}
/*
public Object getValueAt(int row, int col)
{
if (0 == col)
{
return myparameters[row].date;
}
else if (1 == col)
{
return myparameters[row].usage;
}
else if (2 == col)
{
return myparameters[row].ratio;
}
else
{
return null;
}
}
*/ /*
public void createTable(Document response)
{
NodeList list = response.getElementsByTagName("Parameter");
if (list.getLength() > 0)
{
Parameter[] aParameters = new Parameter[list.getLength()];
Element eparameter;
int acounter = 0;
for (int i = 0; i < list.getLength(); i++)
{
eparameter = (Element)list.item(i);
String aStrCategoryName = getElementValue(eparameter.getElementsByTagName("Category Name"));
String aStrCounterName = getElementValue(eparameter.getElementsByTagName("Counter Name"));
String aStrStartTime = getElementValue(eparameter.getElementsByTagName("Start Time"));
String aStrStopTime = getElementValue(eparameter.getElementsByTagName("Stop Time"));
String aStrValue = getElementValue(eparameter.getElementsByTagName("Value"));
aParameters[i] = new Parameter(aStrCategoryName, aStrCounterName, aStrStartTime, aStrStopTime, aStrValue);
}
myparameters = aParameters;
}
else
{
myparameters = new Parameter[0];
} fireTableDataChanged();
}
*/
// Wirte for test
public void createTable(String[] strCategoryName, String[] strCounterName, String[] strStartTime, String[] strStopTime, String[] strValue)
{
int intCounter = strCategoryName.length; for(int i = 0; i < intCounter; i++)
{
addRow(strCategoryName[i], strCounterName[i], strStartTime[i], strStopTime[i], strValue[i]);
} fireTableDataChanged();
}
}
}

由于只是一个小Demo, 所以没有怎么整理代码。各位看官就当看看了。

最核心的部分在79行。table 的最低值会根据table的大小变化的。 table 显示的越大。下拉到最低端的时候,值就越远离Max值,也就是会越小。经过观察数据,发现要得到自动加载数据的判断条件是79行。

我这个只是单纯的从数据层面去找个结果。Java的内部原理并没有去深入了解。各位有什么好办法可以提供下。

JTabbedPane 和 JScrollBar 联合使用的更多相关文章

  1. Dynamics CRM 之ADFS 使用 WID 的独立联合服务器

    ADFS 的使用 WID 的独立联合服务器适用于自己的测试环境,常用的就是在虚机中使用. 拓扑图如下: wID:联合身份验证服务配置为使用 Windows 内部数据库

  2. Dynamics CRM 之ADFS 使用 WID 的联合服务器场

    使用 WID 的联合服务器场 默认拓扑 Active Directory 联合身份验证服务 (AD FS) 是联合服务器场,使用 Windows 内部数据库 (WID). 在这种拓扑, AD FS 使 ...

  3. Hibernate(5)—— 联合主键 、一对一关联关系映射(xml和注解) 和 领域驱动设计

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及的知识点总结如下: One to One 映射关系 一对一单向外键(XML/Annotation) 一对一双向外键关联(XML/A ...

  4. Federated Identity Pattern 联合身份模式

    Delegate authentication to an external identity provider. This pattern can simplify development, min ...

  5. [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合

    [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合 Datasets can often contain components of that require differe ...

  6. SQL联合查询:子表任一记录与主表联合查询

    今天有网友群里提了这样一个关于SQL联合查询的需求: 一.有热心网友的方案: 二.我的方案: select * from ( select a.*,(select top 1 Id from B as ...

  7. Dynamics CRM 之ADFS 使用 SQL Server 的联合服务器场

    此拓扑用于 Active Directory 联合身份验证服务 (AD FS) 不同于使用 Windows 内部数据库 (WID) 部署拓扑,因为不会将数据复制到每台联合服务器场中的联合身份验证服务器 ...

  8. Dynamics CRM 之ADFS 使用 WID 和代理的联合服务器场

    为此部署拓扑 Active Directory 联合身份验证服务 (AD FS) 等同于联合服务器场与 Windows 内部数据库 (WID) 拓扑中,但它将代理服务器计算机添加到外围网络,以支持外部 ...

  9. Mysql联合,连接查询

    一. 联合查询    UNION, INTERSECT, EXCEPT UNION运算符可以将两个或两个以上Select语句的查询结果集合合并成一个结果集合显示,即执行联合查询.UNION的语法格式为 ...

随机推荐

  1. java certificate 工具 portecle.sourceforge.net

    https://sourceforge.net/projects/portecle/?source=directory 当需要处理java证书的时候这个是个好工具.省得敲命令了.

  2. 【Python】[面性对象编程] 获取对象信息,实例属性和类属性

    获取对象信息1.使用isinstance()判断class类型2.dir() 返回一个对象的所有属性和方法3.如果试图获取不存在的对象会抛出异常[AttributeError]4.正确利用对象内置函数 ...

  3. Go语言总结(图片打开略慢请知晓)

  4. Webpack 中文指南

    来源于:http://webpackdoc.com/index.html Webpack 是当下最热门的前端资源模块化管理和打包工具.它可以将许多松散的模块按照依赖和规则打包成符合生产环境部署的前端资 ...

  5. 【BZOJ 3051】【UOJ #57】【WC 2013】平面图

    http://www.lydsy.com/JudgeOnline/problem.php?id=3051 http://uoj.ac/problem/57 这道题需要平面图转对偶图,点定位,最小生成树 ...

  6. oracle 修改密码

    [oracle@t-ecif03-v-szzb ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.3.0 Production on Mon Nov 1 ...

  7. Spring AOP动态切换数据源

    现在稍微复杂一点的项目,一个数据库也可能搞不定,可能还涉及分布式事务什么的,不过由于现在我只是做一个接口集成的项目,所以分布式就先不用了,用Spring AOP来达到切换数据源,查询不同的数据库就可以 ...

  8. Jsoup提取文本时保留标签

    使用Jsoup来对html进行处理比较方便,你可能会用它来提取文本或清理html标签.如果你想提取文本时保留标签,可以使用Jsoup.clean方法,参数为html及标签白名单: Jsoup.clea ...

  9. 怪物AI之发现玩家(视觉范围发现系列)

    在网上找到一些资料参考,然后写写自己的想法. 这里感谢MOMO等大神. 我们用玩家检测怪物的方法来测,这样比较试用与弱联网游戏,每次在同步玩家的时候来判断玩家与怪物的位置. 这里给出两个处理方式: 1 ...

  10. C#------连接SQLServer和MySQL字符串

    <connectionStrings> <add name="ConnectionStrings" connectionString="Data Sou ...