Lubos Programming Blog

Lubos Blog about Programming (mainly Java) and Computers

Archive for the ‘swing’ Category

Prettier failures using Swing TaskDialog « Schneide Blog

Posted by lubosp on April 6, 2010

Prettier failures using Swing TaskDialog « Schneide Blog

via Prettier failures using Swing TaskDialog « Schneide Blog.

Advertisements

Posted in swing | Tagged: | Leave a Comment »

Platform dependent line separator handling in Swing JTextArea

Posted by lubosp on July 25, 2009

If you wish to support platform dependent line separator handling in Swing JTextArea you have to use its read() and write() methods to load or persist the text in the JTextArea. Internally text area remembers the line separators and converts all of them to \n. When the text area is saved into file using write(), the line separators are converted to its original value.

Here is sample code to read the file into text area:

public class MyTextArea extends JTextArea {
...

try {
 FileInputStream fis = new FileInputStream(sourceFile);
 reader = new InputStreamReader(fis);
 read(reader, null);
} finally {
 if (reader != null) {
 try {
 reader.close();
 } catch (Exception ex) {
 // log exception
 }
 }
 }
}

Here is sample code to write text area into a file:

<pre>
<pre>public class MyTextArea extends JTextArea {
...</pre>
</pre>
try {
 writer = new OutputStreamWriter(new FileOutputStream(newFile));
 write(writer);
 } finally {
 if (writer != null) {
 try {
 writer.close();
 } catch (Exception ex) {
 // log exception
 }
 }
 }
}

Do not read the file content using Reader directly to get the content of the file and than call JTextArea.setText() it will not remember the line separator and it will keep the original line separator in the text area, so for example if the file uses Windows line separator \r\n, you will have to press Delete/Backspace key twice to delete line terminator. Pressing Enter key will insert \n and might cause inconsistent line separators (e.g. mixture of \r\n and \n) in the file when saved.

Do not write the file content using Writer directly to save the content of the text area in the file and after call JTextArea.getText(), it  would save the line separators as they are currently in the text area without converting them to the original state.

Check this thread for more info.

Posted in swing | Tagged: , | Leave a Comment »

Kirill Grouchnikov’s Blog: How to create your own icons

Posted by lubosp on February 26, 2005

Kirill Grouchnikov’s Blog: How to create your own icons

Posted in java, swing | Leave a Comment »

Creating Wizard Dialogs with Java Swing Wizard di…

Posted by lubosp on February 17, 2005

Creating Wizard Dialogs with Java Swing

Wizard dialogs are ubiquitous in today’s desktop applications. This article creates a framework for a simple wizard dialog, complete with Back, Next, and Cancel buttons that you can extend for use in your applications as necessary.

[via java.sun.com]

Posted in java, swing | Leave a Comment »