The default model for a list does not allow the addition and removal of items. The list must be created with a DefaultListModel.

    // Create a list that allows adds and removes
DefaultListModel model = new DefaultListModel();
JList list = new JList(model); // Initialize the list with items
String[] items = {"A", "B", "C", "D"};
for (int i=0; i<items.length; i++) {
model.add(i, items[i]);
} // Append an item
int pos = list.getModel().getSize();
model.add(pos, "E"); // Insert an item at the beginning
pos = 0;
model.add(pos, "a");

This method replaces an it

 
 
Home > List of Packages > javax.swing.text [49 examples] > JTextArea [7 examples]

e988. 用文本域实现一个控制台窗口

This example creates a console window using a JTextArea which shows all output printed to System.out and System.err. System.out and System.err are replaced with piped io streams. Two reader threads are created that retrieve the output from these piped io streams and append it to the JTextArea component.

    import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*; public class Console extends JFrame {
PipedInputStream piOut;
PipedInputStream piErr;
PipedOutputStream poOut;
PipedOutputStream poErr;
JTextArea textArea = new JTextArea(); public Console() throws IOException {
// Set up System.out
piOut = new PipedInputStream();
poOut = new PipedOutputStream(piOut);
System.setOut(new PrintStream(poOut, true)); // Set up System.err
piErr = new PipedInputStream();
poErr = new PipedOutputStream(piErr);
System.setErr(new PrintStream(poErr, true)); // Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(50);
getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER);
pack();
setVisible(true); // Create reader threads
new ReaderThread(piOut).start();
new ReaderThread(piErr).start();
} class ReaderThread extends Thread {
PipedInputStream pi; ReaderThread(PipedInputStream pi) {
this.pi = pi;
} public void run() {
final byte[] buf = new byte[1024];
try {
while (true) {
final int len = pi.read(buf);
if (len == -1) {
break;
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(new String(buf, 0, len)); // Make sure the last line is always visible
textArea.setCaretPosition(textArea.getDocument().getLength()); // Keep the text area down to a certain character size
int idealSize = 1000;
int maxExcess = 500;
int excess = textArea.getDocument().getLength() - idealSize;
if (excess >= maxExcess) {
textArea.replaceRange("", 0, excess);
}
}
});
}
} catch (IOException e) {
}
}
}
}

The console window is created using

    try {
new Console();
} catch (IOException e) {
}
Related Example

e778. 在JList中加入和删除项的更多相关文章

  1. IE浏览器中的加载项怎么删除

    IE浏览器中的加载项是一些软件或者浏览器的功能控件,我们可以通过禁用.开启来控制是否使用某些加载项,同时可以将一些加载项删除. 比如当我们遇到了一些不好的加载项,想要将它删除,通过这篇经验,教大家怎么 ...

  2. 关于WrapPanel和RadioButton相互配合使用实WrapPanel现动态添加或删除项

    最近在做一个项目的时候,有一个需求就是,通过RadioButton来控制一行内容的显示与不显示,当不显示的时候,下面的项能够占住相应的位置,当增加的时候,又会在原来的位置重新显示,如果使用一般的Gri ...

  3. Linux系统中查找、删除重复文件,释放磁盘空间。

    在Linux系操作系统中查找并删除重复文件的方法的确有很多,不过这里介绍的是一款非常简单实用的软件FSlint.FSlint是一个重复文件查找工具,可以使用它来清除不必要的重复文件,笔者经常使用它来释 ...

  4. Rails中实现批量删除

    在Rails生成的控制器模版中,包含的destroy只能处理单个对象,而批量删除要求能够同时处理多个对象,这需要自定义一个批量操作action.批量删除的效果图如下:

  5. 解决QML开发中ComboBox中一个已选择项没有清除的问题

    解决QML开发中ComboBox中一个已选择项没有清除的问题 近期使用QML开发一个项目.须要使用ComboBox进行显示.当进行一个操作时,须要向ComboBox加入一个元素,当进行另外一个操作时. ...

  6. vue中添加与删除,关键字搜索

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. JavaScript中数组元素删除的七大方法汇总

    原文链接:https://blog.csdn.net/u010323023/article/details/52700770 在JavaScript中,除了Object之外,Array类型恐怕就是最常 ...

  8. "不能在 DropDownList 中选择多个项。"其解决办法及补充

    探讨C#.NET下DropDownList的一个有趣的bug及其解决办法 摘要: 本文就C#.Net 环境下Web开发中经常使用的DropDownList控件的SelectedIndex属性进行了详细 ...

  9. 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常

    在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应 ...

随机推荐

  1. asp.net mvc 项目架构解析

    请先看框架图: 从上图可知: 1.Controller控制器只是充当了管道的作用.只做任务的分发,不做请求中的具体业务处理. 2.Views视图充当了展示数据的作用.不做任何取数逻辑的处理,只是展示逻 ...

  2. Content-Type中application/x-www-form-urlencoded 和 multipart/form-data的区别

    1.什么是Content-Type Form的enctype属性表示页面表单数据向服务端传输时的编码方式, 常用有两种:application/x-www-form-urlencoded和multip ...

  3. (转)C++头文件顺序

    转自:http://blog.csdn.net/clever101/article/details/7269058关键总结:先包含的头文件的函数会覆盖后包含的头文件的同名函数.建议采用Google C ...

  4. Vue2键盘事件:keydown/keyup...

    Vue2键盘事件:keydown/keyup... 1.使用 <!DOCTYPE html> <html> <head> <title></tit ...

  5. Docker 入门(Mac环境)- part 5 stacks

    part-5 stacks 简介 stack就是栈,栈的结构是什么样的呢?一层一层是紧挨着的,然后互相依赖,不能说中间少了一个.这样说就很明白了,栈实际上在docker中就相当于多个互相依赖的组件,共 ...

  6. idea linux 启动权限不足的问题

    修改 ideaproject 下所有文件  chmod -R 777 文件  递归修改文件权限

  7. 基于html5顶部导航3D翻转展开特效

    基于html5顶部导航3D翻转展开特效是一款基于jQuery+HTML5实现的3D翻转网站导航菜单代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <header cla ...

  8. 基于jQuery实现滚动新闻代码下载

    分享一款基于jQuery实现滚动新闻代码下载.这是一款基于bootstrup 3实现的响应式jQuery滚动新闻插件.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div ...

  9. Virtual DOM 虚拟DOM的理解(转)

    作者:戴嘉华 转载请注明出处并保留原文链接( #13 )和作者信息. 目录: 1 前言 2 对前端应用状态管理思考 3 Virtual DOM 算法 4 算法实现 4.1 步骤一:用JS对象模拟DOM ...

  10. C语言 · 最长公共子序列 · 最长字符序列

    算法提高篇有两个此类题目: 算法提高 最长字符序列   时间限制:1.0s   内存限制:256.0MB      最长字符序列 问题描述 设x(i), y(i), z(i)表示单个字符,则X={x( ...