Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Friday, 11 January 2008
Add a jar file to Java load path at run time
Sometimes it is necessary to amend the class load path at run time. For example, dynamically adding jar files containing user-configurable JDBC data sources. The way this is done is truly monstrous -- the URL Path format was only revealed when a colleague looked into the Java library sources. Here is a simple example of how this is done jar-loader-src-0.1.jar which, as usual, has an ant build file: just type "ant run" at the shell prompt to compile and run it. You need ant and JDK >= 1.5 for this.
Checking if a JEditorPane has been edited (changed)
While I was looking for a way to check if a JEditorPane's text had been changed since the last save, I found a site that actually wanted me to pay for the answer. Bloody cheek. It isn't that tough, but it took a bit of reading. All that is needed is to install a DocumentListener into the editor pane's document. You can download a complete example swing-editor-src-0.1.jar which includes an ant build file. Just type "ant run" at the shell prompt to run it. You will need ant and JDK >= 1.5 (it may work in older JDKs, but I used 1.5 so I can't be sure).
The document can't tell when your program has saved the document (ie, it is no longer "dirty"), so you need to call setDirty(false) after a save.
The document can't tell when your program has saved the document (ie, it is no longer "dirty"), so you need to call setDirty(false) after a save.
Java Swing JButton keyboard shortcuts
It took ages to work out how to do this, so I decided to post it here in the hope someone else will benefit. Here's how to install a keyboard shortcut to a a Swing JButton so the button behaves as though it was clicked when the key is pressed:
A complete example is in shortcut-buttons-src-0.1.jar. The download includes an ant build file. To compile and run, just type "ant run" at the shell prompt. You will need ant and a JDK >= 1.5 installed.
JButton saveButton = new JButton(new SaveAction());
InputMap inputMap = saveButton.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
inputMap.put(enter, "ENTER");
saveButton.getActionMap().put("ENTER", new ClickAction(saveButton));
...
public class ClickAction extends AbstractAction {
private JButton button;
public ClickAction(JButton button) {
this.button = button;
}
public void actionPerformed(ActionEvent e) {
button.doClick();
}
}
A complete example is in shortcut-buttons-src-0.1.jar. The download includes an ant build file. To compile and run, just type "ant run" at the shell prompt. You will need ant and a JDK >= 1.5 installed.
Thursday, 9 August 2007
Making the most of vim: ant and javac
With a bit of rather tricky configuration it is possible integrate vim, ant and javac (the standard Java compiler).
First, vim allows us to set an external program to be called for the :make command. This goes in ~/.vimrc
set makeprg=ant
Now, when you issue a :make command to vim, it will call ant to build the project. Now with a bit more configuration, we can get vim to parse the error messages from javac and step through them.
Back to ~/.vimrc to tell vim what javac's messages look like:
set efm=\ %#[javac]\ %#%f:%l:%c:%*\\d:%*\\d:\ %t%[%^:]%#:%m,\%A\ %#[javac]\ %f:%l:\ %m,%-Z\ %#[javac]\ %p^,%-C%.%#
Make sure that horrendous code goes on one line. It came from a bit of head scratching and a lot of vim's documentation.
Now, after a :make you can step forward and back through the compiler messages using :cn and :cp.
That is all the IDE I need -- just enough help without getting in the way.
First, vim allows us to set an external program to be called for the :make command. This goes in ~/.vimrc
set makeprg=ant
Now, when you issue a :make command to vim, it will call ant to build the project. Now with a bit more configuration, we can get vim to parse the error messages from javac and step through them.
Back to ~/.vimrc to tell vim what javac's messages look like:
set efm=\ %#[javac]\ %#%f:%l:%c:%*\\d:%*\\d:\ %t%[%^:]%#:%m,\%A\ %#[javac]\ %f:%l:\ %m,%-Z\ %#[javac]\ %p^,%-C%.%#
Make sure that horrendous code goes on one line. It came from a bit of head scratching and a lot of vim's documentation.
Now, after a :make you can step forward and back through the compiler messages using :cn and :cp.
That is all the IDE I need -- just enough help without getting in the way.
Wednesday, 8 August 2007
Using the Presentation Model pattern
I came across the Presentation Model pattern while looking at other libraries at JGoodies. This is an interesting alternative to the usual Model, View, Controller approach to implementing a Java & Swing GUI. It fits better with the way Swing works and JGoodies binding puts it all together beautifully. The pattern is described in detail on Martin Fowler's site.
At work, we are are about to begin coding a pretty large Swing application which will model most of the company's business. We are going to be using JGoodies binding and Presentation Model for this. I wrote a reasonably complex prototype to test out some ideas we are considering using. I used the same prototype to try out Presentation Model and binding. I like it a lot. There is a bit of a learning curve at the start, but this is rewarded with a lot less code to write. In particular, there is less boring, repetitive and error-prone code to write to synchronise the view with the domain model.
Anyone about to implement a Java Swing application should take a look.
At work, we are are about to begin coding a pretty large Swing application which will model most of the company's business. We are going to be using JGoodies binding and Presentation Model for this. I wrote a reasonably complex prototype to test out some ideas we are considering using. I used the same prototype to try out Presentation Model and binding. I like it a lot. There is a bit of a learning curve at the start, but this is rewarded with a lot less code to write. In particular, there is less boring, repetitive and error-prone code to write to synchronise the view with the domain model.
Anyone about to implement a Java Swing application should take a look.
Subscribe to:
Posts (Atom)
