0

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.

5
Contributors
5
Replies
12
Views
7 Years
Discussion Span
Last Post by gauravk_bhanot
0

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

0

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);
	}
	
}

Edited by tong1: n/a

0

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);
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.