JTabbedPane 和 JScrollBar 联合使用

需求:实现一个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的内部原理并没有去深入了解。各位有什么好办法可以提供下。

上一篇:java + maven 实现发送短信验证码功能


下一篇:[日常] nginx记录post数据