Java Tutorial – Swing Text Drag And Drop – Sample Example

In computer graphical user interfaces, drag-and-drop or DnD is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects. As a feature, support for drag-and-drop is not found in all software, though it is sometimes a fast and easy-to-learn technique for users to perform tasks. However, the lack of affordances in drag-and-drop implementations means that it is not always obvious that an item can be dragged.

In this tutorial, I will demonstrate the ability to do a drag and drop between a text field and a text area or vice versa in Java Swing GUI development. I will also show how dragging of a text from a text area or text field can be disabled, preventing the text from that field from getting dragged and drop. This is a very powerful feature of Java, and thanks to those who thought of simplifying developer’s life by implementing the feature in API. All we have to do now is to sit back and use the feature which has already been developed.

package com.kushal.gui;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

 * @author Kushal Paudyal
 * Last Modified On 07-14-2009
 * This class demonstrates the ability to do
 * drag and drop of selected text between a text field
 * and a text area.
public class DragAndDropDemo extends JFrame {
	private static final long serialVersionUID = 1L;

	public DragAndDropDemo()
		super(" - Drag and Drop Demo");

		Box verticalBox = Box.createVerticalBox();
		 * Defining a text field.
		 * Setting setDragEnabled() to true.
		 * You can drag on this field, or drag text from
		 * this field to any other field.
		JTextField textField = new JTextField(15);
		textField.setText("Select me and drag me to other fields.");

		 * Defining a text area.
		 * Setting setDragEnabled() to true.
		 * You can drag on this field, or drag text from
		 * this field to any other field.

		JTextArea textArea = new JTextArea(4, 25);
		textArea.setText(" is great.");
		verticalBox.add(new JScrollPane(textArea));

		 * Adding a text field by setting setDragEnabled() to false.
		 * You can drag over this text field, but cannot drag
		 * the text on this field to any other fields.
		JTextField textField1 = new JTextField(15);
		textField1.setText("You cannot drag me.");

		JPanel panel=new JPanel();

		 * Setting some common GUI Features


	 * Testing the DragAndDrop GUI program.
	public static void main(String[] args) {
		new DragAndDropDemo();


Here is the output:

Listing All Available Locales Using Java

This little Java Example/Tutorial shows how to list all available locales using java.

 * @author Kushal Paudyal
 * Last Modified On 2009-APR-06
package com.kushal.utils;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class ListOfAvailableLocales {

	public static void main(String[] args) {

	public static void printAvailableLocales()
		Locale list[] = SimpleDateFormat.getAvailableLocales();

		for (int i = 0; i < list.length; i++) {
			System.out.println(list[i].toString() + "tt"
					+ list[i].getDisplayName());


Here is the output of this program. Sorry the text spacing has been lost:

ar		Arabic
ar_AE		Arabic (United Arab Emirates)
ar_BH		Arabic (Bahrain)
ar_DZ		Arabic (Algeria)
ar_EG		Arabic (Egypt)
ar_IQ		Arabic (Iraq)
ar_JO		Arabic (Jordan)
ar_KW		Arabic (Kuwait)
ar_LB		Arabic (Lebanon)
ar_LY		Arabic (Libya)
ar_MA		Arabic (Morocco)
ar_OM		Arabic (Oman)
ar_QA		Arabic (Qatar)
ar_SA		Arabic (Saudi Arabia)
ar_SD		Arabic (Sudan)
ar_SY		Arabic (Syria)
ar_TN		Arabic (Tunisia)
ar_YE		Arabic (Yemen)
as_IN		Assamese (India)
be		Belarusian
be_BY		Belarusian (Belarus)
bg		Bulgarian
bg_BG		Bulgarian (Bulgaria)
bn_IN		Bengali (India)
ca		Catalan
ca_ES		Catalan (Spain)
ca_ES_PREEURO	Catalan (Spain,PREEURO)
cs		Czech
cs_CZ		Czech (Czech Republic)
cs_CZ_EURO	Czech (Czech Republic,Euro)
cs_CZ_PREEURO	Czech (Czech Republic,PREEURO)
cy_GB		Welsh (United Kingdom)
da		Danish
da_DK		Danish (Denmark)
da_DK_EURO	Danish (Denmark,Euro)
de		German
de_AT		German (Austria)
de_AT_PREEURO	German (Austria,PREEURO)
de_CH		German (Switzerland)
de_DE		German (Germany)
de_DE_PREEURO	German (Germany,PREEURO)
de_LU		German (Luxembourg)
de_LU_PREEURO	German (Luxembourg,PREEURO)
el		Greek
el_GR		Greek (Greece)
el_GR_PREEURO	Greek (Greece,PREEURO)
en		English
en_AU		English (Australia)
en_BE		English (Belgium)
en_BE_PREEURO	English (Belgium,PREEURO)
en_CA		English (Canada)
en_GB		English (United Kingdom)
en_GB_EURO	English (United Kingdom,Euro)
en_HK		English (Hong Kong S.A.R.)
en_IE		English (Ireland)
en_IE_PREEURO	English (Ireland,PREEURO)
en_IN		English (India)
en_NZ		English (New Zealand)
en_PH		English (Philippines)
en_PK		English (Pakistan)
en_SG		English (Singapore)
en_US		English (United States)
en_ZA		English (South Africa)
es		Spanish
es_AR		Spanish (Argentina)
es_BO		Spanish (Bolivia)
es_CL		Spanish (Chile)
es_CO		Spanish (Colombia)
es_CR		Spanish (Costa Rica)
es_DO		Spanish (Dominican Republic)
es_EC		Spanish (Ecuador)
es_ES		Spanish (Spain)
es_ES_PREEURO	Spanish (Spain,PREEURO)
es_GT		Spanish (Guatemala)
es_HN		Spanish (Honduras)
es_MX		Spanish (Mexico)
es_NI		Spanish (Nicaragua)
es_PA		Spanish (Panama)
es_PE		Spanish (Peru)
es_PR		Spanish (Puerto Rico)
es_PY		Spanish (Paraguay)
es_SV		Spanish (El Salvador)
es_US		Spanish (United States)
es_UY		Spanish (Uruguay)
es_VE		Spanish (Venezuela)
et		Estonian
et_EE		Estonian (Estonia)
et_EE_EURO	Estonian (Estonia,Euro)
et_EE_PREEURO	Estonian (Estonia,PREEURO)
fi		Finnish
fi_FI		Finnish (Finland)
fi_FI_PREEURO	Finnish (Finland,PREEURO)
fr		French
fr_BE		French (Belgium)
fr_BE_PREEURO	French (Belgium,PREEURO)
fr_CA		French (Canada)
fr_CH		French (Switzerland)
fr_FR		French (France)
fr_FR_PREEURO	French (France,PREEURO)
fr_LU		French (Luxembourg)
fr_LU_PREEURO	French (Luxembourg,PREEURO)
gu		Gujarati
gu_IN		Gujarati (India)
hi		Hindi
hi_IN		Hindi (India)
hr		Croatian
hr_HR		Croatian (Croatia)
hu		Hungarian
hu_HU		Hungarian (Hungary)
hu_HU_EURO	Hungarian (Hungary,Euro)
hu_HU_PREEURO	Hungarian (Hungary,PREEURO)
in		Indonesian
in_ID		Indonesian (Indonesia)
is		Icelandic
is_IS		Icelandic (Iceland)
it		Italian
it_CH		Italian (Switzerland)
it_IT		Italian (Italy)
it_IT_PREEURO	Italian (Italy,PREEURO)
iw		Hebrew
iw_IL		Hebrew (Israel)
ja		Japanese
ja_JP		Japanese (Japan)
kk		Kazakh
kk_KZ		Kazakh (Kazakhstan)
kn		Kannada
kn_IN		Kannada (India)
ko		Korean
ko_KR		Korean (South Korea)
kok_IN		kok (India)
lt		Lithuanian
lt_LT		Lithuanian (Lithuania)
lt_LT_EURO	Lithuanian (Lithuania,Euro)
lt_LT_PREEURO	Lithuanian (Lithuania,PREEURO)
lv		Latvian
lv_LV		Latvian (Latvia)
lv_LV_EURO	Latvian (Latvia,Euro)
lv_LV_PREEURO	Latvian (Latvia,PREEURO)
mk		Macedonian
mk_MK		Macedonian (Macedonia)
ml_IN		Malayalam (India)
mr		Marathi
mr_IN		Marathi (India)
ms		Malay
ms_MY		Malay (Malaysia)
nb_NO		Norwegian Bokmål (Norway)
nl		Dutch
nl_BE		Dutch (Belgium)
nl_BE_PREEURO	Dutch (Belgium,PREEURO)
nl_NL		Dutch (Netherlands)
nl_NL_PREEURO	Dutch (Netherlands,PREEURO)
no		Norwegian
no_NO		Norwegian (Norway)
no_NO_NY	Norwegian (Norway,Nynorsk)
or_IN		Oriya (India)
pa		Panjabi
pa_IN		Panjabi (India)
pl		Polish
pl_PL		Polish (Poland)
pl_PL_EURO	Polish (Poland,Euro)
pl_PL_PREEURO	Polish (Poland,PREEURO)
pt		Portuguese
pt_BR		Portuguese (Brazil)
pt_PT		Portuguese (Portugal)
pt_PT_PREEURO	Portuguese (Portugal,PREEURO)
ro		Romanian
ro_RO		Romanian (Romania)
ru		Russian
ru_RU		Russian (Russia)
sa		Sanskrit
sa_IN		Sanskrit (India)
sh		sh
sh_CS		sh (Serbia and Montenegro)
sh_YU		sh (YU)
sk		Slovak
sk_SK		Slovak (Slovakia)
sk_SK_EURO	Slovak (Slovakia,Euro)
sk_SK_PREEURO	Slovak (Slovakia,PREEURO)
sl		Slovenian
sl_SI		Slovenian (Slovenia)
sl_SI_EURO	Slovenian (Slovenia,Euro)
sl_SI_PREEURO	Slovenian (Slovenia,PREEURO)
sq		Albanian
sq_AL		Albanian (Albania)
sr		Serbian
sr_CS		Serbian (Serbia and Montenegro)
sr_YU		Serbian (YU)
sv		Swedish
sv_SE		Swedish (Sweden)
sv_SE_EURO	Swedish (Sweden,Euro)
sv_SE_PREEURO	Swedish (Sweden,PREEURO)
ta		Tamil
ta_IN		Tamil (India)
te		Telugu
te_IN		Telugu (India)
th		Thai
th_TH		Thai (Thailand)
th_TH_TH	Thai (Thailand,TH)
tr		Turkish
tr_TR		Turkish (Turkey)
uk		Ukrainian
uk_UA		Ukrainian (Ukraine)
vi		Vietnamese
vi_VN		Vietnamese (Vietnam)
zh		Chinese
zh_CN		Chinese (China)
zh_CN_HK	Chinese (China,HK)
zh_HK		Chinese (Hong Kong S.A.R.)
zh_SG		Chinese (Singapore)
zh_TW		Chinese (Taiwan)