需求:实现一个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. Batsing的网页编程规范(HTML/CSS/JS/PHP)

    特别注意!!!我这里的前端编程规范不苟同于Bootstrap的前端规范. 因为我和它的目的不同,Bootstrap规范是极简主义,甚至有些没有考虑到兼容性的问题. 我的规范是自己从编程实践中总结出来的 ...

  2. NDK开发之一

    2015.07.22 Wiki_Tree: --NDK开发: --NDK特征: --MK文件编写规则: NDK开发: Ndk-build编译时会生成的两个同名的so库,位于不同的目录/project ...

  3. Spring IOC容器创建对象的方式

    一.无参构造函数创建                                                                            我们用Spring创建Stu ...

  4. Android LitePal 神一般的数据库框架 超级好用

    参考: Android数据库高手秘籍(一)--SQLite命令 Android数据库高手秘籍(二)--创建表和LitePal的基本用法 Android数据库高手秘籍(三)--使用LitePal升级表 ...

  5. Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)

     传送门 Description Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has ...

  6. android md5加密与rsa加解密实现代码

    import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security. ...

  7. myBatis foreach详解【转】

    MyBatis的foreach语句详解 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,ope ...

  8. 简述block

    block传值也适用于从后往前传值 先介绍block的基本知识 /** * 1.如何定义一个Block变量 2.怎样给定义的Block变量赋初值 3.如何冲定义Block类型 4.如何使用Block实 ...

  9. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  10. 转载:postgresql分区与优化

    --对于分区表constraint_exclusion 这个参数需要配置为partition或on postgres=# show constraint_exclusion ; constraint_ ...