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. win7如何不用点击用户名 直接自动登录桌面

    win7如何不用点击用户名 直接自动登录桌面 在win7系统中开机时必须点击相应的用户名才能登陆系统桌面那么如何取消这一功能使当前账户自动登录到系统桌面呢? 1 .在开始菜单搜索框输入 “netplw ...

  2. SolrCloud基本过程

    转:http://www.data321.com/yunjisuan/20160514880/SolrZhiJieDuQuZKZhongDePeiZhiXin SolrCloud之分布式索引及与Zoo ...

  3. 【Acm】算法之美—Jugs

    题目概述:Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted wi ...

  4. QQ通信原理及QQ是怎么穿透内网进行通信的?

    http://blog.csdn.net/frank_good/article/details/51160027 ******************************************* ...

  5. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  6. Lintcode: First Bad Version 解题报告

    First Bad Version http://lintcode.com/en/problem/first-bad-version The code base version is an integ ...

  7. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  8. 【机器学习】主成分分析PCA(Principal components analysis)

    1. 问题 真实的训练数据总是存在各种各样的问题: 1. 比如拿到一个汽车的样本,里面既有以“千米/每小时”度量的最大速度特征,也有“英里/小时”的最大速度特征,显然这两个特征有一个多余. 2. 拿到 ...

  9. java多线程16:join()的使用

    讲解join()方法之前请确保对于即wait()/notify()/notifyAll()机制已熟练掌握.可以参考前面的笔记 join()方法的作用是等待线程销毁.join()方法反应的是一个很现实的 ...

  10. JAVA-JSP内置对象之request对象参数

    相关资料:<21天学通Java Web开发> request对象1.request对象不但可以用来设置和取得requset范围变量,还可以用来获得客户端请求参数请求的来源.表头.cooki ...