DEV Community

NazarioLuis
NazarioLuis

Posted on

Implementing jTextField for Swing with Decimal Values and Thousand Separator

Introduction

When developing Java Swing applications, handling numeric inputs efficiently is essential, especially when dealing with decimal values and thousand separators. xNumberField is a custom JTextField component that simplifies this process, ensuring accurate input and formatting. This guide will walk you through installing and using xNumberField in your Swing project.

GitHub Repository: xNumberField

Installing xNumberField

If you want to use xNumberField in your Java Swing project with Maven, follow these steps:

1. Add JitPack Repository

In your pom.xml file, add the following repository in the <repositories> section:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
Enter fullscreen mode Exit fullscreen mode

2. Add Dependency

Include the following dependency inside the <dependencies> section to use version v1.0.0 of the library:

<dependency>
    <groupId>com.github.NazarioLuis</groupId>
    <artifactId>xNumberField</artifactId>
    <version>LATEST</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

3. Usage Example

Here is an example of how to use NumberTextField in a Swing application:


// Import the xNumberField component
import py.com.cs.xnumberfield.component;

// Create an instance of NumberTextField
NumberTextField numberTextField = new NumberTextField();

// Set an initial value of 1,000,000
numberTextField.setValue(1000000d);

// Retrieve the current value from the field
Double number = numberTextField.getValue();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)