Entries Tagged 'Java' ↓
May 27th, 2009 — Java
I have an application that is sort of a networked console. It allows multiple clients to send messages to it, and displays those messages either in a console instance or in a JFrame with a JTextArea.
I ran into the problem of the JTextArea not scrolling down when new text was appended. (FYI, the JTextArea is in a JScrollPane.) Once the JTextArea contained more lines of text than could be displayed, the new text being appended wasn’t visible unless I scrolled. What I wanted was automatic scrolling. That was accomplished with one line of code:
textArea.setCaretPosition(textArea.getDocument().getLength());
March 24th, 2009 — Java
I’m working on a somewhat complicated Java web application that has evolved over a number of years. As usual, evolution has resulted in the code getting a bit unwieldy. I needed to get the details of the call stack in various places so I could determine (at runtime) where a method was called from. To accomplish that, I developed this class:
package com.purplewasp.util;
import java.util.ArrayList;
public class CallStackUtil {
public synchronized static String getCallStackAsString() {
StringBuilder sb = new StringBuilder();
StackTraceElement[] stackTraceElements =
Thread.currentThread().getStackTrace();
String[] array = getCallStackAsStringArray(stackTraceElements);
for (int i = 0; i < array.length; i++) {
sb.append(array[i] + "\n");
}
return sb.toString();
}
public synchronized static String[] getCallStackAsStringArray() {
StackTraceElement[] stackTraceElements =
Thread.currentThread().getStackTrace();
String[] array = getCallStackAsStringArray(stackTraceElements);
return array;
}
private synchronized static String[] getCallStackAsStringArray(StackTraceElement[] stackTraceElements) {
ArrayList<String> list = new ArrayList<String>();
String[] array = new String[1];
for (int i = 0; i < stackTraceElements.length; i++) {
StackTraceElement element = stackTraceElements[i];
String classname = element.getClassName();
String methodName = element.getMethodName();
int lineNumber = element.getLineNumber();
list.add(classname + "." + methodName + ":" + lineNumber);
}
return list.toArray(array);
}
}
|
June 20th, 2008 — Java
I recently installed Maven in Eclipse (specifically, MyEclipse) and kept getting this error at startup:
6/20/08 3:44:40 PM CDT: Eclipse is running in a JRE, but a JDK is required
Some Maven plugins may not work when importing projects or updating source folders.
The fix was to modify my eclipse.ini file as shown here (changes are bold):
-showsplash
com.genuitec.myeclipse.product
--launcher.XXMaxPermSize
256m
-vm
C:\java\jdk1.6.0_06\bin\javaw.exe
-vmargs
-Xms128m
-Xmx512m
-Duser.language=en
-XX:PermSize=128M
-XX:MaxPermSize=256M