Skip to main content

Posts

Showing posts with the label swing

Taking care of user inputs: Using jFormattedTextFields

When developing an application, it is critical to validate the user inputs. In order to give the right output the right input should be given. But the users don't always provide the right inputs as we expect, the right design should have the ability to deal with all kinds of bizarre inputs that user might give in. One way to handle them is validating the given inputs before we process it. But it is better to limiting the user to type something we don't expect. In Java swing we can use formatted text fields for this task. This is a quick tutorial for jFormattedTextFields using Netbeans. This will be enough to get it done, and I won't go more details on it. Formatted text field is very similar to the simple jTextField but we can set a format that it would only accept as an input. Therefor user will not be able to type anything else other than the given format. First let's look doing that easily using Netbeans IDE. Netbeans provides jFormattedTextField in the...

Day-to-day Programming 1: File names into a text file

How to get the names of the files in a specific directory (folder)  to a text file? Today my mother asked me to write some MP3s into a DVD. There were nearly 200 MP3 files. and she also asked me to number them and make a list of file names to print out. I was like o.O how am I going to do that? type all 200 file names into a document? I felt lazy and thought why can't I write a program that will do it for me !. so I wrote a simple program that gets file names and writes them into a text file, so I can print them out. This is the simple program I wrote. It took less than 2 minutes to write it. and saved a considerable amount of time!  ;) 1: import java.io.BufferedWriter; 2: import java.io.File; 3: import java.io.FileWriter; 4: import java.io.IOException; 5: 6: public class FilenamesToTxt { 7: public static void main(String[] args) throws IOException { 8: File folder = new File("C:/My Folder"); 9: BufferedWriter bf ...

How to set a background image to a jFrame?

in Eclipse in NetBeans IDE If you are using Netbeans IDE or windowbuilder in Eclipse to develop Swing GUIs, you might wanted to set background images to jFrames. The easiest way is putting a jLabel to cover whole jFrame and set an image to it. It might be ok Absolute layout but if you are using  something like SpringLayout this will not work. And also there might be problems with re-sizing the jFrame. Instead of that what we are going to do is put a jPanel to cover up the whole jFrame and set a background image to it. in Eclipse WindowBuilder I have already installed WindowBuilder plugin in my Eclipse and created a java project. And I have created a new jFrame form with WindowBuilder. (If you are not familiar with eclipse and windowbuilder please google it and read first) Here is the code for MyFrame class which is automatically created by windowbuilder 1: package org.jframebg; 2: import java.awt.BorderLayout; 3: import java.awt.EventQ...