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
***********************************************************************

Put JRadioButton into list

Complete source code below will show you, how to put JRadioButton into list. During my try using java i found no way to put JRadioButton into JList. So i try to create it on my own. I hope you enjoy what i share with you.

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


import javax.swing.*;

import java.awt.GridLayout;

import java.awt.Color;

public class AddRadioButtonIntoList
{
public static void main(String[]args)
{
//Create radio button using JRadioButton that will be put into list
JRadioButton radioButton1=new JRadioButton("Duck");
JRadioButton radioButton2=new JRadioButton("Sheep");
JRadioButton radioButton3=new JRadioButton("Cow");
JRadioButton radioButton4=new JRadioButton("Buffalo");
JRadioButton radioButton5=new JRadioButton("Horse");
JRadioButton radioButton6=new JRadioButton("Goat");

//Set all radio button background color to white
radioButton1.setBackground(Color.WHITE);
radioButton2.setBackground(Color.WHITE);
radioButton3.setBackground(Color.WHITE);
radioButton4.setBackground(Color.WHITE);
radioButton5.setBackground(Color.WHITE);
radioButton6.setBackground(Color.WHITE);

//Create button group for JRadioButton
ButtonGroup bg=new ButtonGroup();

//Add all created radio button into button group
bg.add(radioButton1);
bg.add(radioButton2);
bg.add(radioButton3);
bg.add(radioButton4);
bg.add(radioButton5);
bg.add(radioButton6);

//Create panel that will be use to put all radio button into it
JPanel panel=new JPanel(new GridLayout(6,1));

//Add all created radio button into panel
panel.add(radioButton1);
panel.add(radioButton2);
panel.add(radioButton3);
panel.add(radioButton4);
panel.add(radioButton5);
panel.add(radioButton6);

//Create scroll bar for created panel
//Vertical scrollbar always show
//Horizontal scrollbar never show
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

//Create window using JFrame with title ( Add JRadioButton into list )
JFrame frame=new JFrame("Add JRadioButton into list");

//Add created scroll bar into JFrame
frame.add(scrollBar);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,100);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Get index of character from String

Complete source code below will show you, how to get index of a character from a String.

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


public class GetIndexOfCharacterFromString
{
public static void main(String[]args)
{
//Create a String
String a="ABCD";

//Create an int variable that will store index for character C
int index=a.indexOf('C');

//Print index that we get
System.out.println("Index for C is : "+index);
}
}


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

Get substring from String

Complete source code below will show you, how to get substring from a String.

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


public class GetSubstringFromString
{
public static void main(String[]args)
{
//This source code will show you how to get substring from a String
//We will get String "love" from String "I love you"
String a="I love you";

//We will use substring() method
//2=index in "I love you" for 'l'
//6=(index in "I love you" for 'e')+1
String b=a.substring(2,6);

//Print substring that we get
System.out.println(b);
}
}


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

Determine String end with specified word

Complete source code below will show you, how to determine whether a String end with specified word or not.

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


public class DetermineStringEndWithWord
{
public static void main(String[]args)
{
//First String
//You can try change it
String a="Hello";

//Second String
//You can try change it
String b="llo";

//Determine if first String end with second String or not
//If yes, program will print YES, otherwise it will print NO
if(a.endsWith(b))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}


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

Determine String start with specified word

Complete source code below will show you, how to determine whether a String start with specified word or not.

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


public class DetermineStringStartWithWord
{
public static void main(String[]args)
{
//First String
//You can try change it
String a="Hello";

//Second String
//You can try change it
String b="Hel";

//Determine if first String start with second String or not
//If yes, program will print YES, otherwise it will print NO
if(a.startsWith(b))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You