binfalse
Conditionally autoscroll a JScrollPane
April 10th, 2012I’m currently developing some GUI stuff and was wondering how to let a JScrollPane scroll automatically if it’s already on the bottom and the size of it’s content increases.
For example if you use a JTextArea to display some log or whatever, than it would be nice if the scroll bars move down while there are messages produced, but it shouldn’t scroll down when the user just scrolled up to read a specific line. To scroll down to the end of a JTextArea can be done with just setting the carret to the end of the text:
JTextArea log = new JTextArea (20, 20);
log.setEditable (false);
JScrollPane scroller = new JScrollPane ();
scroller.setViewportView (output);
// [...]
log.append ("your message");
log.setCaretPosition (log.getDocument ().getLength ());
But we first want to check whether the scroll bar is already at the bottom, and only if that’s the case it should automatically scroll down to the new bottom if another message is inserted. To obtain the position data of the vertical scroll bar on can use the following code:
JScrollBar vbar = scroller.getVerticalScrollBar ();
// get the current position
int currentPosition = vbar.getValue ();
// getMaximum () gives maximum + extent.
int maxPosition = vbar.getMaximum () - vbar.getVisibleAmount ();
if (currentPosition == maxPosition)
{
// in this case we want to scroll after the new text is appended
}
Unfortunately log.append ("some msg")
won’t append the text in place, so the size of the text area will not necessarily change before we ask for the new maximum position. To avoid a wrong max value one can also schedule the scroll event:
private void logText (String text)
{
final JScrollBar vbar = scroller.getVerticalScrollBar ();
// is the scroll bar at the bottom?
boolean end = vbar.getMaximum () == vbar.getValue () + vbar.getVisibleAmount ();
// append some new text to the text area
// (or do something else that increases the contents of the JScrollPane)
log.append (text + "\\n");
// if scroll bar already was at the bottom we schedule
// a new scroll event to again scroll to the bottom
if (end)
{
EventQueue.invokeLater (new Runnable ()
{
public void run ()
{
EventQueue.invokeLater (new Runnable ()
{
public void run ()
{
vbar.setValue (vbar.getMaximum ());
}
});
}
});
}
}
As you can see, here a new event is put in the EventQueue, and this event is told to put another event in the queue that will do the scroll event. Correct, that’s a bit strange, but the swing stuff is very lazy and it might take a while until the new maximum position of the scroll bar is calculated after the whole GUI stuff is re-validated. So let’s be sure that our event definitely happens when all dependent swing events are processed.
galternatives
April 5th, 2012Some days ago I discovered galternatives, a GNOME tool to manage the alternatives system of Debian/Ubuntu. It’s really smart I think.
For example to update the default editor for your system you need to update the alternatives system via:
update-alternatives --set editor /usr/bin/vim
There is also an interactive version available:
update-alternatives --config editor
To see available browsers you need to run
update-alternatives --list x-www-browser
However, the alternatives system is a nice idea I think, but it’s a bit confusing sometimes. And installing a new group or adding another entry to an existing group is pretty complicated and requires information from multiple other commands beforehand.
With galternatives
you’ll get a graphical interface to manage all these things. That really brings light into the dark! Just install it via
aptitude install galternatives
You’ll be astonished if you give it a try! ;-)
YOURLS Firefox Extension Version 1.4
April 2nd, 2012I submitted a new version of the YOURLS Firefox extension.
It just contains some minor changes, but I want to inform my loyal readers! The add-on is currently in the review queue, hopefully this time I’ll get a complete review by the AMO-team ;-)
If you’re crazy you can try the new version, it’s available on SourceForge and on AMO.
UPDATE: I just received a fully review, so my add-on is finally stable!!
J-vs-T goes Java
April 1st, 2012I just ported the Jabber -vs- Twitter bridge to Java.
That was a point on my todo list for a long time, because I hate the hacked stuff from the improvised Perl solution. And in the end I finally did it ;-)
You can find the new XMPP to Twitter bridge with the name XTB in my sidebar. It’s now written in nice Java code, easy to understand and much easier to work with! So feel free to give it a try!
End of announcement! :P
IonHunter
November 28th, 2011Some days ago IonHunter came into the world!
IonHunter is the tool I’m actually developing to receive one of these diplomas. It’s is a software to identify biomarkers in a huge number of LC/MS runs. IonHunter is able to preprocess mass spectrometry data, to merge multiple runs of the same sample and also to correct retention time shifts to align various experiments.
The whole software is modularly designed and easy to extend with further plugins. So developers feel free to join my development!
Currently it isn’t published, but since it’s written in Java it will run on nearly all operating systems. We focused on usability and comfort for mass spec scientists, who will use the tool.
This is just a small announcement for the recently launched website, you might want to take a look at it!? (-;
Small hint for my faithful readers: Subscribe to IonHunters newsletter to stay informed and miss no release!