需求:实现一个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. HTML 转 PDF

    使用WkHtmlToXSharp,免费的软件,内集成了chrome 的内核,可以对CSS进行渲染,很好用 是需要传入 HTML 网页地址就可以转化为PDF文件,不过网页的编码要是 utf-8 Nuge ...

  2. centos6.4下安装php7+nginx+mariadb环境

    一,安装php71,创建php用户和用户组,并在github下载php7源码#新建php用户和php组# groupadd -r php && useradd -r -g php -s ...

  3. windows下编译chromium浏览器的15个流程整理

    编译chromium 系统为windows, 国内在windows上编译chromium的资料比较少, 我这篇文章只能作为参考, 记录我遇到的一些问题,因为chromium团队也会修改了代码,或者编译 ...

  4. UIDynamic - 推动行为: UIPushBehavior

    用途: 从一个点移动到另外一个点; 相关属性: mode : UIPushBehaviorModeContinuous  //推移模式 angle : setAngle  //推移角度 magnitu ...

  5. Linux--目录结构解释(转)

    / root --- 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home --- 存储普通用户的个人文件 ftp --- 用户所有服务 httpd samba user ...

  6. Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)

    传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...

  7. web前端基础知识-(三)JavaScript基本操作

    JavaScript 是一种轻量级的编程语言. JavaScript 是可插入 HTML 页面的编程代码. JavaScript 插入 HTML 页面后,可由所有的现代浏览器执行. JavaScrip ...

  8. 获取centos6.5系统信息脚本

    最近想尝试做两件比较重要的事情,第一是用python写个cmdb,第二还是用python写个小型监控系统,下面是获取系统信息的脚本: #!/usr/bin/env python # coding:ut ...

  9. redis3.2 最新版本启动配置文件redis.conf详细说明

    Redis.conf文件内容详细说明: # 默认redis不是以后台进程的方式启动,如果需要在后台运行,需要将这个值设置成yes # 以后台方式启动的时候,redis会写入默认的进程文件/var/ru ...

  10. 说一说ASCLL和Unicode

    关于字符编码这个展开来说有太多东西了,这里主要是想说一说最常说的ASCLL和Unicode字符编码的问题,这样至少你在用相关函数的时候,可以搞明白参数的真正含义. ASCLL编码 计算机就是0和1的世 ...