Posts

Showing posts from 2009

Coding java gui using Swing designing it with CSS

Building a GUI in java isn't complicated having netbeans as a great Swing gui builder. But what if we want to have some of the design left out of the code in a CSS file? As when building a java applet which should blend with a website, out best shot will be to use the same CSS file in the swing code as we use for the website. How can we design the GUI components using swing? We will use TK-UI's SwingCSSEngine (which is opensourced). Their documentation can be reached using the following link: http://tk-ui.sourceforge.net/user-guide/cssengine/swingcssengine.html How is it done? Download the libraries: http://sourceforge.net/projects/tk-ui/files/org.akrogen.tkui.css.swing/ Copy the jar files (also those in their "lib" directory [excluding the commons if you wish]) into your project's (the swing project) lib directory. In the code before initializing the swing components (a method which is actually called init...

Log All Your Web-method Calls

After a good deal of trial-and-error, and delving into the CXF code, I've found a way to get CXF to log every web-method call -- parameters and all. Put the following into your Tomcat's log4j.xml: <logger name="org.apache.cxf.service.invoker"> <level value="trace"> </level> </logger> The level should be set to "trace" by default; you won't get ton's of output, just a line (maybe 2) every time a web method is called. Unfortunately, the line is a bit wordy, as in Invoking method public boolean com.company.entities.ws.EntityManagementImpl.login(java.lang.String,java.lang.String,java.lang.String) throws java.io.UnsupportedEncodingException on object com.company.entities.ws.EntityManagementImpl@71e6b with params [CLI, arg2, arg3]. but it was either use the built in log statement or write a whole interceptor and add it to the chain processing the call. If we get really annoyed we could always tweak the source and rebuil...

Java Swing Applet: HowTo

This post explains how to take an existing Java Swing application and wrap it as an applet which can then be used on any website. This post assumes the java swing class file (which is run in the original application) to be: FileUploadClientGUIView. Create the java applet wrapper as follows: package packageName ; import javax.swing.JApplet; import javax.swing.SwingUtilities; public class AppletStarter extends JApplet { //Called when this applet is loaded into the browser. public void init() { //Execute a job on the event-dispatching thread; //creating this applet's GUI. try { SwingUtilities. invokeAndWait ( new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System. err .println( "AppletStarter didn't complete successfully" ); } } private void createGUI() { //Create and set up the...

Exceptions in JUnit Tests

When an exception is thrown in a unit test, the unit test will fail, giving a red bar - unless there is a try/catch that catches the exception and swallows it quietly.  Therefore, you should make sure that catching an exception in a unit test is indeed what you really mean to do.  Should the test really handle the error and continue, perhaps succeeding, or should the test fail, giving a visual indication that something went wrong? I'd add that when the right behavior of a test case is a thrown Exception, one should add to the @TEST annotation one's expectation.  e.g. @Test(expected = NumberFormatException.class)

False condition comes true?

Image
This is a really weird bug I found in my program. I have an "if" condition, which takes a boolean variable as its condition, the variable's name is bMember. When I saw unexpected behaviour in my code and decided to debug it I saw the above surprising and un-understandable thing, the debug enters a falsed condition!? look at the picture above and see for yourselves. by the way, if someone knows why, please post a comment. Chaiavi

Getting the file names of a given directory as plain text

At the beginning I searched the web for a freeware which does the job, I didn't even think of paying for this simple job to be done. But then I found the simplest of all solution. Just go to the command prompt (Windows button + R) type in cmd and press enter - you are in the command line view. Go to the directory from which you want a file list as text. Then type: dir /B mark the text and press enter (used as copy in the console view) then paste the filelist from the clipboard anywhere you want. If you want the filelist to be in a text file you can shorten the process by executing the following command from the command line console window: dir /B > filename.txt Now the file's list will be in the file you typed (filename.txt). A thought: Some functions just don't need any fancy tool for doing them...

What does: "Fault occurred while processing" in the client mean? and how do you reveal the real exception?

I have this CXF client which sometimes throws normal exceptions but some of the other times it throws me a "Fault occurred while processing", well, what does that mean? When you get back a "Fault occurred while processing" it means that a web service threw an unchecked exception.  The CXF framework catches it and puts together a fault message which it sends back to the client.  The client’s stack trace at that point is pretty-much irrelevant . There may be a way to get CXF to log the server stack trace, but I haven’t found it yet (feel free to comment on the post if you found one).  There may also be a way to stick a handler into the flow to get the exception before constructing the fault message, but I haven’t found that yet either (ditto). If a checked-exception is thrown, then the exception – or at least its message – gets sent back to the client where it’s reconstructed and rethrown to be caught by the client. Debugging a problem like this, o...

Embed Nimbus's look and feel to your Java programs using Netbeans

This one gave me a little headache, cause it shouldn't really be an issue, but it turned out to be a non trivial tweak. What's the story? I use Netbeans 6.5 and Java 1.6.0_11 and I wanted my gui programs to "wear" the nimbus look and feel. How should I configure my netbeans gui builder to dress my program with the nimbus l&f? First thing I needed to configure my current project to use the latest JDK, it used as a default a previous version (1.6.0_07) which doesn't support the nimbus l&f. First thing was to add the latest jdk to the netbeans platform, go to Tools --> java platform and add the new jdk. The next task is to add the new jdk to the specific project; project properties --> Libraries and under the Java platform pick the new JDK to run this program.  Now that the JDK issue is set, it's time to set the actual L&F:  Project properties --> Application --> Desktop app --> Look & Feel --> com.sun.java.swing.plaf.nimbus.Nimb...

Ant Explorer graphical Ant build tool

Every serious java developer needs to create or edit some ant building script. Eclipse offers nice ant handling features which make the task much easier, eclipse's plugin (which is embeded into eclipse since version 3.1 or so) understands the ant scripting language thus enables not only syntax coloring but also nice mouse hovering which reveals values of properties, variable names which enables finding the source of each variable and also some nice debugging features. But, there is one field in which eclipse's ant handling lacks perfection, its in viewing the whole build file in one gaze graphically feature, that feature isn't supported by eclipse. Luckily enough, it is supported by YWorks, which created a nice and free util which loads an ant script file and shows it in a beautiful and so very useful way. I think that this util (which was released as a standalone as well as an eclipse and idea) is a must for any serious java developer. Unluckily, YWorks have remo...

D-link DU-E100 Driver (USB to Lan)

Several years ago (at about 2002) I found this store which got stuck with a large amount of these usb to lan cards, so they sold the card for about 6$ a piece; it's not that I needed one, it was just the great price. I bought with a friend of mine about 10-20 pieces and sold them for double the price. Anyway, I kept one for myself of course, and of course - I didn't use it till... today. My Ethernet card stopped working suddenly and I didn't have any access to the internet, I started looking for a solution, till I found this old card - old but useful (I'm writing this post using the same card). The problem was finding the driver, although I had the original 1.44 inch disk, it is useless when you don't have any slot to insert it - and no, you can't insert a diskette into a USB port. So I started looking in the internet, and I found out that D-Link doesn't have the driver on their site, and I couldn't find it anywhere else! I didn't have any choice but...

Get a feel for what your computer can do

When examining performance with an eye to optimization, it's worth developing a feel for how fast typical software operations actually are on modern hardware. That sense will help you know when to look deeper for the real cause of a bottleneck, or when to look elsewhere, or when to mistrust the results that your profiling tools are giving you. For instance, I ran the following short bit of code: TreeSet tree = new TreeSet (); int found = 0; long time = System.currentTimeMillis(); System.out.println("add #'s to tree"); for (int i = 0; i long time2 = System.currentTimeMillis(); System.out.println("elapsed:  " + (time2 - time)); System.out.println("find #'s in tree"); for (int i = 0; i     if (tree.contains(Math.random())) found++; long time3 = System.currentTimeMillis(); System.out.println("elapsed:  " + (time3 - time2)); Here's the output: add #'s to tree elapsed: 8240 find #'s in tree elapsed: 5476 We see that filling a J...

Use Parent/Interface types rather than implementation types

If you've got a method that returns, say, a  TreeMap  pointer, you can generally declare it to return a  Map ; you're unlikely to use a method of TreeMap  that isn't a method of  Map  (if there even are such things), so the calling code won't be affected. This allows you, though, to change the implementation class to, say,  HashMap  without any other code being affected. The same goes for parameters passed into a method. In general, only explicitly use the implementation type when creating the instance [i.e. new ()]. [ NOTE : There's an issue peculiar to C++ where one must make sure that the implementation class' destructor is virtual, or it won't be executed when one deletes a pointer of the parent-class type.]

The use of Java Sets

The Set interface requires that the underlying implementation can tell whether two objects added to it are the same, so that the second can be rejected (a Set is defined as only containing one copy of any given object).  Ensuring adherence to this requirement involves both the Set implementation and the mechanism by which the objects can be distinguished from one-another.  The two basic implementations that i use in my code, TreeSet and HashSet , handle this in different ways: TreeSets are ordered b-trees (or something very similar) of objects.  Because they’re ordered, the objects they contain must implement the Comparable interface, or alternatively the TreeSet itself may have a Comparator member; in either case, a compareTo () method is called to compare objects to one another.  compareTo () returns -1, 0 or 1, depending on whether the first argument is less than, equal to or greater than the second; this is how the nodes in the tree are ordered, and how the Tre...