Giter Club home page Giter Club logo

multiline-label's Introduction

Java CI with Maven License Latest Version javadoc

multiline-label

Java Swing component to display a plain, left-to-right text (single line or multiline). Allows to specify preferred wrapping width (in pixels) for cases when there is not enough room to display the text as a single line.

Motivation: Though standard Swing provides a number of options to display multiline text (e.g. JLabel with HTML, read-only JTextArea, etc.), none of them IMHO is very convenient and straightforward. The goal of this component is to provide a multiline label with predictable and easily configurable behavior.

The label allows to specify custom separator characters (a space character by default). See MultilineLabel.setSeparators. The label always honors line breaks (e.g. \n) if they are present in the text.

The label implements javax.swing.Scrollable interface to support scrolling. By default, up to 20 lines will be displayed without vertical scroll bar. See MultilineLabel.setPreferredViewportLineCount.

The MultilineLabel class provides a number of static methods to support multiline text painting on components other than the MultilineLabel. For example, MultilineLabel.calculatePreferredSize method.

Example:

import javax.swing.border.EmptyBorder;
import java.util.Set;

import io.github.parubok.text.multiline.MultilineLabel;

var label = new MultilineLabel();
label.setText(myText); // set text - possibly requiring multiline presentation
label.setPreferredWidthLimit(330); // the label's preferred width won't exceed 330 pixels
label.setLineSpacing(1.2f); // relative spacing between adjacent text lines
label.setMaxLines(30); // limit the label to 30 lines of text
label.setBorder(new EmptyBorder(10, 5, 10, 5));
label.setSeparators(Set.of(' ', '/', '|', '(', ')')); // allow separators other than space
panel.add(label); // add label to its parent container

A demo application is provided. See io.github.parubok.text.multiline.demo.Demo.

alt tag

This library is packaged as a Java 9 module io.github.parubok.text.multiline (with a single dependency on a system module java.desktop).

This project has no external dependencies (except JUnit 5, for testing).

Requires Java 11 or later.

License

This project is licensed under Apache License, version 2.0

Installation

Releases are available in Maven Central

Maven

Add this snippet to the pom.xml dependencies section:

<dependency>
    <groupId>io.github.parubok</groupId>
    <artifactId>multiline-label</artifactId>
    <version>1.20</version>
</dependency>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.