Some Basic GUI Methods of Java before Event handling

Some basic GUI Methods of Java before Event Handling


logo

Introduction

Before we move forward I would like you to know some of the methods that are frequently used in and are important while building GUI (Graphical User Interface) based desktop application using java language. Java has many inbuilt methods that are used to do different task in the programming. 

Some of the Method are given below with the description and use of it;

a) setTitle(String title): It helps to provide the title to the frame. It is not necessary to user setTitle() method to provide the title to the frame. You can also provide title to the frame at the time of its object creation i.e JFrame f=new JFrame(“Title Here”);. And by using setTitle() method should be passed with the string value which will be the title of the frame i.e f.setTitle(“Title Here”);

b) setSize(int width,int vertical): It provides the size of the frame/container. This method should be passed with two integer value which will  be the width and height of the frame/container i.e f.setSize(400,300);. Without this method your frame may look like this. So it is important to call this method.

frame_without_setSize_method


c) setDefaultCloseOperation(int operation): This method is only applicable in case of SWING GUI. This method is used to close the frame. setDefaultCloseOperation() method should be passed the parameters to make it work. The value of the parameters are; JFrame.EXIT_ON_CLOSE, JFrame.DISPOSE_ON_CLOSE, JFrame.HIDE_ON_CLOSE and JFrame.DO_NOTHIG_ON_CLOSE.

setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE): It terminates the whole program when close button is clicked.

setDefaultCloseOperation (JFrame. DISPOSE_ON_CLOSE): It terminates the specific window when close button is clicked in case of multiple window of the program.

setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE): It hides the window when user clicks to the close button and runs in background.

setDefaultCloseOperation(JFrame.DO_NOTHIG_ON_CLOSE): It does not do anything even if the close button is clicked.

d) setLayout(LayoutManager manager): It is the method which is used for setting the layout in the frame. And in a frame layout must be added in-order add the components to the frame. It is set as setLayout(new FlowLayout();.

add(components comp): This method is used to add the components to the frame/container. It should be passed a component to its parameter i.e. f.add(button1);.

e) add(components comp): This method is used to add the components to the frame/container. It should be passed a component to its parameter i.e. f.add(button1);.

f) setVisible(boolean b): This method is used for making the frame visible to the user. It takes a boolean value i.e true or false. If this method is not called and not set to true i.e setVisible(true), you cannot see the frame and the program gets terminated automatically.

g) setText(String text): It is used to set the text in the textbox, textarea, label and also to set the title of the button. For example;
//for textbox
JTextField t;
t=new JTextField();
t.setText(“Hello”);

//for textarea
JTextArea ta;
ta=new JTextArea();
Ta.setText(“Kathmandu,Nepal);

//for button
JButton b1;
b1=new JButton();
b1.setText(“Title”);

//for label
JLabel l;
l=new JLabel();
l.setText(“Your Label”);

h) getText(): It is used to retrieve the text from the textbox,label, textarea and button. It is just opposite of the setText() method. It does not need any value to be passed because it does not have the parameter. For example;
//retrieve the text which is set above
JTextField t1;
t1=new JTextField();
String str=t.getText(); //text of t is copied to str

//now set the text into t1
t1.setText(str); //t1 will show the text to t now.

So these are the method you should know well before we start the tutorials.
******************************************************************

Post a Comment

0 Comments