is there any layout manager or some other way to add a label or any other component starting from a specific point like we draw rectangles i need this because i want to move the label around with input.

Hey gauravk_bhanot

Turn the layout manager off by setting the layout manager to null.
Then you can position components yourself.

The following code may help you where clicking mouse may relocate the label.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MovingLabel extends JFrame{
	int x,y; // define the location of a label
	JLabel label; // movable label
	Container container;
	MovingLabel(){
		super("Test a movable label");
		x=y=100;
		label= new JLabel("label relocated");
        Container container= getContentPane();
        setLayout(null); // Turn the layout manager off by setting the layout manager to null, as NormR1 indicated
		container.add(label); // place the label into the container
		label.setBounds(100,100,150,30);//position and sizing the label yourself
		container.addMouseListener(new MouseAdapter(){ // add MouseListener
			public void mouseClicked(MouseEvent e){
				x = e.getX();
				y = e.getY();
				label.setBounds(x,y,150,50); // relocating label. You may also resizing the label by altering the 3rd and 4th arguments
			}
		});
		setSize(900,500);
		setVisible(true);
	}	
	public static void main(String args[]){
		MovingLabel m = new MovingLabel();
		m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}

You can add label at specific location by

// create a label
  JLabel jl_Name = new JLabel("NEW_PROGRAMMER");
//Location 
jl_Name.setBounds(int,int,int,int);

/* first parameter : Starting X point
second parameter : Starting Y point
   third  parameter : How much pixels to right
 fourth  parameter : How much pixels downwards
*/

//Add label to panel
 JPanel jp_MyPanel = new JPanel();
jp_MyPanel.add(jl_Name);

The following code may help you where clicking mouse may relocate the label.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MovingLabel extends JFrame{
	int x,y; // define the location of a label
	JLabel label; // movable label
	Container container;
	MovingLabel(){
		super("Test a movable label");
		x=y=100;
		label= new JLabel("label relocated");
        Container container= getContentPane();
        setLayout(null); // Turn the layout manager off by setting the layout manager to null, as NormR1 indicated
		container.add(label); // place the label into the container
		label.setBounds(100,100,150,30);//position and sizing the label yourself
		container.addMouseListener(new MouseAdapter(){ // add MouseListener
			public void mouseClicked(MouseEvent e){
				x = e.getX();
				y = e.getY();
				label.setBounds(x,y,150,50); // relocating label. You may also resizing the label by altering the 3rd and 4th arguments
			}
		});
		setSize(900,500);
		setVisible(true);
	}	
	public static void main(String args[]){
		MovingLabel m = new MovingLabel();
		m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}

thanks again tong

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.