Showing posts with label location. Show all posts
Showing posts with label location. Show all posts

Get current cursor location in JTextArea

Complete source code below will show you, how to get current cursor location in JTextArea.

***********************************************************************
COMPLETE SOURCE CODE FOR : GetCurrentCursorLocationInJTextArea.java
***********************************************************************


import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class GetCurrentCursorLocationInJTextArea extends JTextArea implements MouseMotionListener
{
//Create window using JFrame with title ( Get current cursor location in JTextArea )
JFrame frame=new JFrame("Get current cursor location in JTextArea");

//Create text field that will show you, current cursor location
JTextField textFieldX=new JTextField(5);
JTextField textFieldY=new JTextField(5);

//Create label that will use to describe function for each text field
JLabel labelX=new JLabel("Coordinate X : ");
JLabel labelY=new JLabel("Coordinate Y : ");

//Create panel that will use to store text field and label
JPanel panelX=new JPanel();
JPanel panelY=new JPanel();

//Panel that will use to store panelX and panelY
JPanel container=new JPanel();

public GetCurrentCursorLocationInJTextArea()
{
//Add mouse motion listener to JTextArea
addMouseMotionListener(this);

//Set container layout using BorderLayout
container.setLayout(new BorderLayout());

//Set panelX and panelY layout using GridLayout
panelX.setLayout(new GridLayout(1,2));
panelY.setLayout(new GridLayout(1,2));

//Add labelX and textFieldX into panelX
panelX.add(labelX);
panelX.add(textFieldX);

//Add labelY and textFieldY into panelY
panelY.add(labelY);
panelY.add(textFieldY);

//Add panelX and panelY into container
container.add(panelX,BorderLayout.WEST);
container.add(panelY,BorderLayout.EAST);

//Set frame layout using BorderLayout
frame.setLayout(new BorderLayout());

//Add JTextArea into JFrame
frame.add(this,BorderLayout.CENTER);

//Add container into JFrame
frame.add(container,BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}

public void mouseMoved(MouseEvent event)
{
//Get current X-coordinate for cursor
textFieldX.setText("");
textFieldX.setText(Integer.toString(event.getX()));

//Get current Y-coordinate for cursor
textFieldY.setText("");
textFieldY.setText(Integer.toString(event.getY()));
}

public void mouseDragged(MouseEvent event)
{
//Do nothing because our case not handle dragging
}

public static void main(String[]args)
{
GetCurrentCursorLocationInJTextArea gcclijta=new GetCurrentCursorLocationInJTextArea();
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Determine character location in a String

Now, i will share with you, how to determine location of a character in a String. Ok...firstly you must know location of a character in a String is determine using 'index'. Index for first character in a String is equal to 0 and each character include white space after that will add 1.

Note : Character location = index

String a="Hello dude";

Below is a list all character in String above with it's index.

H - 0
e - 1
l - 2
l - 3
o - 4
white space - 5
d - 6
u - 7
d - 8
e - 9

How to know current location of jframe after it moved to other location on screen


Result of the current location for a jframe in this program is current coordinate of the top left corner for the jframe.

********************************************************************
COMPLETE SOURCE CODE FOR : HowToKnowCurrentLocationOfJframe.java
********************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import java.awt.Point;
import java.awt.Component;

public class HowToKnowCurrentLocationOfJframe extends ComponentAdapter
{
JFrame b=new JFrame("TRY MOVE ME");

public HowToKnowCurrentLocationOfJframe()
{
b.addComponentListener(this);

b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setSize(400,100);
b.setVisible(true);
}

public void componentMoved(ComponentEvent evt)
{
Component temp=(Component)evt.getSource();
JOptionPane.showMessageDialog(null,"CURRENT LOCATION : "+" X = "+(temp.getLocationOnScreen()).getX()+" Y = "+(temp.getLocationOnScreen()).getY(),"CURRENT LOCATION",JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String[]args)
{
HowToKnowCurrentLocationOfJframe myObject=new HowToKnowCurrentLocationOfJframe();
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Set jframe size and it's location when click on maximize button


***********************************************************
COMPLETE SOURCE CODE FOR : SettingBound.java
***********************************************************


/**
*THIS TUTORIAL WILL SHOW YOU HOW TO SET THE MAXIMUM SIZE OF YOUR FRAME
*AND IT'S LOCATION WHEN YOU CLICK
*MAXIMIZE BUTTON BESIDE JFRAME CLOSE BUTTON
**/

import java.awt.Rectangle;
import javax.swing.JFrame;

public class SettingBound
{
public static void main(String[]args)
{
//first value, 400=x coordinate on your screen
//second value, 400=y coordinate on your screen
//third value, 800=JFrame width
//fourth value, 200=JFrame height
Rectangle rec=new Rectangle(400,400,800,200);

JFrame frame1=new JFrame("Setting Bound");

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setMaximizedBounds(rec);//set maximize setting for your JFrame
frame1.setVisible(true);
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

RELAXING NATURE VIDEO