JTabbedPane 和 JScrollBar 联合使用

本文介绍了一个Java Swing应用实例,通过监听JScrollBar的变化来实现在滚动到底部时自动加载更多数据的功能。该功能主要适用于JTabbedPane组件中的表格数据展示。

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

    下面是具体代码:

  1 import java.awt.*;
  2 import javax.swing.table.DefaultTableModel;
  3 import javax.swing.*;
  4 import javax.swing.event.ChangeEvent;
  5 import java.awt.event.*;
  6 
  7 public class JTabbedPaneExample extends JFrame implements AdjustmentListener
  8 {
  9     private GridBagLayout gridBagLayout = new GridBagLayout();
 10     private JSplitPane splitPane = new JSplitPane();
 11     private JPanel timePanel = new JPanel();
 12     
 13     private GridBagLayout gridBagLayoutForTime = new GridBagLayout();
 14     private JButton btnSearch = new JButton();
 15     private HistoryStatisticsTableModel historyStatisticsTableModel = new HistoryStatisticsTableModel();
 16     private JTable searchTable = new JTable(historyStatisticsTableModel);
 17     private JScrollPane searchScrollPane = new JScrollPane(searchTable);
 18     private JScrollBar scrollBarForVertical = new JScrollBar(JScrollBar.VERTICAL, 10, 10, 0, 100);
 19     private JScrollBar scrollBarForHorizontal = new JScrollBar(JScrollBar.HORIZONTAL, 10, 10, 0, 100);
 20     private int intPageNum = 1;
 21     
 22     
 23     public JTabbedPaneExample()
 24     {
 25         jbInit();
 26     }
 27     
 28     private void jbInit()
 29     {
 30         this.setLayout(gridBagLayout);
 31         searchTable.setEnabled(false);
 32         splitPane.setLastDividerLocation(-1);
 33         splitPane.setDividerLocation(150);
 34         this.add(splitPane,   new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
 35                 ,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
 36         
 37         timePanel.setLayout(gridBagLayoutForTime);
 38         splitPane.add(timePanel, JSplitPane.RIGHT);
 39         
 40         btnSearch.setText("Search");
 41         btnSearch.addActionListener(new java.awt.event.ActionListener()
 42         {
 43             public void actionPerformed(ActionEvent e)
 44             {
 45                 searchBtn_actionPerformed(e);
 46             }
 47         });
 48         
 49         timePanel.add(btnSearch, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0 
 50                       ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 50, 0, 0), 0, 0));
 51         timePanel.add(searchScrollPane, new GridBagConstraints(0, 3, 5, 1, 1.0, 1.0 
 52                       ,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 50, 50, 50), 0, 0));
 53         scrollBarForVertical.setUnitIncrement(1);
 54         scrollBarForVertical.setBlockIncrement(10);
 55         scrollBarForVertical.setMaximum(90);
 56         scrollBarForVertical.addAdjustmentListener(this);
 57         searchScrollPane.setVerticalScrollBar(scrollBarForVertical);
 58         searchScrollPane.setHorizontalScrollBar(scrollBarForHorizontal);
 59     }
 60     
 61      public void adjustmentValueChanged(AdjustmentEvent e)
 62      {
 63          boolean isChange = false;
 64          System.out.println("XXXXX bar Max : " + scrollBarForVertical.getMaximum());
 65          System.out.println("XXXX bar Max Size : " + scrollBarForVertical.getMaximumSize());
 66          System.out.println("XXXX bar pre size : " + scrollBarForVertical.getPreferredSize());
 67          System.out.println("XXXX pane Max size : " + searchScrollPane.getMaximumSize());
 68          System.out.println("XXXX pane size : " + searchScrollPane.getSize());
 69          System.out.println("XXXX pane getHeight size : " + searchScrollPane.getHeight());
 70          System.out.println("XXXX pane pre size : " + searchScrollPane.getPreferredSize());
 71          System.out.println("XXXX intPageNum : " + intPageNum);
 72          
 73          if ((JScrollBar) e.getSource() == scrollBarForVertical) 
 74          {
 75              long currentValue = e.getValue();
 76              long diff = scrollBarForVertical.getMaximum() - searchScrollPane.getHeight();
 77              System.out.println("XXXX currentValue = " + currentValue);
 78              System.out.println("XXXX diff = " + diff);
 79              if(currentValue >= diff && 0 != currentValue) //下拉到table底部,自动加载下一次数据功能
 80              {
 81                  //System.out.println("XXXX scrollBarForVertical currentSize : " + currentSize);
 82                  System.out.println("XXXX Yes");
 83                  test(); 
 84                  intPageNum = intPageNum + 1;
 85                  isChange = true;
 86              }
 87              
 88              if(isChange) 
 89              {
 90                 // scrollBarForVertical.setValue(0);
 91              }
 92          }
 93      }
 94     
 95     public static void main(String[] args)
 96     {
 97         JTabbedPaneExample test = new JTabbedPaneExample();
 98         test.setVisible(true);
 99     }
100     
101     private void test()
102     {
103         String[] aStrCategoryName = new String[500];
104         String[] aStringCounterName = new String[500];
105         String[] aStrStartTime = new String[500];
106         String[] aStopTime = new String[500];
107         String[] aStrValue = new String[500];
108         int i = 0;
109         for(; i < 499; i++)
110         {
111             aStrCategoryName[i] = i + "";
112             aStringCounterName[i] = i + "";
113             aStrStartTime[i] = i + "";
114             aStopTime[i] = i + "";
115             aStrValue[i] = i + "";
116         }
117         
118         //historyStatisticsTableModel.createTable(aStrCategoryName, aStringCounterName, aStrStartTime, aStopTime, aStrValue);
119         historyStatisticsTableModel.createTable(aStrCategoryName, aStringCounterName, aStrStartTime, aStopTime, aStrValue);
120         //historyStatisticsTableModel.addRow();
121         
122     }
123     
124     private void searchBtn_actionPerformed(ActionEvent e)
125     {
126         test();
127     }
128     
129     public class Parameter
130     {
131         public Parameter(String strCategoryName, String strCounterName, String strStartTime, String strStopTime, String strValue)
132         {
133             this.strCategoryName = strCategoryName;
134             this.strCounterName = strCounterName;
135             this.strStartTime = strStartTime;
136             this.strStopTime = strStopTime;
137             this.strValue = strValue;
138         }
139 
140         public String strCategoryName;
141         public String strCounterName;
142         public String strStartTime;
143         public String strStopTime;
144         public String strValue;
145     }
146     
147     private class HistoryStatisticsTableModel  extends DefaultTableModel 
148     {
149         private String[] COLUMN_NAMES = {"Category Name", "Counter Name", "Start Time", "Stop Time", "Value"};
150         public Parameter[] myparameters = new Parameter[0];
151     
152         public HistoryStatisticsTableModel()
153         { 
154             super();
155             super.setColumnIdentifiers(COLUMN_NAMES);
156             
157             super.setRowCount(0);
158         }
159     
160         public int getColumnCount()
161         {
162             return COLUMN_NAMES.length;
163         }
164         
165 /*
166         public int getRowCount()
167         {
168             return myparameters.length;
169         }
170 */
171         
172         public void setRowCount(int rowCount)
173         {
174         }
175         
176         public String getColumnName(int col)
177         {
178             return COLUMN_NAMES[col];
179         }
180         
181         
182         
183         public void addRow(String strCategoryName, String strCounterName, String strStartTime, String strStopTime, String strValue)
184         {
185             Object[] rowData = new Object[COLUMN_NAMES.length];
186             for (int i = 0; i < COLUMN_NAMES.length; i++)
187             {
188                 switch(i)
189                 {
190                     //Category Name
191                     case 0:
192                         rowData[i] = strCategoryName;
193                         break;
194                     //Counter Name
195                     case 1:
196                         rowData[i] = strCounterName;
197                         break;
198                     //Start Time
199                     case 2:
200                         rowData[i] = strStartTime;
201                         break;
202                     //Stop Time
203                     case 3:
204                         rowData[i] = strStopTime;
205                         break;
206                     //Value
207                     case 4:
208                         rowData[i] = strValue;
209                         break;
210                     default:
211                         rowData[i] = "";
212                 }
213             }
214             
215             super.addRow(rowData);
216         }
217        /* 
218         public Object getValueAt(int row, int col)
219         {
220             if (0 == col)
221             {
222                 return myparameters[row].date;
223             }
224             else if (1 == col)
225             {
226                 return myparameters[row].usage;
227             }
228             else if (2 == col)
229             {
230                 return myparameters[row].ratio;
231             }
232             else
233             {
234                 return null;
235             }
236         }
237         */   
238         
239 
240 /*
241         public void createTable(Document response)
242         {
243             NodeList list = response.getElementsByTagName("Parameter");
244             if (list.getLength() > 0)
245             {
246                 Parameter[] aParameters = new Parameter[list.getLength()];
247                 Element eparameter;
248                 int acounter = 0;
249                 for (int i = 0; i < list.getLength(); i++)
250                 {
251                     eparameter = (Element)list.item(i);
252                     String aStrCategoryName = getElementValue(eparameter.getElementsByTagName("Category Name"));
253                     String aStrCounterName = getElementValue(eparameter.getElementsByTagName("Counter Name"));
254                     String aStrStartTime = getElementValue(eparameter.getElementsByTagName("Start Time"));
255                     String aStrStopTime = getElementValue(eparameter.getElementsByTagName("Stop Time"));
256                     String aStrValue = getElementValue(eparameter.getElementsByTagName("Value"));
257                     aParameters[i] = new Parameter(aStrCategoryName, aStrCounterName, aStrStartTime, aStrStopTime, aStrValue);
258                 }
259                 myparameters = aParameters;
260             }
261             else
262             {
263                 myparameters = new Parameter[0];
264             }
265 
266             fireTableDataChanged();
267         }
268 */
269         // Wirte for test
270         public void createTable(String[] strCategoryName, String[] strCounterName, String[] strStartTime, String[] strStopTime, String[] strValue)
271         {
272             int intCounter = strCategoryName.length;
273             
274             for(int i = 0; i < intCounter; i++)
275             {
276                 addRow(strCategoryName[i], strCounterName[i], strStartTime[i], strStopTime[i], strValue[i]);
277             }
278             
279             fireTableDataChanged();
280         } 
281     }
282 }

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

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

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

转载于:https://www.cnblogs.com/AndyStudy/p/5976400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值