1. 功能

作为swing的组件,JList与JTextArea是不可以单独实现滚动功能的,需要与JScrollPane结合才可以。

本代码中:

JList实现从其它数据源获取数据,然后依次对这些数据进行处理,处理过程中,在JList中选择当前处理的记录,依次向下移动。

JTextArea显示处理结果,因为有很多数据,内容满了的时候,需要滚动显示,就是一直显示最新的数据。

2. 实现代码

注意:下面的代码片段必须插入类的各相关段中,不是完整代码。

    // 代码片段一,定义变量
private JList<String> jListAuthor;
private JScrollPane jScrollPaneAuthor;
private JScrollPane jScrollPaneInfo;
private JTextArea jTextAreaInfo; // ......

// 代码片段二,生成对象并加入到界面中
{
{
jListAuthor = new JList<String>();
}
jScrollPaneAuthor = new JScrollPane(); // For ensureIndexIsVisible method to work, the JList must be within a JViewport.
jScrollPaneAuthor.getViewport().setView(jListAuthor);
getContentPane().add(jScrollPaneAuthor);
jScrollPaneAuthor.setBounds(5, 5, 150, 403);
jScrollPaneAuthor.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
} {
{
jTextAreaInfo = new JTextArea();
jTextAreaInfo.setText("");
jTextAreaInfo.setLineWrap(true); // 设置自动换行 // 设置断行不断字
// If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width.
// If set to false, the lines will be wrapped at character boundaries. By default this property is false.
jTextAreaInfo.setWrapStyleWord(true);
}
jScrollPaneInfo = new JScrollPane(jTextAreaInfo);
getContentPane().add(jScrollPaneInfo);
jScrollPaneInfo.setBounds(347, 0, 290, 403);
} // ......

// 代码片段三,获取数据并填充左边的JList
TreeSet<String> ts = myService.getAuthors();
    @SuppressWarnings({ "rawtypes", "unchecked" })
    ListModel<String> jListModelAuthor = new DefaultComboBoxModel(
ts.toArray());
jListAuthor.setModel(jListModelAuthor); // ......

// 代码片段四,对左边的JList进行遍历,处理,处理结果显示在右边JTextArea,并刷左右界面显示
ListModel<String> lm = jListAuthor.getModel();
int totalIndexs = lm.getSize();


// 起始值从当前选择的记录+1
for(int index=jListAuthor.getSelectedIndex()+1; index<totalIndexs; index++) {
String uname = (String)lm.getElementAt(index);


// ......
// ......


// 刷新左边JList窗口
jListAuthor.setSelectedIndex(index);
jListAuthor.ensureIndexIsVisible(index);


// 如果左边界面刷新出现问题,可以尝试加入此条语句
jScrollPaneAuthor.repaint();


List<String> tempResult = myService.processRecord(uname);
for(String str: tempResult) {
// 右边增加一行处理结果
jTextAreaInfo.append(str + "\n"); // 刷新右边JTextArea窗口
jTextAreaInfo.setCaretPosition(jTextAreaInfo.getDocument().getLength());


// ......
// ......
}
}

3. 效果

最新文章

  1. js给数组去重写法
  2. Asp.Net MVC&lt;二&gt; : IIS/asp.net管道
  3. git流程及操作
  4. IOC Container(服务容器)的工作机制
  5. Javascript 错误捕获
  6. [HDOJ3466]Proud Merchants(贪心+01背包)
  7. jquery选择器的使用
  8. 技术博客rss订阅源收集
  9. sql-从查询结果创建一个永久表
  10. C++对象模型学习笔记
  11. 使用SOAPUI测试WEBAPI接口
  12. matlab---边缘之sobel简单实例
  13. 转: 深入理解 AngularJS 的 Scope
  14. English is very important!
  15. Redis其他常用操作
  16. hdu 5179 beautiful number(数位dp)
  17. C和C指针小记(十三)-数组
  18. php算法面试案例(转)
  19. python 数据类型 之 tuple 元组
  20. [android] 轮播图-无限循环

热门文章

  1. VB LISTBOX属性
  2. vim插件之pathogen,NERDTree,Command-T,Powerline
  3. CF MVC3此操作要求连接到 &#39;master&#39; 数据库。无法创建与 &#39;master&#39; 数据库之间的连接,这是因为已打开原始数据库连接,并且已从连接字符串中删除凭据。请提供未打开的连接 解决方法
  4. 微信小程序:input输入框和form表单几种传值和取值方式
  5. js removeChild
  6. 狄斯奎诺(dijkstra 模板)
  7. java MessageFormat.format
  8. C#搞工控的一些代码
  9. 配置Windows server 2008文件同步[转]
  10. iPhone手机屏幕的尺寸180330更新