Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, November 27, 2008

silly string problem!

Today is no different in office. We are nearing the release and test team is chucking out bugs tirelessly. Every day we have more bugs to fix :( As usual i started to work on with a high severity bug. I found the cause without any problem.

But then came a surprise to me. We have a function which takes an array and returns the index of any object you are looking for. So we have an array with few entries and search for an string which is there in the array. But the returned -1 (not found).

I am surprised and puzzled. One things i did was to replace the == with equals method and it worked well. But what I am not clear is why == didn't work. Since string is immutable any string object which has same data will be interned to the same reference in the string pool.

Then I searched on google and found that if any one of the string is read from file, then it is not interned.

consider the following code:
public class Test {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("c:\\test.txt"));

String str = reader.readLine();
str = str.trim();
String str2 = "A";
System.out.println(str==str2);

reader.close();
}

public final static int indexOf(T[] array, T t) {
for(int i = 0;i
if(array[i]==(t)) {
return i;
}
}
return -1;
}

}

content of the file is just "A", what do you expect the program to print? true? you are wrong.. answer is false. This is where we have to use the intern function in the string class.

Hope somebody will find it useful!!

Friday, June 06, 2008

Java. Is it dying...?

This is the first time I am reading that JavaOne is not good (2008).. So I just read along many other blogs and there are many blogs related to the end of java.. I asked to myself "Is java going to die?" and heard back a sound response.. No way! atleast anytime sooner.. I read a blog in which the author says that Java is not going to die anytime sooners and he gave some number on job requirement based on Dice.com and impressively Java is way to high than anything else. Almost twice the next language C++, Ruby and Phyton not even 10%..

As many are blaming about how bad Generic are in Java, but still I feel java is a better language to code on.. Another blog says "Your top biz guys doesn't mind what language you use.. they wants the things to be done easier, faster and better.. and many new languages like Ruby (not really new!) were much simpler and less verbose.. But still Java is very widely used and millions of people know it (though not properly!) Java is going to be there and Sun is doing great job in enhancing Java each version and adding new features to it...

So I feel Java is going to be there for some time and there is enough time to learn any new language that might get be popular!!!

So chill out..!!

Some blogs to read..

google killing java,
Another one

Monday, January 21, 2008

Looking at C# wearing Java Glass?

I always loved Microsoft products whether it is there operating system or any software development tool. The computer that I first used had windows 95 and from that Windows has always been my favorite operating system. So I have spent lot of time developing small projects only on windows (and till now!). By the time we studied Visual Basic and Java in our high school, I always loved VB, cursing Java for not having any IDE (atleast my teacher didn't introduce any Java IDE to us. But it is there at that time!). Most of the time we have to memorize the syntax of Java, where we do VB just like that. Java lab is always a big problem!! I hated Java to the core!!

But my career made me work on Java and I came to know lot of professional IDEs (eclipse) available for Java which are far better than Visual Basic IDEs. I still love Eclipse IDE more than Microsoft Visual C# express edition 2008 (never had a look at professional version though). As a fan of Microsoft, I thought to take a look at C# from the perspective of Java.

Sure C# had lots and lot of language structs and keyword to code on. But how frequently are they used? I really don't understand the reason for having a struct as well as class. But C# have the concept of Managed Pointers and also allows free pointer manipulating is allowed which is something cool! But I really don't want or expect Java to have those features. I am fine with what Java already has. But still I am learning C# is going to be fun!!!!

Happy coding!

Monday, January 14, 2008

Flat Button using swing

I always wonder about how people creating such a nice button and swing controls in swing. Swing provides the most customizable user interface. Though I am not expert in it, just tried myself to do something that will give me a better understanding of how all these things fit together.

All the components in swings are rendered by ComponentUI, so a component delegates the painting job to ComponentUI. For example, a JButton will be a rendered by BasicButtonUI (not exactly!). So ComponentUI which renders a component is decided based on the LAF (look & feel) selected. So to create a new look, you just create ComponentUI for all the swing components.

windows look and feel for JButton is provided by WindowsButtonUI.

When I first read, this I thought it's bit difficult to understand, actually it's pretty easy to understand. So I tried to create a Flat Button (you can think of them as buttons that appear on toolbar in standard Microsoft Office App.).

To create your own UI for Button, we start by extending MetalButtonUI, by just overriding some painting function, we can create customized LAF for the Button.

The picture below shows the button that I tried to implement.

Java code below shows how it is done:

public class JFlatButton extends JButton
{

protected PaintProperties paintProp;

public JFlatButton(String text)
{
super(text);
}

public JFlatButton(Action action) {
super(action);
}

public JFlatButton(Icon icon) {
super(icon);
}

public JFlatButton(String text,Icon icon) {
super(text,icon);
}

public void setUI(ButtonUI ui) {
// defaults
if(paintProp==null) {
paintProp = new PaintProperties();
paintProp.borderColor = Color.BLACK;
paintProp.rollOver1 = Color.WHITE;
paintProp.rollOver2 = Color.LIGHT_GRAY;

paintProp.pressed1 = paintProp.rollOver2;
paintProp.pressed2 = paintProp.rollOver1;
}
// override the UI
// always set our UI instead of platform based LAF!
super.setUI(new MetalButtonUI() {
protected void paintButtonPressed(Graphics g, AbstractButton b) {
// if back group paint required
if ( b.isContentAreaFilled() ) {
Dimension size = b.getSize();
Graphics2D g2d = (Graphics2D) g;

Paint paint = g2d.getPaint(); // save to restore

GradientPaint pt = new GradientPaint(new Point2D.Float(0,0),paintProp.pressed1,new Point2D.Float(0,b.getHeight()),paintProp.pressed2);
g2d.setPaint(pt);
g.fillRect(0, 0, size.width, size.height);

g.setColor(paintProp.borderColor);
g.drawRect(0,0,size.width-1,size.height-1);

g2d.setPaint(paint);
}
}

public void update(Graphics g, JComponent c) {
// under normal condition, don't render the background
// on roll over render my custom color
if(model.isRollover()) {
drawBG(g,c);
}
paint(g, c);
}

protected void drawBG(Graphics g,JComponent b) {
Graphics2D g2d = (Graphics2D) g;
Paint paint = g2d.getPaint(); // save to restore
GradientPaint pt = new GradientPaint(new Point2D.Float(0,0),paintProp.rollOver1,new Point2D.Float(0,b.getHeight()),paintProp.rollOver2);
g2d.setPaint(pt);
// fill button
g.fillRect(0,0,b.getWidth()-1,b.getHeight()-1);

// draw border line
g.setColor(paintProp.borderColor);
g.drawRect(0,0,b.getWidth()-1,b.getHeight()-1);

g2d.setPaint(paint); // restore
}

protected void paintFocus(Graphics g, AbstractButton b,
Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
// nothing, focus not rendered!
}
});
// override some properties
this.setBorderPainted(false);
this.setOpaque(false);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
}

public PaintProperties getPaintProp() {
return paintProp;
}

public void setPaintProp(PaintProperties paintProp) {
this.paintProp = paintProp;
}
}

Code is self explanatory and sufficiently commented. (I don't understand some code, I will always sun's similar implementation. They are commented well! really well!!!)

After reading about this and implementing something with what I learned, really helped me to understand it clearly.

Here you can find something similar to this but with JTabbedPane .. Really good resource..

Happy swinging!!

Friday, December 28, 2007

Read-Only Collections...

Recently I was looking at some open source code and I found out that there is utility function in Collections class which could come handy. When you have mutable Collection object and you don't want anybody to edit (add/delete item) the list, you can just derive immutable version of the same object using the following function..

Collections.unmodifiableList();

If somebody tries to edit the derived list, they will get a neat UnSupportedOperationException. You might wonder why would you need such a list.. If you are working on project with some 50 odd people and you need to make sure that your list (which could be used in some other place) should not be modified by any other fellow programmer, this is the right way to do..!

Happy programming !

This could really come handy in some scenarios as I said above..

Good Bye!