Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Java Swing Set And Get Values

Related Tutorials


 
 

Share on Google+Share on Google+

Java Swing Set And Get Values

Advertisement
In this tutorial we will learn about how to set and get values using setter and getter methods. This example explains you how to use setter and getter method in Java Swing.

Java Swing Set And Get Values

In this tutorial we will learn about how to set and get values using setter and getter methods. This example explains you how to use setter and getter method in Java Swing.

Example

Here I am giving a simple example which will demonstrate you about how to use setter and getter methods in swing. This example is about showing the address of a person in two forms i.e. one is short address which will show only the person's first name, second name, and the phone number and the other is full address which will show the first name, second name, house number, address (add 1, add 2), city, pin code, phone number. To display the short address I have created a class named ShortAddress which contains the fields firstName, secondName, phoneNumber and their setter and getter methods and a display method which displays the short address then to display the full address I have created a class named FullAddress which extends the ShortAddress class and contains the fields houseNumber, add1, add2, cityName, pincode and their setter and getter methods and a display method which displays the full address. Then I have created a class named MainClass which contains the public static void main(String [] args). In this class I have created a method for creating the GUI as well as created a method for setting the values using setter methods. In the display() method of both ShortAddress and FullAddress classes I have used the getter methods for finding the values which are set by the setter methods in the MainClass.

Source Code

ShortAddress.java

package roseindia.net;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class ShortAddress {
	
	String firstName;
	String secondName;
	int phoneNumber;
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getSecondName() {
		return secondName;
	}
	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}
	public int getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(int phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	public void display(ShortAddress s)
	   {		
		//System.out.println("Full Address : ");
		JFrame frame;
		frame = new JFrame("Short Address");
		frame.setLayout(null);
		frame.setSize(300,200);
		frame.setVisible(true);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel fNameLabel = new JLabel("First Name ");
		JLabel sNameLabel = new JLabel("Second Name ");
		JLabel phNumLabel = new JLabel("Phone Number ");
		
		String firstName = s.getFirstName();
		JLabel fName = new JLabel(firstName);
		
		String secondName = s.getSecondName();
		JLabel sName = new JLabel(secondName);
		
		int phNumber = s.getPhoneNumber();
		JLabel phNum = new JLabel(""+phNumber);
     
		fNameLabel.setBounds(10,30,100,20);
		fName.setBounds(200,30,120,30);   

		sNameLabel.setBounds(10,60,150,20);
		sName.setBounds(200,60,120,30);        

		phNumLabel.setBounds(10,90,150,20);
		phNum.setBounds(200,90,120,30);
		
		frame.add(fNameLabel);        
		frame.add(fName);

		frame.add(sNameLabel);        
		frame.add(sName);
     
		frame.add(phNumLabel);        
		frame.add(phNum);       
	    
	   }
	}

FullAddress.java

package roseindia.net;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class FullAddress extends ShortAddress{	

	String houseNumber;
	String add1;
	String add2;
	String cityName;
	int pincode;	
	
	public String getHouseNumber() {
		return houseNumber;
	}
	public void setHouseNumber(String houseNumber) {
		this.houseNumber = houseNumber;
	}
	public String getAdd1() {
		return add1;
	}
	public void setAdd1(String add1) {
		this.add1 = add1;
	}
	public String getAdd2() {
		return add2;
	}
	public void setAdd2(String add2) {
		this.add2 = add2;
	}
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public int getPincode() {
		return pincode;
	}
	public void setPincode(int pincode) {
		this.pincode = pincode;
	}
	
	public void display(FullAddress f)
	   {		
		JFrame frame;
		frame = new JFrame("Full Address");
        frame.setLayout(null);
        frame.setSize(400,400);
        frame.setVisible(true);
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel fNameLabel = new JLabel("First Name ");
        JLabel sNameLabel = new JLabel("Second Name "); 
        JLabel label = new JLabel("Address Detail");   
        JLabel hNumLabel = new JLabel("House Number ");         
        JLabel addLine1Label = new JLabel("Add Line 1 ");
        JLabel addLine2Label = new JLabel("Add Line 2 ");
        JLabel cityLabel = new JLabel("City ");
        JLabel pinCodeLabel = new JLabel("PIN Code ");
        JLabel phNumLabel = new JLabel("Phone Number ");
        
        String firstName = f.getFirstName();
        JLabel fName = new JLabel(firstName);
        
        String secondName = f.getSecondName();
        JLabel sName = new JLabel(secondName);
        
        String houseNumber = f.getHouseNumber();
        JLabel hNum = new JLabel(houseNumber);
        
        String addLine1 = f.getAdd1();
        JLabel street1 = new JLabel(addLine1);
        
        String addLine2 = f.getAdd2();
        JLabel street2 = new JLabel(addLine2);
        
        String cityName = f.getCityName();
        JLabel city = new JLabel(cityName);
        
        int pCode = f.getPincode();
        JLabel pinCode = new JLabel(""+pCode);
        
        int phNumber = f.getPhoneNumber();
        JLabel phNum = new JLabel(""+phNumber);
        
        fNameLabel.setBounds(10,30,100,20);
        fName.setBounds(300,30,120,30);   

        sNameLabel.setBounds(10,60,150,20);
        sName.setBounds(300,60,120,30);        

        label.setBounds(10,90,150,20);
        
        hNumLabel.setBounds(10,120,150,20);
        hNum.setBounds(300,120,120,30);
        
        
        addLine1Label.setBounds(10,150,150,20);
        street1.setBounds(300,150,120,30);
        

        addLine2Label.setBounds(10,180,150,20);
        street2.setBounds(300,180,120,30);
                
        cityLabel.setBounds(10,210,150,20);
        city.setBounds(300,210,120,30);
       
        pinCodeLabel.setBounds(10,240,150,20);
        pinCode.setBounds(300,240,120,30);
        
        phNumLabel.setBounds(10,270,150,20);
        phNum.setBounds(300,270,120,30);
                
        frame.add(fNameLabel);        
        frame.add(fName);

        frame.add(sNameLabel);        
        frame.add(sName);

        frame.add(label);
        
        frame.add(hNumLabel);
        frame.add(hNum);

        frame.add(addLine1Label);        
        frame.add(street1);
        
        frame.add(addLine2Label);        
        frame.add(street2);
        
        frame.add(cityLabel);        
        frame.add(city);
        
        frame.add(pinCodeLabel);        
        frame.add(pinCode);
        
        frame.add(phNumLabel);        
        frame.add(phNum);       
	    
	   }
}

MainClass.java

package roseindia.net;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MainClass implements ActionListener
{
	 ShortAddress sa = new ShortAddress();
	 FullAddress fa = new FullAddress();	 
	 
     JTextField fName = new JTextField();
     JTextField sName = new JTextField();
     JTextField phNum = new JTextField();
     JTextField hNum = new JTextField();
     JTextField add1 = new JTextField();
     JTextField add2 = new JTextField();
     JTextField city = new JTextField();
     JTextField pinCode = new JTextField();
     
     JButton sButton = new JButton("Short Address");
     JButton dButton = new JButton("Full Address");
     JFrame frame;    
     
     public void createUI()
      {         
         frame = new JFrame("Show Address Example");
         frame.setLayout(null);
         frame.setSize(700,500);
         frame.setVisible(true);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         JLabel lab1 = new JLabel("Enter First Name ");
         JLabel lab2 = new JLabel("Enter Second Name "); 
         JLabel lab3 = new JLabel("Address Detail.....");   
         JLabel lab4 = new JLabel("Enter House Number ");         
         JLabel lab5 = new JLabel("Add Line 1 ");
         JLabel lab6 = new JLabel("Add Line 2 ");
         JLabel lab7 = new JLabel("City ");
         JLabel lab8 = new JLabel("PIN Code ");
         JLabel lab9 = new JLabel("Enter Phone Number ");        
         
         lab1.setBounds(10,40,100,20);
         fName.setBounds(370,40,120,30);

         lab2.setBounds(10,70,150,20);
         sName.setBounds(370,70,120,30);         

         lab3.setBounds(10,100,150,20);
         
         lab4.setBounds(10,130,150,20);
         hNum.setBounds(370,130,120,30);         
         
         lab5.setBounds(10,160,150,20);
         add1.setBounds(370,160,120,30);         

         lab6.setBounds(10,190,150,20);
         add2.setBounds(370,190,120,30);         
         
         lab7.setBounds(10,220,150,20);
         city.setBounds(370,220,120,30);         
         
         lab8.setBounds(10,250,150,20);
         pinCode.setBounds(370,250,120,30);         
         
         lab9.setBounds(10,280,150,20);
         phNum.setBounds(370,280,120,30);         
         
         sButton.setBounds(50, 430, 150, 30);
         sButton.addActionListener(this);
         
         dButton.setBounds(300, 430, 150, 30);
         dButton.addActionListener(this);
         
         frame.add(lab1);        
         frame.add(fName);

         frame.add(lab2);        
         frame.add(sName);

         frame.add(lab3);
         
         frame.add(lab4);
         frame.add(hNum);

         frame.add(lab5);        
         frame.add(add1);
         
         frame.add(lab6);        
         frame.add(add2);
         
         frame.add(lab7);        
         frame.add(city);
         
         frame.add(lab8);        
         frame.add(pinCode);
         
         frame.add(lab9);        
         frame.add(phNum);
         
         frame.add(sButton);
         frame.add(dButton);

      }// createUI() method closed
     
     public void setValues()
     {
    	 sa.setFirstName(fName.getText());
    	 sa.setSecondName(sName.getText());
    	 sa.setPhoneNumber(Integer.parseInt(phNum.getText()));
    	 
    	 fa.setFirstName(fName.getText());
    	 fa.setSecondName(sName.getText());
    	 fa.setPhoneNumber(Integer.parseInt(phNum.getText()));
    	 fa.setHouseNumber(hNum.getText());
    	 fa.setAdd1(add1.getText());
    	 fa.setAdd2(add2.getText());
    	 fa.setCityName(city.getText());
    	 fa.setPincode(Integer.parseInt(pinCode.getText()));
     }// setValues() method closed
     
     @Override
 	public void actionPerformed(ActionEvent e) {
    	 JButton newButton = (JButton)e.getSource();
    	 setValues();
    	 if(newButton.equals(sButton))
    	  {
    		 sa.display(sa);
    	  }
    	 if(newButton.equals(dButton))
    	 {    		
    		fa.display(fa); 
    	 }    	 
 	}

      public static void main(String args[])
        {
            MainClass main = new MainClass();
            main.createUI();
        }// main() method closed	

}// class closed

Output

When you will compile and execute the MainClass.java then the output will be as follows :

At first a GUI will be opened for filling the values so fill the values in the respective fields as I have filled in the image below :

After filling the fields when you will click on the buttons showed on GUI the respective address will be displayed in another window :

You can download the source code from the link given below.

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 15, 2013

Related Tutorials

Discuss: Java Swing Set And Get Values  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com