需求:实现一个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. JavaScript实现计算两个日期之间的天数

    以“yyyy-MM-dd”格式为例,现在有两个日期,分别为“2006-01-01”,“2007-05-20”. 1.调用Date.parser()方法,将字符串格式的时间戳转换为Date类型时间对象: ...

  2. 86 ipmitools-查看硬件信息工具

    1.简介 IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备"智能化"的新一代通用接口标准.用户可以利 ...

  3. string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别

    string.IsNullOrEmpty 都知道,这个功能是判断字符串是否为:null或者string.Empty.如果是如"\t"这样的字符就返回false了,为了达到判断过滤这 ...

  4. Asp.Net MVC<五>:过滤器

    ControllerActionInvoker在执行过程中除了利用ActionDescriptor完成对目标Action方法本身的执行外,还会执行相关过滤器(Filter).过滤器采用AOP的设计,它 ...

  5. VMware安装Centos7,已将该虚拟机配置为使用64为,却无法执行64位操作

    在新建虚拟机之后,相信很多人都遇到了这个问题,这个问题的本质就是电脑是否支持虚拟化,虽然不是很清楚这是什么 解决方案就是,重启电脑(这边的电脑不是虚拟机而是主机),进入BIOS界面(不同电脑进入BIO ...

  6. Neural Style学习3——操作

    Basic usage: th neural_style.lua -style_image <image.jpg> -content_image <image.jpg> Ope ...

  7. PHPstorm的数据库功能

    PHPstorm真是神器,居然有表.视图.存储过程的功能,非常人性化,建表那叫一个舒服,而且sql语句可以像其他代码一样显示"区域",结构更加清晰.

  8. CommonJS,AMD,CMD区别

    学得比较晕,再次看commonjs,amd, cmd时好像还是没完全弄清楚,今天再整理一下: commonjs是用在服务器端的,同步的,如nodejs amd, cmd是用在浏览器端的,异步的,如re ...

  9. 一、Daily Scrum Meeting【Alpha】------Clover

    [Alpha]Daily Scrum Meeting 第一次 [Alpha]Daily Scrum Meeting 第二次 [Alpha]Daily Scrum Meeting 第三次 [Alpha] ...

  10. 【Alpha】阶段汇总

    [项目文档&API文档] PhyLab2.0需求与功能分析改进文档(NABCD) PhyLab2.0设计分析阶段任务大纲(α) 团队个人贡献分分配规则 功能规格说明书 [Phylab2.0]A ...