Followers

Monday, April 19, 2010

Playing with GridBagLayout

/******************NEW POST********************************/
Hi guys,

This time we're gonna see how to make life easy while working with Swing in java.

Final Year students using java for their BE project, may find this article to be of usage.

  • All of us at some stage(while working with java), decide to build a standalone gui based application.
  • Now, we al like good and organized GUIs, but making one via swing can prove to be a bit tricky !!!
  • To place different items on a window frame in java, we hava something called as Layouts.
  • We've the different layouts like FlowLayout, Border Layout, CardLayout etc..
  • also worth mentioning are Gridlayout and GridBagLayout.
  • Now, I am saying that :-
Instead of using Other layouts or using setLayout(null) kind of things, it is best to use :

The GridBaglayout:-

/**************************************************************/

Funda about : System.out.println(obj); PRINTING AN OBJECT

/******************NEW POST********************************/
Hi guys,
Have you ever thought of:- how the following particular statement gets executed in java, and what's its real meaning:-

public void some_method()
{
....
....
public Object1 obj=new Object1();
System.out.println(obj1);
....
....
....
}


ANSWER:

internally, every object in java extends the "Object" clas, which we can call as the superclass for all the objects in java.

now there is a method "String toString(void)" in "Object" class.
So it gets inherited by all the classes.


the method "toString()" returns the String representation of the object, through which it is called.

When we say:

System.out.println(obj1);


this get internally interpreted as:-

System.out.println( obj1.toString() );

the default implementation of this method in Object returns some cryptic JVM generated code for the object.

Eg:-
for a JFrame object it printed:-

com.fns_issues.gui.Issue_Window[frame0,0,0,400x400,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,5,5,390x390,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16785865,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]

com.fns_issues.gui.Issue_Window being the fully qualified class name.



Now we can always override this method in our class of the concerned object.

Simply say:

public String toString()
{
return "hello here i am";
}


and

then:-

System.out.println(obj1);

will print:-

hello here i am.



So, that's all for now. See you guys.

Bye...

/**************************************************************/