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. PHP网站环境搭配: Apache Http+PHP+Mysql

    Apache Http+PHP+Mysql 环境搭配 1. 先下载上述三个软件 都要下载对应系统的软件,mysql还可以再下载navicat for mysql. 2.  安装Apache Http ...

  2. HBase最佳实践-用好你的操作系统

    终于又切回HBase模式了,之前一段时间因为工作的原因了解接触了一段时间大数据生态的很多其他组件(诸如Parquet.Carbondata.Hive.SparkSQL.TPC-DS/TPC-H等),虽 ...

  3. Python+SparkStreaming+kafka+写入本地文件案例(可执行)

    从kafka中读取指定的topic,根据中间内容的不同,写入不同的文件中. 文件按照日期区分. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Tim ...

  4. js实现默认或者触发一个事件选中元素内容的方法

    方法一:非文本框.文本域的选中内容方法 <!Doctype html> <html> <head> <script type="text/javas ...

  5. 三者互ping,PC,虚拟机,uboot,nfs网络文件系统搭建

    要想实现三者互ping,韦老师虽然专门出了视频说明,但是在自己配置过程还是出现了问题,这里记录一下解决办法,虽然我也不知道原因,但是解决了出现的问题也实现了三者互ping. 首先,我的硬件设备是PC通 ...

  6. EXCEL行倒叙

  7. CTF之PHP黑魔法总结

    继上一篇php各版本的姿势(不同版本的利用特性),文章总结了php版本差异,现在在来一篇本地日记总结的php黑魔法,是以前做CTF时遇到并记录的,很适合在做CTF代码审计的时候翻翻看看. 一.要求变量 ...

  8. xss绕过htmlspecialchars实体编码的姿势

    倘若是在script.input标签当中,即可突破.Payload ' oninput=alert`1` //      当要在input中输入内容时触发事件' oninput=alert`1` ' ...

  9. elasticsearch golang的sdk使用

    文档第一 <elasticsearch权威指南>直接看官网在线版的,比较新,网上那些pdf版的,都是2.x版的,许多不兼容 官方API手册,可以选择版本. golang sdk库的选择 主 ...

  10. idea中git git pull push需要反复输入密码

    在使用idea开发的过程中,在终端terminal中git pull和git push时遇到一个问题,一个是 每次提交都需要输入用户名和密码,,从网上找了下解决方案,记录一下. 解决: 打开git终端 ...