Currently, I've been trying to disable the JTextField of a JFileChooser that I am working.
At first blush I thought: "piece of cake"
Unfortunately, the Java Standard API does not implement it. So I've tried something like this to reach my goal.
Components[] comp = myJFileChooser.getComponents();
for(Component c: comp) {
if(c instanceof JTextField) {
c.setVisible(false);
}
}
It didn't work out.
But, the following idea worked :)
Click here to check out the solution.
Indeed the dificulty of this problem is to get the reference of the target JTextField. A single call of myJFileChooser.getComponents() is not enough because the text field is not directly involved by the JFileChooser.
Saturday, December 5, 2009
Tuesday, May 12, 2009
Running .bat file using Java
Hello folks,
I know that some people try to find some information in the blog about .bat execution using Java.
It's perfectly possible. Compile and execute the following code:
public class RunBat {
public static void main(String[] args) throws Exception {
String batFileName = "name.bat"
Process p = Runtime.getRuntime().exec(batFileName);
System.out.println("Waiting .bat file finishes execution");
p.waitFor();
}
}
The String batFileName can assume one of the following values:
1-) .Bat file simple name if the .class of the .java above is in the same directory.
2-) .Bat file simple name if it is recognized by the operating system, e.g: The file can be found by the path.
3-) .Bat absolute name.
I know that some people try to find some information in the blog about .bat execution using Java.
It's perfectly possible. Compile and execute the following code:
public class RunBat {
public static void main(String[] args) throws Exception {
String batFileName = "name.bat"
Process p = Runtime.getRuntime().exec(batFileName);
System.out.println("Waiting .bat file finishes execution");
p.waitFor();
}
}
The String batFileName can assume one of the following values:
1-) .Bat file simple name if the .class of the .java above is in the same directory.
2-) .Bat file simple name if it is recognized by the operating system, e.g: The file can be found by the path.
3-) .Bat absolute name.
Saturday, April 4, 2009
Adding an icon on a executable file with C/C++
Hello everybody,
Many days without post because I was thinking about a diferent idea to publish.
Finally, it showed up.
Every compilers that I've used add the default icon when an executable is created. As a matter of design, I've been looking for a way to add an own icon on the executable.
I got the achievement with C/C++ in Windows.
C/C++ programs can use a resources file. It is a text file that can be written manually by the programmer. Many information can be inserted on executable through it i.e: Description, Company, Version...and the name of the icon file.
Steps:
1-) Icon file must have the .ico extension. (We will use "im.ico")
2-) Create the resources file with .rc extension. (We will use "recTest.rc")
3-) Write the following lines in it:
#include <winver.h>
MEUAPLIC ICON DISCARDABLE "im.ico"
1 VERSIONINFO
FILEVERSION 3,3,0,0
PRODUCTVERSION 3,3,0,0
#ifdef _DEBUG
FILEFLAGS 0xbl
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904B0"
BEGIN
VALUE "CompanyName", "GenericCompany\0"
VALUE "FileDescription", "GenericApplication\0"
VALUE "FileVersion", "1.0\0"
VALUE "InternalName", "1.0\0"
VALUE "LegalCopyright", "Copyright \251 Generic Company. 1997\0"
VALUE "LegalTrademarks", "Generic Trademark.\0"
VALUE "OriginalFilename", "\0"
VALUE "ProductName", "Generic Application.\0"
VALUE "ProductVersion", "1.0\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
4-) Create the file "recTestLib.rc" with the following content:
#include "recTest.rc"
5-) Use the following arguments to the program windres.exe(installed with Devian) to transform the resources text file to binary.
windres -i recTestLib.rc --input-format=rc -o recTestLib.res -O coff
6-) Compile your source file (g.e: source.c) with gcc using the following arguments:
gcc source.c recTestLib.res -o "source"
Done!
The icon was added and in addition some information was inserted on the executable. Check them by selecting the executable and press with the right mouse button, click in properties and finally click on "Version" tab.

p.s:
1-) If you don't want to do all this job only create the resources file and add it in a Devian project.
2-) I won't explain the windres sintax int this post. To it does not get long. If you want to learn some more click on this link
3-) The resources file has many utilities and one of them is the easy way to add graphical components on a Windows Frame. When I learn more about them I want to publish here.
Many days without post because I was thinking about a diferent idea to publish.
Finally, it showed up.
Every compilers that I've used add the default icon when an executable is created. As a matter of design, I've been looking for a way to add an own icon on the executable.
I got the achievement with C/C++ in Windows.
C/C++ programs can use a resources file. It is a text file that can be written manually by the programmer. Many information can be inserted on executable through it i.e: Description, Company, Version...and the name of the icon file.
Steps:
1-) Icon file must have the .ico extension. (We will use "im.ico")
2-) Create the resources file with .rc extension. (We will use "recTest.rc")
3-) Write the following lines in it:
#include <winver.h>
MEUAPLIC ICON DISCARDABLE "im.ico"
1 VERSIONINFO
FILEVERSION 3,3,0,0
PRODUCTVERSION 3,3,0,0
#ifdef _DEBUG
FILEFLAGS 0xbl
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904B0"
BEGIN
VALUE "CompanyName", "GenericCompany\0"
VALUE "FileDescription", "GenericApplication\0"
VALUE "FileVersion", "1.0\0"
VALUE "InternalName", "1.0\0"
VALUE "LegalCopyright", "Copyright \251 Generic Company. 1997\0"
VALUE "LegalTrademarks", "Generic Trademark.\0"
VALUE "OriginalFilename", "\0"
VALUE "ProductName", "Generic Application.\0"
VALUE "ProductVersion", "1.0\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
4-) Create the file "recTestLib.rc" with the following content:
#include "recTest.rc"
5-) Use the following arguments to the program windres.exe(installed with Devian) to transform the resources text file to binary.
windres -i recTestLib.rc --input-format=rc -o recTestLib.res -O coff
6-) Compile your source file (g.e: source.c) with gcc using the following arguments:
gcc source.c recTestLib.res -o "source"
Done!
The icon was added and in addition some information was inserted on the executable. Check them by selecting the executable and press with the right mouse button, click in properties and finally click on "Version" tab.
p.s:
1-) If you don't want to do all this job only create the resources file and add it in a Devian project.
2-) I won't explain the windres sintax int this post. To it does not get long. If you want to learn some more click on this link
3-) The resources file has many utilities and one of them is the easy way to add graphical components on a Windows Frame. When I learn more about them I want to publish here.
Sunday, March 29, 2009
.bat file
A .bat is a text file where commands can be written to be executed by DOS interpreter (I've never teste a .bat file in Unix/Linux). Which commands can we insert? Standard DOS commands, file names and the path variable contents.
Example:
Suppose in your current work directory the file Register.java that prints a register and we want to compile it, execute it, redericting the out to "out.txt" and at last open it. Instead of doing this manually we can create the test.bat with the following content.
javac Register.java
java Register > out.txt
out.txt
---END---Don't include this line in your file.
When the test.bat is double clicked or runned by command line the above tasks will be done. We have increased the productivity
We can use to simulate a Java executable. Only write the command in the .bat file.
java ClassName
This is not like a executable solution but it is better than write commands to the interpreter.
Example:
Suppose in your current work directory the file Register.java that prints a register and we want to compile it, execute it, redericting the out to "out.txt" and at last open it. Instead of doing this manually we can create the test.bat with the following content.
javac Register.java
java Register > out.txt
out.txt
---END---Don't include this line in your file.
When the test.bat is double clicked or runned by command line the above tasks will be done. We have increased the productivity
We can use to simulate a Java executable. Only write the command in the .bat file.
java ClassName
This is not like a executable solution but it is better than write commands to the interpreter.
Sunday, February 1, 2009
The Dijkstra Algorithm (Shortest Path) in Java
Hello Folks,
SearchGraphAlgorithms is a class that I've made that only has a single method search(type::TypeSearchEnum, v:int, cost:Graph):SearchInfo
Let's have a discussing about the O.O of this class:
This method was designed for diferent kinds of search. The enumeration TypeSearchEnum has only one type "DIJKSTRA", because is the unique search that I've implemented. After, we can insert other kinds as "BFS", "DFS","KRUSKAL",...
If "TypeSearchEnum" is "DIJKSTRA" the private method is called:
-dijkstra(v:int, cost:Graph<Double,Double>):SearchInfo
Obviously, this previous method searchs using Dijkstra algorithm in a graph defined by the interface Graph<K,V> (K = V = Double). Why an interface?
I could have created a signature for each type of graph e.g: "double[][] costs",
"{LinkedList<LinkedList<Double>> costs", "ArrayList<ArrayList<Double>> custos", "ArrayList<LinkedList<Double>> costs"...
It's possible think about a huge amount of signatures, that causes code multiplication. But, if a graph class implements the interface "Graph<K,V>" that contains appropriated access methods to the data structure, doesn't mind the implementation because this class is a "Graph<E,V>". So with a single signature we can solve this problem! (Polimorfism provided by the interface)
The method returns an object SearchInfo (I'll explain later) that contains the results of the search. Which are these results?
Each vertex is represented by an integer. The search result from a vertex "v" are the shortest paths from it to all other. A shortest path is not the number of the vertices but is the lowest distance. So these are the information that a SearchInfo object keeps. This class contains two public methods:
getPathFrom(vertex:int):LinkedList<Integer>
It returns the shortest path from a vertex "v" to an vertex "vertex" of the graph. A LinkedList is used because of the diferent number of vertices in a path and due the elements are added by addFirst(e:E) (O(1) execution)
getPathFrom(vertex:int):double
It returns the lowest distance from a vertex "v" to another "vertex" of the graph.
SearchInfo is an inner class of SearchGraphAlgorithms, public with private constructor. It was thought by the following way:
This class should contain a method that would build the paths from a vector of previous vertices generated by dijkstra method. But the class users must not have access to this method, so, it should be private. However, the class SearchGraphAlgorithms should be able to use the method to build the paths, so, the class SearchInfo should be inner of SearchGraphAlgorithms.
SearchInfo should be public to extern code, however, the constructor is created from an array of lowest distances that is created by "dijsktra" method, so, the constructor should be private.
The interface Graph contains only two methods:
+ get(i:int, j:int):V
+ size():int
As an application, I've created the class MatGraph that builds an adjacency matrix from a formated file
Ex:
8
0 * * * * * * *
30 0 * * * * * *
100 80 0 * * * * *
* * 120 0 * * * *
* * * 150 0 25 * *
* * * 100 * 0 90 140
* * * * * * 0 100
170 * * * * * * 0
The first line must contains the number of vertices. The other lines contain the distances of the adjacencies. The symbol "*" is inserted when there is no conexion between two vertices.
Main is a class that runs all the codes as an application.
The vertices ordering start from the zero.
I hope that you've enjoyed!
p.s:
1- Be correct, if you copy these codes on your college work/project don't forget to write that I am the author. Remember yourself that this post is really easy to find and has an unique Objects Orientation, don't be fool.
2-If there is any kind of english error, I really would appreciate to know.
SearchGraphAlgorithms is a class that I've made that only has a single method search(type::TypeSearchEnum, v:int, cost:Graph
Let's have a discussing about the O.O of this class:
This method was designed for diferent kinds of search. The enumeration TypeSearchEnum has only one type "DIJKSTRA", because is the unique search that I've implemented. After, we can insert other kinds as "BFS", "DFS","KRUSKAL",...
If "TypeSearchEnum" is "DIJKSTRA" the private method is called:
-dijkstra(v:int, cost:Graph<Double,Double>):SearchInfo
Obviously, this previous method searchs using Dijkstra algorithm in a graph defined by the interface Graph<K,V> (K = V = Double). Why an interface?
I could have created a signature for each type of graph e.g: "double[][] costs",
"{LinkedList<LinkedList<Double>> costs", "ArrayList<ArrayList<Double>> custos", "ArrayList<LinkedList<Double>> costs"...
It's possible think about a huge amount of signatures, that causes code multiplication. But, if a graph class implements the interface "Graph<K,V>" that contains appropriated access methods to the data structure, doesn't mind the implementation because this class is a "Graph<E,V>". So with a single signature we can solve this problem! (Polimorfism provided by the interface)
The method returns an object SearchInfo (I'll explain later) that contains the results of the search. Which are these results?
Each vertex is represented by an integer. The search result from a vertex "v" are the shortest paths from it to all other. A shortest path is not the number of the vertices but is the lowest distance. So these are the information that a SearchInfo object keeps. This class contains two public methods:
getPathFrom(vertex:int):LinkedList<Integer>
It returns the shortest path from a vertex "v" to an vertex "vertex" of the graph. A LinkedList is used because of the diferent number of vertices in a path and due the elements are added by addFirst(e:E) (O(1) execution)
getPathFrom(vertex:int):double
It returns the lowest distance from a vertex "v" to another "vertex" of the graph.
SearchInfo is an inner class of SearchGraphAlgorithms, public with private constructor. It was thought by the following way:
This class should contain a method that would build the paths from a vector of previous vertices generated by dijkstra method. But the class users must not have access to this method, so, it should be private. However, the class SearchGraphAlgorithms should be able to use the method to build the paths, so, the class SearchInfo should be inner of SearchGraphAlgorithms.
SearchInfo should be public to extern code, however, the constructor is created from an array of lowest distances that is created by "dijsktra" method, so, the constructor should be private.
The interface Graph contains only two methods:
+ get(i:int, j:int):V
+ size():int
As an application, I've created the class MatGraph
Ex:
8
0 * * * * * * *
30 0 * * * * * *
100 80 0 * * * * *
* * 120 0 * * * *
* * * 150 0 25 * *
* * * 100 * 0 90 140
* * * * * * 0 100
170 * * * * * * 0
The first line must contains the number of vertices. The other lines contain the distances of the adjacencies. The symbol "*" is inserted when there is no conexion between two vertices.
Main is a class that runs all the codes as an application.
The vertices ordering start from the zero.
I hope that you've enjoyed!
p.s:
1- Be correct, if you copy these codes on your college work/project don't forget to write that I am the author. Remember yourself that this post is really easy to find and has an unique Objects Orientation, don't be fool.
2-If there is any kind of english error, I really would appreciate to know.
Starting
Hello Folks,
This is an english version of www.programminghome.blogspot.com
I've decided to make this version because of the non-brazilian that usually enter in the page but don't understand what is written.
For now, I will only translate the most visited and new posts.
This is an english version of www.programminghome.blogspot.com
I've decided to make this version because of the non-brazilian that usually enter in the page but don't understand what is written.
For now, I will only translate the most visited and new posts.
Subscribe to:
Posts (Atom)